CF453D Little Pony and Elements of Harmony

机房的同学都把这题秒啦
我还是太菜啦
题目链接

题目大意

有一个数组\(f\)和一个数组\(b\),每次操作,\(f[i]\)会变成\(\sum_{j=0}^nb[popcount(i\oplus j)]*f[j]\)
\(t\)次操作之后的\(f\),对\(P\)取模.
\(n\leq 2^{20},t\leq 10^{18},b\leq 20\)


解析

\(c[i]=b[popcount(i)]\)
\(f[i]=\sum_{j=0}^nc[i\oplus j]*f[j]\)
考虑到\(i\oplus j\oplus j=i\),因此\(f[i]=\sum_{j\oplus k=i}c[j]*f[k]\).
我们很愉快的发现这就是一个\(FWT\)的形式.
因此只要把\(c\)先做一次\(FWT\),然后对每个数快速幂一下(\(t\)次方),然后再把\(f\)做一次\(FWT\),最后乘起来然后\(IFWT\)即可.

考虑到这个\(P\)可能没有模\(n\)的乘法逆元,因此把\(P\)乘上\(n\),然后\(IFWT\)的时候直接除以\(n\)即可.乘的时候注意\(O(1)\)快速乘.

代码如下

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define N (1100010)
#define inf (0x7f7f7f7f)
#define rg register int
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand())<<(15))^(rand()))
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
inline char read(){
	static const int IN_LEN=1000000;
	static char buf[IN_LEN],*s,*t;
	return (s==t?t=(s=buf)+fread(buf,1,IN_LEN,stdin),(s==t?-1:*s++):*s++);
}
template<class T>
inline void read(T &x){
	static bool iosig;
	static char c;
	for(iosig=false,c=read();!isdigit(c);c=read()){
		if(c=='-')iosig=true;
		if(c==-1)return;
	}
	for(x=0;isdigit(c);c=read())x=((x+(x<<2))<<1)+(c^'0');
	if(iosig)x=-x;
}
inline char readchar(){
	static char c;
	for(c=read();!isalpha(c);c=read())
	if(c==-1)return 0;
	return c;
}
const int OUT_LEN = 10000000;
char obuf[OUT_LEN],*ooh=obuf;
inline void print(char c) {
	if(ooh==obuf+OUT_LEN)fwrite(obuf,1,OUT_LEN,stdout),ooh=obuf;
	*ooh++=c;
}
template<class T>
inline void print(T x){
	static int buf[30],cnt;
	if(x==0)print('0');
	else{
		if(x<0)print('-'),x=-x;
		for(cnt=0;x;x/=10)buf[++cnt]=x%10+48;
		while(cnt)print((char)buf[cnt--]);
	}
}
inline void flush(){fwrite(obuf,1,ooh-obuf,stdout);}
LL e[N],b[N],c[N],P,t;
int n,m;
int popcount(int x){
	int ans=0;
	while(x)x&=(x-1),ans++;
	return ans; 
}
LL mult(LL x,LL y,LL mod){
	LL tmp=(x*y-(LL)((ld)x/mod*y+1.0e-8)*mod);
	return tmp<0?tmp+mod:tmp;
}
LL FWT(LL *a,int tp){
	for(int i=1;i<n;i<<=1)
	for(int R=i<<1,j=0;j<n;j+=R)
	for(int k=j;k<j+i;k++){
		LL x=a[k],y=a[k+i];
		a[k]=(x+y)%P,a[k+i]=(x-y+P)%P;
	}
	if(tp==-1)
	for(int i=0;i<n;i++)a[i]/=n;
}
LL ksm(LL a,LL p){
	LL res=1;
	while(p){
		if(p&1)res=mult(res,a,P);
		a=mult(a,a,P),p>>=1;
	}
	return res;
}
int main(){
	read(m),n=(1<<m),read(t),read(P),P*=n;
	for(int i=0;i<n;i++)read(e[i]);
	for(int i=0;i<=m;i++)read(b[i]);
	for(int i=0;i<n;i++)c[i]=b[popcount(i)];
	FWT(e,1),FWT(c,1);
	for(int i=0;i<n;i++)c[i]=ksm(c[i],t);
	for(int i=0;i<n;i++)e[i]=mult(e[i],c[i],P);
	FWT(e,-1);
	for(int i=0;i<n;i++)
	print(e[i]),ent;
	return flush(),0;
}
posted @ 2018-12-04 15:05  Romeolong  阅读(327)  评论(1编辑  收藏  举报