# bzoj3160 万径人踪灭

http://www.elijahqi.win/archives/3096

Description

Input

Output

Sample Input

Sample Output

HINT

Source

2013湖北互测week1

$f\left[k\right]=\sum _{i}^{不越界}\left[k-i\phantom{\rule{0.667em}{0ex}}\mathrm{mod}\phantom{\rule{thinmathspace}{0ex}}\phantom{\rule{thinmathspace}{0ex}}2=0\right]\left[{s}_{k-i}{=}^{\prime }{a}^{\prime }\right]\left[{s}_{k+i}{=}^{\prime }{a}^{\prime }\right]$$f[k]=\sum\limits_{i}^{不越界}[k-i\mod 2=0][s_{k-i}='a'][s_{k+i}='a']$ $\sum _{t}\sum _{i}^{不越界}\left[k-i=2×t\right]\left[{s}_{\frac{k-i}{2}}{=}^{\prime }{a}^{\prime }\right]\left[{s}_{\frac{k+i}{2}}{=}^{\prime }{a}^{\prime }\right]$$\sum\limits_{t}\sum\limits_{i}^{不越界}[k-i=2\times t][s_{\frac{k-i}{2}}='a'][s_{\frac{k+i}{2}}='a']$ $\sum _{t}\sum _{i}^{不越界}\left[k-i=2×t\right]{s}_{t}\ast {s}_{k-t}$$\sum\limits_{t}\sum\limits_{i}^{不越界}[k-i=2\times t]s_{t}*s_{k-t}$ $\sum _{t}\left[k-2\ast t>=0\right]{s}_{t}\ast {s}_{k-t}$$\sum\limits_{t}[k-2*t>=0]s_{t}*s_{k-t}$ $\sum _{t=0}^{⌊\frac{k}{2}⌋}{s}_{t}\ast {s}_{k-t}$$\sum\limits_{t=0}^{\lfloor\frac{k}{2}\rfloor}s_{t}*s_{k-t}$ s即表示该串这个位置是否为a|b为的话就是1

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define pi acos(-1)
#define ll long long
using namespace std;
const int mod=1000000007;
const int N=100020;
struct C{
double a,b;
inline friend C operator +(const C &x,const C &y){return (C){x.a+y.a,x.b+y.b};}
inline friend C operator -(const C &x,const C &y){return (C){x.a-y.a,x.b-y.b};}
inline friend C operator *(const C &x,const C &y){return (C){x.a*y.a-x.b*y.b,x.a*y.b+x.b*y.a};}
inline void operator *=(const C &y){*this=*this*y;}
}a[N<<2],b[N<<2];
int n,m,R[N<<2],p[N<<1];ll ans;
inline void fft(C *x,int f){
for (int i=0;i<n;++i) if (i<R[i]) swap(x[i],x[R[i]]);
for (int i=1;i<n;i<<=1){
C wn=(C){cos(pi/i),f*sin(pi/i)};
for (int j=0;j<n;j+=i<<1){
C w=(C){1,0},t1,t2;
for (int k=0;k<i;++k,w*=wn)
t1=x[j+k],t2=x[j+k+i]*w,x[j+k]=t1+t2,x[j+k+i]=t1-t2;
}
}if (f==-1) for (int i=0;i<n;++i) x[i].a/=n;
}
inline int ksm(ll b,int t){static ll tmp;tmp=1;
for (;t;b=b*b%mod,t>>=1) if (t&1) tmp=tmp*b%mod;return tmp;
}char s[N],s1[N<<1];
template<class T>
template<class T>
inline void dec(T &x,int v){x=x-v<0?x-v+mod:x-v;}
int main(){
freopen("bzoj3160.in","r",stdin);
scanf("%s",s+1);m=strlen(s+1);m<<=1;
int t=0;for (n=1;n<=m;n<<=1,++t);m>>=1;
for (int i=0;i<n;++i) R[i]=(R[i>>1]>>1)|((i&1)<<t-1);
for (int i=1;i<=m;++i) a[i].a=s[i]=='a',b[i].a=s[i]=='b';
fft(a,1);
//  for (int i=0;i<n;++i) printf("%f\n",a[i].a);
fft(b,1);for (int i=0;i<n;++i) a[i]=a[i]*a[i],b[i]=b[i]*b[i],a[i]=a[i]+b[i];
fft(a,-1);for (int i=1;i<=m;++i) s1[i*2-1]='#',s1[i<<1]=s[i];m<<=1;int mx=0,id=0;
//  for (int i=1;i<=m;++i) printf("%lf\n",a[i].a);
//  for (int i=2;i<=m;++i) printf("%d\n",(int)(a[i].a+0.1)>>1);
for (int i=1;i<=m;++i){
if (i<mx) p[i]=min(mx-i,p[(id<<1)-i]);else p[i]=1;
while(i-p[i]>0&&i+p[i]<=m&&s1[i-p[i]]==s1[i+p[i]]) ++p[i];
if(i+p[i]>mx) mx=i+p[i],id=i;dec(ans,p[i]>>1);
}
//  for (int i=1;i<=m;++i) printf("%d ",p[i]);
printf("%lld\n",ans);
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120