萌新刚学状压DP求助
查看原帖
萌新刚学状压DP求助
195331
Mine_KingCattleya楼主2020/11/19 21:24

RT.
样例3都过不了,不知道哪里挂掉了,越调越离谱,求大佬帮忙看看

#include<cstdio>
#include<cstring>
#include<algorithm>
#define int long long
using namespace std;
const int N=(1<<8)-1;
int n;
long long mod,ans;
long long dp[N+5][N+5],g[N+5][N+5],h[N+5][N+5];
const int prime[]={0,2,3,5,7,11,13,17,19};
struct node
{
	int S,big,num;
	void make()
	{
		int now=num;
		for(int i=1;i<=8;i++)
			if(now%prime[i]==0)
			{
				S|=(1<<(i-1));
				while(now%prime[i]==0) now/=prime[i];
			}
		if(now!=1) big=num;
		return ;
	}
	friend bool operator<(node x,node y){return x.big<y.big;}
}a[505];
void cpy()
{
	for(int i=0;i<=N;i++)
		for(int j=0;j<=N;j++)
			dp[i][j]=((g[i][j]+h[i][j]-dp[i][j])%mod+mod)%mod;
	return ;
}
signed main()
{
	scanf("%lld%lld",&n,&mod);
	for(int i=2;i<=n;i++) a[i-1].num=i,a[i-1].make();
	sort(a+1,a+n);
	dp[0][0]=1;
	for(int i=1;i<n;i++)
	{
		if(i==1||!a[i].big||a[i].big!=a[i-1].big)
		{
			memcpy(g,dp,sizeof(g));
			memcpy(h,dp,sizeof(h));
		}
		for(int S1=N;S1>=0;S1--)
			for(int S2=N;S2>=0;S2--)
			{
				if(S1&S2) continue;
				if((S2&a[i].big)==0) (g[a[i].S|S1][S2]+=g[S1][S2])%=mod;
				if((S1&a[i].big)==0) (h[S1][a[i].S|S2]+=h[S1][S2])%=mod;
			}
		if(i==n-1||!a[i].big||a[i].big!=a[i+1].big) cpy();
	}
	for(int S1=0;S1<=N;S1++)
		for(int S2=0;S2<=N;S2++)
		{
			if(S1&S2) continue;
			if(!dp[S1][S2]) continue;
			ans=(ans+dp[S1][S2])%mod;
		}
	printf("%lld\n",ans);
	return 0;
}
2020/11/19 21:24
加载中...