bzoj 4178: A

传送门:http://www.lydsy.com:808/JudgeOnline/problem.php?id=4178

比较裸的NTT

NTT讲的比较好的博客 http://blog.csdn.net/acdreamers/article/details/39026505

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
const int maxn=33000;
const int mod=(453<<21)+1,G=7,inv_G=135715694;
using namespace std;
int n,m,k,N,inv_N,rev[maxn];

int qpow(int a,int b){
	int res=1;
	for (;b;b>>=1,a=1ll*a*a%mod)
		if (b&1) res=1ll*res*a%mod;
	return res;
}

int rever(int x){
	int len=N,res=0;
	while (len--) res<<=1,res^=(x&1),x>>=1;
	return res;
}

struct DFT{
	int a[maxn];
	void ntt(int op){
  		for (int i=0;i<N;i++) if (rev[i]>i) swap(a[rev[i]],a[i]);
		int g=op==1?G:inv_G;
		for (int sz=2;sz<=N;sz<<=1){
			int t=qpow(g,(mod-1)/sz);
			for (int bg=0;bg<N;bg+=sz){
				for (int po=bg,w=1;po<bg+(sz>>1);po++){
					int x=a[po],y=1LL*a[po+(sz>>1)]*w%mod;
					a[po]=(x+y)%mod,a[po+(sz>>1)]=(x-y+mod)%mod;
					w=1ll*w*t%mod;
				}
			}
		}
		if (op==-1) for (int i=0;i<N;++i) a[i]=1LL*a[i]*inv_N%mod;
	}
}a,f;

void qpow(DFT f,DFT a){
	for (;k;k>>=1){
		a.ntt(1);
		if (k&1){
			f.ntt(1); for (int i=0;i<N;i++) f.a[i]=1ll*f.a[i]*a.a[i]%mod;
			f.ntt(-1); for (int i=n;i<N;i++) f.a[i]=0;
		}
		for (int i=0;i<N;i++) a.a[i]=1ll*a.a[i]*a.a[i]%mod; a.ntt(-1);
		for (int i=n;i<N;i++) a.a[i]=0;
	}
	for (int i=0;i<n;i++) printf("%d ",f.a[i]);puts("");
}

int main(){
	//printf("%d",qpow(12340918,91028347));
 	scanf("%d%d%d",&n,&m,&k);
	for (int i=0;i<n;i++) scanf("%d",&f.a[i]),f.a[i]%=mod;
	for (int i=1,x;i<=m;i++) scanf("%d",&x),a.a[x]++;
	N=(int)ceil(log2(n))+1;
	for (int i=0;i<(1<<N);i++) rev[i]=rever(i);
	//for (int i=0;i<(1<<N);i++) printf("%d %d\n",i,rev[i]);
	N=1<<N,inv_N=qpow(N,mod-2),qpow(f,a);
	
	return 0;
}


posted @ 2015-07-24 12:04  orzpps  阅读(177)  评论(0编辑  收藏  举报