ccz181078

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::

Description

为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴。小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴。

在晚宴上,主办方为大家提供了 n−1 种不同的寿司,编号 1,2,3,…,n−1,其中第 i 种寿司的美味度为 i+1 (即寿司的美味度为从 2 到 n)。
现在小 G 和小 W 希望每人选一些寿司种类来品尝,他们规定一种品尝方案为不和谐的当且仅当:小 G 品尝的寿司种类中存在一种美味度为 x 的寿司,小 W 品尝的寿司中存在一种美味度为 y 的寿司,而 x 与 y 不互质。
现在小 G 和小 W 希望统计一共有多少种和谐的品尝寿司的方案(对给定的正整数 p 取模)。注意一个人可以不吃任何寿司。

Input

输入文件的第 1 行包含 2 个正整数 n,p,中间用单个空格隔开,表示共有 n 种寿司,最终和谐的方案数要对 p 取模。

Output

输出一行包含 1 个整数,表示所求的方案模 p 的结果。

对每个数分解质因数,小于20的质因数压位表示,大于20的质因数最多只有一个,将大于20的质因数相同的数集中起来处理

f[i][j]表示第一人取的数质因数状态为i,第二人取的数质因数状态为j的方案数

#include<cstdio>
#include<cstring>
#include<algorithm>
int ps[]={2,3,5,7,11,13,17,19};
struct num{
    int x,y;
}v[512];
bool operator<(num a,num b){
    return a.y<b.y;
}
long long f[256][256],g[256][256],h[256][256],p;
int n;
void inc(long long&a,long long b){
    a+=b;
    while(a>=p)a-=p;
    while(a<0)a+=p;
}
int nx[256][257];
int main(){
    for(int a=0;a<256;a++){
        for(int b=0;b<256;){
            int c=b+1;
            while(c&a)++c;
            nx[a][b]=c;
            b=c;
        }
    }
    scanf("%d%lld",&n,&p);
    for(int i=2;i<=n;i++){
        int x=i;
        for(int j=0;j<8;j++){
            if(!(x%ps[j])){
                v[i].x|=1<<j;
                do x/=ps[j];while(!(x%ps[j]));
            }
        }
        v[i].y=x;
    }
    std::sort(v+2,v+n+1);
    f[0][0]=1;
    int i=2;
    for(;v[i].y==1;i++){
        int x=v[i].x;
        memcpy(g,f,sizeof(f));
        for(int a=0;a<256;a++){
            for(int b=0;b<256;b=nx[a][b]){
                if(!(b&x))inc(g[a|x][b],f[a][b]);
                if(!(a&x))inc(g[a][b|x],f[a][b]);
            }
        }
        memcpy(f,g,sizeof(g));
    }
    for(int p=i;v[i].y;){
        memcpy(g,f,sizeof(f));
        memcpy(h,f,sizeof(f));
        while(v[p].y==v[i].y)++p;
        for(;i<p;i++){
            int x=v[i].x;
            for(int a=255;~a;a--){
                for(int b=0;b<256;b=nx[a][b]){
                    if(!(b&x))inc(g[a|x][b],g[a][b]);
                }
            }
            for(int a=255;~a;a--){
                for(int b=0;b<256;b=nx[a][b]){
                    if(!(b&x))inc(h[b][a|x],h[b][a]);
                }
            }
        }
        for(int a=0;a<256;a++){
            for(int b=0;b<256;b=nx[a][b]){
                if(a&b)continue;
                f[a][b]=-f[a][b];
                inc(f[a][b],g[a][b]);
                inc(f[a][b],h[a][b]);
            }
        }
    }
    long long ans=0;
    for(int a=0;a<256;a++){
        for(int b=0;b<256;b++){
            inc(ans,f[a][b]);
        }
    }
    printf("%lld\n",ans);
    return 0;
}

 

posted on 2016-05-26 21:58  nul  阅读(568)  评论(0编辑  收藏  举报