P3226 [HNOI2012]集合选数

本文介绍了一种使用状态压缩动态规划(状压DP)解决矩阵中合法集合选择问题的方法,通过避免选择相邻元素来求解所有可能的组合,特别关注了矩阵规模为log级别的场景。

考虑构造矩阵
1 3 9 27......
2 6 18 54......
4 12 36 108......
......

发现在这个矩阵上一个合法的集合是一个满足选择的数字不相邻的集合,由于行数列数的大小都是log级别的,可以直接状压dp。
此外,不仅要以1位左上角做dp,还要分别以所有既不是2的倍数,也不是3的倍数的数字做dp。
把所有方案乘起来即可。

#include<iostream>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#define N 22
#define S 110000
#define eps 1e-7
#define inf 1e9+7
#define ll long long
using namespace std;
inline ll read()
{
    char ch=0;
    ll x=0,flag=1;
    while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*flag;
}
const ll mo=1000000001;
bool flag[S];
ll n,dp[N][S];
ll solve(ll k)
{
    dp[0][0]=1;
    ll x,a,b,last=0,ans=0;
    for(x=k,a=0;x<=n;x*=2)a++;
    for(ll i=1;i<=a;i++,k*=2)
    {
        for(x=k,b=0;x<=n;x*=3)b++;
        for(ll s=0;s<(1<<b);s++)if(flag[s])
        {
            dp[i][s]=0;
            for(ll p=0;p<(1<<last);p++)if(flag[p])
            if(!(s&p))dp[i][s]=(dp[i][s]+dp[i-1][p])%mo;
        }
        last=b;
    }
    for(ll s=0;s<(1<<last);s++)ans=(ans+dp[a][s])%mo;
    return ans;
}
int main()
{
    n=read();
    ll ans=1;
    for(ll s=0;s<S;s++)
    {
        flag[s]=true;
        for(ll i=0;i<=15;i++)
        if(((1<<i)&s)&&(1<<(i+1)&s))
        flag[s]=false;
    }
    for(ll i=1;i<=n;i++)
    if(i%2&&i%3)ans=(ans*solve(i))%mo;
    printf("%lld",ans);
    return 0;
}

转载于:https://www.cnblogs.com/Creed-qwq/p/10159691.html

根据引用\[1\]和引用\[2\]的描述,题目中的影魔拥有n个灵魂,每个灵魂有一个战斗力ki。对于任意一对灵魂对i,j (i<j),如果不存在ks (i<s<j)大于ki或者kj,则会为影魔提供p1的攻击力。另一种情况是,如果存在一个位置k,满足ki<c<kj或者kj<c<ki,则会为影魔提供p2的攻击力。其他情况下的灵魂对不会为影魔提供攻击力。 根据引用\[3\]的描述,我们可以从左到右进行枚举。对于情况1,当扫到r\[i\]时,更新l\[i\]的贡献。对于情况2.1,当扫到l\[i\]时,更新区间\[i+1,r\[i\]-1\]的贡献。对于情况2.2,当扫到r\[i\]时,更新区间\[l\[i\]+1,i-1\]的贡献。 因此,对于给定的区间\[l,r\],我们可以根据上述方法计算出区间内所有下标二元组i,j (l<=i<j<=r)的贡献之和。 #### 引用[.reference_title] - *1* *3* [P3722 [AH2017/HNOI2017]影魔(树状组)](https://blog.youkuaiyun.com/li_wen_zhuo/article/details/115446022)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [洛谷3722 AH2017/HNOI2017 影魔 线段树 单调栈](https://blog.youkuaiyun.com/forever_shi/article/details/119649910)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值