## bzoj 3160 万径人踪灭——FFT

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
#define db double
using namespace std;
const int N=1e5+5,M=N<<2,mod=1e9+7;
const db pi=acos(-1);
int n,r[M],len,ans,bin[N];
char ch[N],tc[N<<1];
struct cpl{db x,y;}a[M],b[M],I;
cpl operator+ (cpl a,cpl b){return (cpl){a.x+b.x,a.y+b.y};}
cpl operator- (cpl a,cpl b){return (cpl){a.x-b.x,a.y-b.y};}
cpl operator* (cpl a,cpl b){return (cpl){a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x};}
void upd(int &x){x>=mod?x-=mod:0;x<0?x+=mod:0;}
void fft(cpl *a,bool fx)
{
for(int i=0;i<len;i++)
if(i<r[i])swap(a[i],a[r[i]]);
for(int R=2;R<=len;R<<=1)//<<=1
{
int m=R>>1;
cpl Wn=(cpl){ cos(pi/m),fx?-sin(pi/m):sin(pi/m) };
for(int i=0;i<len;i+=R)
{
cpl w=I;
for(int j=0;j<m;j++,w=w*Wn)//w=w*Wn
{
cpl tmp=w*a[i+m+j];
a[i+m+j]=a[i+j]-tmp;
a[i+j]=a[i+j]+tmp;
}
}
}
}
void manachar()
{
int m=(n<<1)-1;
for(int i=0,j=0;j<n;i+=2,j++)
tc[i]=ch[j],tc[i+1]=',';
int id=0;
r[0]=0;ans--;
for(int i=1;i<m;i++)
{
r[i]=0;
if(id+r[id]>i)
{
r[i]=min(id+r[id]-i,r[(id<<1)-i]);
}
if(id+r[id]<=i+r[i])
{
for(;i+r[i]<m&&i-r[i]>=0&&tc[i+r[i]]==tc[i-r[i]];r[i]++);
r[i]--; id=i;
}
int tmp=(r[i]+(tc[i]==','))>>1;//don't change r[i]!!!!!!
ans-=tmp+(tc[i]!=','); upd(ans);
}
}
int main()
{
I.x=1;
scanf("%s",ch);n=strlen(ch);
bin[0]=1;
for(int i=1,m=n+1>>1;i<=m;i++)bin[i]=bin[i-1]<<1,upd(bin[i]);
len=1;
for(;len<=n<<1;len<<=1);
for(int i=0;i<len;i++) r[i]=(r[i>>1]>>1)+((i&1)?len>>1:0);
for(int i=0;i<n;i++)
if(ch[i]=='a')a[i].x=1;
fft(a,0);
for(int i=0;i<len;i++)a[i]=a[i]*a[i];
fft(a,1);
for(int i=0;i<n;i++)
if(ch[i]=='b')b[i].x=1;
fft(b,0);
for(int i=0;i<len;i++)b[i]=b[i]*b[i];
fft(b,1);
for(int i=0;i<len;i++)//len
{
a[i].x=(int(a[i].x/len+0.5)+1)>>1;
b[i].x=(int(b[i].x/len+0.5)+1)>>1;
a[i].x+=b[i].x;
ans+=bin[(int)a[i].x]-1; upd(ans);
}
manachar();
printf("%d\n",ans);
return 0;
}

posted on 2018-11-26 19:59  Narh  阅读(140)  评论(0编辑  收藏  举报