bzoj4589: Hard Nim

传送门

不难看出就是求\(n\)个小于\(m\)的质数异或和为\(0\)的方案数,可以用\(FWT\)+快速幂解决
(我的代码跑了4500ms……不是很明白那几位52ms的巨巨是怎么做到的……可能是我人傻常数大……也不至于这么大吧……)

//minamoto
#include<cstdio>
#include<cstring>
#define R register
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
const int N=(1<<16)+5,P=1e9+7,inv=500000004;
inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int vis[N],m,n,k;
void init(){
	vis[1]=1;
	fp(i,2,50000)if(!vis[i]){
		for(R int j=(i<<1);j<=50000;j+=i)vis[j]=1;
	}
}
int A[N],B[N],lim;
void FWT(int *A,int ty){
	for(R int mid=1;mid<lim;mid<<=1)
		for(R int j=0;j<lim;j+=(mid<<1))	
			for(R int k=0;k<mid;++k){
				int x=A[j+k],y=A[j+k+mid];
				A[j+k]=add(x,y),A[j+k+mid]=dec(x,y);
				if(ty==-1)A[j+k]=mul(A[j+k],inv),A[j+k+mid]=mul(A[j+k+mid],inv);
			}
}
void ksm(int *A,int *B,int y){
	FWT(A,1),FWT(B,1);
	while(y){
		if(y&1)fp(i,0,lim-1)B[i]=mul(B[i],A[i]);
		fp(i,0,lim-1)A[i]=mul(A[i],A[i]);
		y>>=1;
	}FWT(B,-1);
}
int main(){
//	freopen("testdata.in","r",stdin);
	init();
	while(~scanf("%d%d",&n,&m)){
		lim=1;while(lim<=m)lim<<=1;
		memset(A,0,sizeof(A)),memset(B,0,sizeof(B));
		fp(i,1,m)A[i]=B[i]=!vis[i];
		ksm(A,B,n-1);printf("%d\n",B[0]);
	}return 0;
}
posted @ 2018-12-29 14:00  bztMinamoto  阅读(166)  评论(0编辑  收藏  举报
Live2D