[CQOI2018]九连环
传送门
Solution
\[ans=\left \lfloor \frac{2^{n+1}}{3} \right \rfloor \]我为什么要写FFT啊~
#include<bits/stdc++.h>
#define ll long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}
#define MN 16384
const double Pi=std::acos(-1.);
struct complex
{
double x,y;
complex(double x=0,double y=0):x(x),y(y){}
inline complex operator+(const complex& o)const{return complex(x+o.x,y+o.y);}
inline complex operator-(const complex& o)const{return complex(x-o.x,y-o.y);}
inline complex operator*(const complex& o)const{return complex(x*o.x-y*o.y,x*o.y+y*o.x);}
inline void swap(complex& o){register complex t=o;o=(*this);*this=t;}
}a[MN],b[MN];
int N=16384,di=14,pos[MN];
inline void FFT(complex *a,int type)
{
register int i,j,p,k;
for(i=0;i<N;++i)if(i<pos[i])a[i].swap(a[pos[i]]);
for(i=1;i<N;i<<=1)
{
complex wn(cos(Pi/i),type*sin(Pi/i));
for(p=i<<1,j=0;j<N;j+=p)
{
complex w(1,0);
for(k=0;k<i;++k,w=w*wn)
{
complex X=a[j+k],Y=w*a[j+i+k];
a[j+k]=X+Y;a[j+i+k]=X-Y;
}
}
}
}
inline void dig(complex *a)
{
register int i;
for(i=0;i<N;++i)
{
int o=(int)(a[i].x/1000.);
a[i+1].x+=(double)o;
a[i].x-=(double)o*1000.;
}
}
void print(complex *a)
{
bool fl=0;
for(int i=N-1;~i;--i)if(fl||(int)(a[i].x)>0) fl=1,printf("%03d",(int)a[i].x);
puts("");
}
inline void sqr(complex *a)
{
FFT(a,1);
register int i;
for(i=0;i<N;++i) a[i]=a[i]*a[i];
FFT(a,-1);
for(i=0;i<N;++i) a[i].x=(int)(a[i].x/N+.5),a[i].y=0;
dig(a);
}
inline void pro(complex *a,complex *b)
{
FFT(a,1);FFT(b,1);
register int i;
for(i=0;i<N;++i) a[i]=a[i]*b[i];
FFT(a,-1);FFT(b,-1);
for(i=0;i<N;++i) a[i].x=(int)(a[i].x/N+.5),a[i].y=0;
for(i=0;i<N;++i) b[i].x=(int)(b[i].x/N+.5),b[i].y=0;
dig(a);dig(b);
}
inline void fpow(int m)
{
register int i;
bool fl=0;
for(i=0;i<N;++i) a[i].x=a[i].y=b[i].x=b[i].y=0.;
a[0].x=1.;b[0].x=2.;
for(;m;m>>=1,sqr(b)) if(m&1) pro(a,b);
}
int main()
{
register int i,m=read();
for(i=0;i<N;++i)
pos[i]=(pos[i>>1]>>1)|((i&1)<<(di-1));
while(m--)
{
fpow(read()+1);
bool fl=0;dig(a);
for(i=N-1;~i;--i)
{
int o=(int)(a[i].x/3.);
if(!fl&&o>0) fl=1,printf("%d",o);
else if(fl) printf("%03d",o);
if(i) a[i-1].x+=(double)(a[i].x-3.*o)*1000.;
}
puts("");
}
return 0;
}
Blog来自PaperCloud,未经允许,请勿转载,TKS!
致虚极,守静笃,万物并作,吾以观其复