[NOI2015] 寿司晚宴 题解
转化原题:
30pts
对于
时间复杂度
100pts
对于
发现性质
按上述三种情况分类分别统计即可
若处理
对于
从而
时间复杂度
其实可以优化到
代码(码风有些个性化,大家看情况参考):
#include<bits/stdc++.h>
using namespace std;
#define O(x) cerr<<#x<<":"<<x<<endl
#define For(i, l, r) for(int i=(l);i<=(r);++i)
#define Rfor(i, r, l) for(int i=(r);i>=(l);--i)
int read(){int x=0,t=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')t=-t;ch=getchar();}while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();return x*t;}
int p;
int add(int x,int y){return x+y<p?x+y:x+y-p;}
int dec(int x,int y){return x-y<0?x-y+p:x-y;}
const int N=256,M=500,B=22;
bool isprime[M+1];bool check(int x){for(int i=2;i*i<=x;++i)if(x%i==0)return 0;return 1;}
int inc[M+1];
struct dynamics
{
int f[N][N];
void clear(){memset(f,0,sizeof(f));};
void show(){For(i, 0, 3)For(j,0,3)cout<<i<<" "<<j<<" "<<f[i][j]<<" ";cout<<endl<<endl;}
}f,t1,t2,tmp;
void update(dynamics &x,int k,bool dir)
{
int k_=k;k=0;
for(int i=2,t=0;i<=B;++i)if(isprime[i]){if(k_%i==0)k|=1<<t;++t;}
For(i, 0, N-1)
For(j, 0, N-1) tmp.f[i][j]=x.f[i][j];
if(dir)
{
For(i, 0, N-1)
For(j, 0, N-1)
if(!(i&k))x.f[i][j|k]=add(x.f[i][j|k],tmp.f[i][j]);
}
else
{
For(i, 0, N-1)
For(j, 0, N-1)
if(!(j&k))x.f[i|k][j]=add(x.f[i|k][j],tmp.f[i][j]);
}
}
int main()
{
int n=read();p=read();
f.f[0][0]=1;
For(i, 2, n) isprime[i]=check(i);
For(i, 2, n) Rfor(j, i, 2) if(i%j==0&&isprime[j]){inc[i]=j;break;}
// f.show();
For(i, 2, n) if(inc[i]<B) update(f,i,0), update(f,i,1);//f.show();
For(i, B, n)
if(isprime[i])
{
t1=f;t2=f;
For(j, 1, n/i)
update(t1,j,0),update(t2,j,1);
For(j, 0, N-1)
For(k, 0, N-1)tmp.f[j][k]=f.f[j][k];
For(j, 0, N-1)
For(k, 0, N-1)
f.f[j][k]=dec(add(t1.f[j][k],t2.f[j][k]),tmp.f[j][k]);
// f.show();
}
int ans=0;
For(i, 0, N-1)
For(j, 0, N-1) ans=add(ans,f.f[i][j]);
printf("%d\n",ans);
}