bzoj2734 [HNOI2012]集合选数(数学+状压dp)

妙啊。
我们考虑搞出这样一张表:

1 3 9 27 ...
2 6 18 54
4 12 36 108
8 24 72 216
.
.
.

相邻位置的数不能同时选即可,注意到最多只有11列,于是我们可以状压dp来计算方案数。我们注意到5,7…并没有出现在这张表中,于是记录已经算过的数,没算过的数就放在左上角生成这张表算一遍,把方案数乘起来即可。
复杂度大概 O ( n / l n n l o g 2 n 2 l o g 3 n )

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 100010
#define mod 1000000001
inline char gc(){
    static char buf[1<<16],*S,*T;
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,a[20][12],f[20][2048],b[20],bin[20];
bool vis[N];
inline void inc(int &x,int y){x+=y;x%=mod;}
inline ll calc(int x){
    a[1][1]=x;
    for(int i=2;i<=18;++i) a[i][1]=min(a[i-1][1]*2,n+1),vis[a[i][1]]=1;
    for(int i=1;i<=18;++i){
        for(int j=2;j<=11;++j) a[i][j]=min(a[i][j-1]*3,n+1),vis[a[i][j]]=1;
        int y=11;
        for(int j=1;j<=11;++j) if(a[i][j]>n){y=j-1;break;}
        b[i]=bin[y]-1;
        for(int s=0;s<=b[i];++s) f[i][s]=0;
    }f[0][0]=1;
    for(int i=0;i<18;++i)
        for(int s=0;s<=b[i];++s){
            if(!f[i][s]) continue;
            for(int ss=0;ss<=b[i+1];++ss){
                if((s&ss)!=0||(ss&(ss>>1))!=0) continue;
                inc(f[i+1][ss],f[i][s]);
            }
        }return f[18][0];
}
int main(){
//  freopen("a.in","r",stdin);
    n=read();ll ans=1;bin[0]=1;
    for(int i=1;i<=11;++i) bin[i]=bin[i-1]<<1;
    for(int i=1;i<=n;++i){
        if(vis[i]) continue;
        ans*=calc(i);ans%=mod;
    }printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/icefox_zhx/article/details/80493847