【Codeforces 623E】Transforming Sequence(MTT)

传送门

f[i][j]f[i][j]表示ii个数,jj位有11时的方案数
那么有
fa,ifb,j(i+ji)2bi>fa+b,i+jf_{a,i}*f_{b,j}*{i+j\choose i}2^{bi}->f_{a+b,i+j}
于是倍增dp,fftdp,fft优化
要写mttmtt

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
struct plx{
	double x,y;
	plx(double _x=0,double _y=0):x(_x),y(_y){}
	friend inline plx operator +(cs plx &a,cs plx &b){
		return plx(a.x+b.x,a.y+b.y);
	}
	friend inline plx operator -(cs plx &a, cs plx &b){
		return plx(a.x-b.x,a.y-b.y);
	}
	friend inline plx operator *(cs plx &a,cs plx &b){
		return plx(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
	}
	friend inline plx operator /(cs plx &a,cs int &b){
		return plx(a.x/b,a.y/b);
	}
	inline plx conj(){return plx(x,-y);}
	inline void operator /=(cs int &b){
		*this=*this/b;
	}
};
cs int N=30005,C=16,M=(1<<15)-1,L=(1<<C)+1;
int fac[N],ifac[N];
inline void init_inv(cs int len=N-5){
	fac[0]=ifac[0]=1;
	for(int i=1;i<=len;i++)fac[i]=mul(fac[i-1],i);
	ifac[len]=Inv(fac[len]);
	for(int i=len-1;i;i--)ifac[i]=mul(ifac[i+1],i+1);
}
inline int c(int n,int m){return n<m?0:mul(fac[n],mul(ifac[m],ifac[n-m]));}
cs double pi=acos(-1);
int rev[L],lim;
ll n;int k;
inline void init_rev(int lim){
	for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>1));
}
plx *w[C+1];
inline void init_w(){
	for(int i=1;i<=C;i++)w[i]=new plx[1<<(i-1)];
	for(int i=0,l=(1<<(C-1));i<l;i++)w[C][i]=plx(cos(pi*i/l),sin(pi*i/l));
	for(int i=C-1;i;i--)
	for(int j=0,l=1<<(i-1);j<l;j++)w[i][j]=w[i+1][j<<1];
}
inline void fft(plx *f,int lim,int kd){
	for(int i=0;i<lim;i++)if(i>rev[i])swap(f[i],f[rev[i]]);
	plx a0,a1;
	for(int mid=1,l=1;mid<lim;mid<<=1,l++)
	for(int i=0;i<lim;i+=mid<<1)
	for(int j=0;j<mid;j++)
	a0=f[i+j],a1=f[i+j+mid]*w[l][j],f[i+j]=a0+a1,f[i+j+mid]=a0-a1;
	if(kd==-1){
		reverse(f+1,f+lim);
		for(int i=0;i<lim;i++)f[i]/=lim;
	}
}
inline void mul(int *A,int *B,int *ret){
	static plx a[L],b[L],c[L],d[L],da,db,dc,dd;
	for(int i=0;i<=k;i++)a[i]=plx(A[i]&M,A[i]>>15),b[i]=plx(B[i]&M,B[i]>>15);
	for(int i=k+1;i<=lim;i++)a[i]=b[i]=plx();
	fft(a,lim,1),fft(b,lim,1);
	for(int i=0;i<lim;i++){
		int j=(lim-i)&(lim-1);
		da=(a[i]+a[j].conj())*plx(0.5,0);
		db=(a[j].conj()-a[i])*plx(0,0.5);
		dc=(b[i]+b[j].conj())*plx(0.5,0);
		dd=(b[j].conj()-b[i])*plx(0,0.5);
		c[i]=(da*dc)+((da*dd)*plx(0,1));
		d[i]=(db*dd)+((db*dc)*plx(0,1));
	}
	fft(c,lim,-1),fft(d,lim,-1);
	for(int i=0;i<=k;i++){
		ll da=(ll)(d[i].x+0.5)%mod,db=(ll)(d[i].y+0.5)%mod,dc=(ll)(c[i].y+0.5)%mod,dd=(ll)(c[i].x+0.5)%mod;
		ret[i]=((da<<30)+((db+dc)<<15)+dd)%mod;
	}
}
int tmp[N];
inline void init(int *a,int ii){
	for(int i=0;i<=k;i++)tmp[i]=mul(a[i],ksm(2,1ll*i*ii%(mod-1)));
}
int a[N],res[N];
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	cin>>n>>k;
	if(n>k)return puts("0"),0;
	init_w(),init_inv();
	for(int i=1;i<=k;i++)res[i]=a[i]=ifac[i];
	lim=1,n--;
	while(lim<(k+1)*2)lim<<=1;init_rev(lim);
	for(int i=1;n;init(a,i),mul(tmp,a,a),i<<=1,n>>=1)if(n&1){
		init(res,i),mul(tmp,a,res);
	}
	for(int i=0;i<=k;i++)Mul(res[i],fac[i]);
	int ret=0;
	for(int i=0;i<=k;i++)Add(ret,mul(c(k,i),res[i]));
	cout<<ret;
}
posted @ 2020-02-04 19:34  Stargazer_cykoi  阅读(121)  评论(0编辑  收藏  举报