【bzoj3160】万径人踪灭 FFT
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3160
我是一个傻叉 微笑脸
1 #include<bits/stdc++.h> 2 #define inf 1000000000 3 #define ll long long 4 #define N 200005 5 #define mod 1000000007 6 using namespace std; 7 int read(){ 8 int x=0,f=1;char ch=getchar(); 9 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 10 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 11 return x*f; 12 } 13 struct CD{ 14 double x,y; 15 CD(double a=0,double b=0){x=a;y=b;} 16 friend CD operator + (CD n1,CD n2){return CD(n1.x+n2.x,n1.y+n2.y);} 17 friend CD operator - (CD n1,CD n2){return CD(n1.x-n2.x,n1.y-n2.y);} 18 friend CD operator * (CD n1,CD n2){return CD(n1.x*n2.x-n1.y*n2.y,n1.x*n2.y+n1.y*n2.x);} 19 }; 20 const double Pi=acos(-1.0); 21 int bit,n,nn,ans,m,mx,id,p[N<<1]; 22 char s[N],st[N<<1]; 23 CD a[N<<1],b[N<<1]; 24 void FFT(CD *a,int n,int type){ 25 for(int i=0,j=0;i<n;i++) { 26 if(j>i)swap(a[i],a[j]); 27 int k=n; 28 while(j&(k >>= 1))j&=~k; 29 j|=k; 30 } 31 for(int i=1;i<=bit;i++){ 32 CD w_n(cos(2*type*Pi/(1<<i)),sin(2*type*Pi/(1<<i))); 33 for(int j=0;j<n;j+=(1<<i)){ 34 CD w(1,0); 35 for(int k=j;k<j+(1<<(i-1));k++){ 36 CD tmp=a[k],tt=w*a[k+(1<<(i-1))]; 37 a[k]=tmp+tt; 38 a[k+(1<<(i-1))]=tmp-tt; 39 w=w*w_n; 40 } 41 } 42 } 43 if(type<0)for(int i=0;i<n;i++)a[i].x/=n; 44 } 45 int main(){ 46 scanf("%s",s); 47 n=strlen(s);nn=n; 48 bit=1; 49 while((1<<bit)<(n<<1))bit++; 50 n=1<<bit; 51 for(int i=nn;i<n;i++)a[i]=b[i]=CD(0,0); 52 for(int i=0;i<nn;i++)if(s[i]=='a')a[i]=CD(1,0); 53 FFT(a,n,1); 54 for(int i=0;i<n;i++)a[i]=a[i]*a[i]; 55 FFT(a,n,-1); 56 57 for(int i=0;i<nn;i++)if(s[i]=='b')b[i]=CD(1,0); 58 FFT(b,n,1); 59 for(int i=0;i<n;i++)b[i]=b[i]*b[i]; 60 FFT(b,n,-1); 61 62 for(int i=0;i<n;i++){ 63 int x=round(a[i].x+b[i].x); 64 x=(x+1)/2; 65 ans=(ans+(1<<x)-1)%mod; 66 } 67 68 m=nn<<1|1; 69 for(int i=0;i<nn;i++)st[(i+1)<<1]=s[i]; 70 mx=id=0; 71 for(int i=1;i<=m;i++){ 72 if(mx>i)p[i]=min(p[2*id-i],mx-i);else p[i]=0; 73 while(i-p[i]-1>0&&i+p[i]+1<=m&&st[i-p[i]-1]==st[i+p[i]+1])p[i]++; 74 if(i+p[i]>mx)mx=i+p[i],id=i; 75 ans=((ans-(p[i]+1)/2)%mod+mod)%mod; 76 } 77 printf("%d\n",ans); 78 return 0; 79 }
posted on 2016-06-01 21:36 wjyi 阅读( ...) 评论( ...) 编辑 收藏