点击跳转
不重叠的时候就是个带有通配符的字符串匹配,很裸
但是当前缀和后缀重合的时候,情况就不太一样了
可以去想一想当年初学KMP的时候做的一类循环串题,最后会发现:我要检查 i i i是不是 b o r d e r border border,就等价于检验 n − i n-i n−i是不是循环节
检验的过程就用多项式卷积即可
#include
#include
#include
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
struct EasyMath
{
ll prime[maxn], phi[maxn], mu[maxn];
bool mark[maxn];
ll fastpow(ll a, ll b, ll c)
{
ll t(a%c), ans(1ll);
for(;b;b>>=1,t=t*t%c)if(b&1)ans=ans*t%c;
return ans;
}
void exgcd(ll a, ll b, ll &x, ll &y)
{
if(!b){x=1,y=0;return;}
ll xx, yy;
exgcd(b,a%b,xx,yy);
x=yy, y=xx-a/b*yy;
}
ll inv(ll x, ll p) //p是素数
{return fastpow(x%p,p-2,p);}
ll inv2(ll a, ll p)
{
ll x, y;
exgcd(a,p,x,y);
return (x+p)%p;
}
void shai(ll N)
{
ll i, j;
for(i=2;i<=N;i++)mark[i]=false;
*prime=0;
phi[1]=mu[1]=1;
for(i=2;i<=N;i++)
{
if(!mark[i])prime[++*prime]=i, mu[i]=-1, phi[i]=i-1;
for(j=1;j<=*prime and i*prime[j]<=N;j++)
{
mark[i*prime[j]]=true;
if(i%prime[j]==0)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
mu[i*prime[j]]=-mu[i];
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
}
ll CRT(vector<ll> a, vector<ll> m) //要求模数两两互质
{
ll M=1, ans=0, n=a.size(), i;
for(i=0;i<n;i++)M*=m[i];
for(i=0;i<n;i++)(ans+=a[i]*(M/m[i])%M*inv2(M/m[i],m[i]))%=M;
return ans;
}
}em;
#define mod 998244353ll
struct NTT
{
ll n;
vector<ll> R;
void init(ll bound) //bound是积多项式的最高次幂
{
ll L(0);
for(n=1;n<=bound;n<<=1,L++);
R.resize(n);
for(ll i=0;i<n;i++)R[i]=(R[i>>1]>>1)|((i&1)<<(L-1));
}
void ntt(vector<ll>& a, int opt)
{
ll i, j, k, wn, w, x, y, inv(em.fastpow(n,mod-2,mod));
for(i=0;i<n;i++)if(i>R[i])swap(a[i],a[R[i]]);
for(i=1;i<n;i<<=1)
{
if(opt==1)wn=em.fastpow(3,(mod-1)/(i<<1),mod);
else wn=em.fastpow(3,(mod-1-(mod-1)/(i<<1)),mod);
for(j=0;j<n;j+=i<<1)
for(w=1,k=0;k<i;k++,w=w*wn%mod)
{
x=a[k+j], y=a[k+j+i]*w%mod;
a[k+j]=(x+y)%mod, a[k+j+i]=(x-y)%mod;
}
}
if(opt==-1)for(i=0;i<n;i++)(a[i]*=inv)%=mod;
}
};
struct Poly
{
vector<ll> v;
ll n; //n是最高次项的次数
Poly(ll N){v.resize(N+1);n=N;}
Poly(const Poly& p){v=p.v; n=p.n;}
void resize(ll N){n=N; v.resize(N+1);}
ll& operator[](ll id){return v[id];}
void show()
{
printf("n=%lld\n",n);
ll i; rep(i,0,n-1)printf("%lldx^%lld + ",(v[i]+mod)%mod,i);
printf("%lldx^%lld\n",(v[n]+mod)%mod,n);
}
};
Poly operator+(Poly A, Poly B)
{
ll i;
Poly C(max(A.n,B.n));
A.resize(C.n), B.resize(C.n);
rep(i,0,C.n)C[i]=(A[i]+B[i])%mod;
return C;
}
Poly operator*(ll x, Poly A)
{
x%=mod;
ll i; rep(i,0,A.n)(A[i]*=x)%=mod;
return A;
}
Poly operator*(Poly A, Poly B)
{
NTT ntt;
ll i, n=A.n+B.n;
ntt.init(A.n+B.n);
A.resize(ntt.n-1), B.resize(ntt.n-1);
ntt.ntt(A.v,1), ntt.ntt(B.v,1);
Poly C(ntt.n-1);
rep(i,0,C.n)C[i]=(A[i]*B[i])%mod;
ntt.ntt(C.v,-1);
C.resize(n);
return C;
}
int main()
{
ios::sync_with_stdio(0);
string s; cin>>s;
ll n=s.length(), i, ans=0, j;
Poly A(n-1), B(n-1);
rep(i,0,n-1)
{
A[i] = (s[i]=='0');
B[i] = (s[n-1-i]=='1');
}
auto P = A*B;
rep(i,1,n-1)
{
bool flag = true;
auto L = n-i;
for(j = n%L ? n%L : L; j<=n; j+=L)
{
if(P[j-1] or P[2*n-1-j])flag=false;
}
if(flag)ans ^= i*i;
}
ans ^= n*n;
cout<<ans;
return 0;
}