Here are the official tutorial:
A2. Medium
Let us first find a method to check whether a shift by a fixed number
yields a solution or not. For the sake of simplicity let us work with the case of
, let also
. We can start by writing a set of equations
(all modulo
)
Note that for
to be solution to the shift equation, these equations are necessary to be satisfied, but of course it is not yet sufficient. Also it is easy to see that the
first equations can be always satisfied, and that by taking the sum of all these equations we obtain
which means that
should be even. This can be easily seen to be a necessary and sufficient condition for
to have a solution
. Similarly we can write an analogous system for bits
which is solvable if
is even. Finally we obtain that there is a solution x to the
-xor-shift equation.
Note also that the set of equations corresponding to the value
would be exactly the same as above!This observation is quite easy to generalize for all values of
and
. More precisely, whenever
then one can write
equations for
that determine whether the xor-shift equations has a
solution for
(and equivalently for
).
The complete solution is: first precompute the answer for every
being a divisor of
, then for every other
, compute
to reduce to one of the precomputed cases. The complexity is
where
is the number of divisors of
.
就是分层数1的个数,必须每一层都有偶数个1才合法。
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int f[N],n,a[N];
char ch[N];
int main()
{
scanf("%d",&n);
int t=1;a[1]=1;
for(int i=2;i<=sqrt(n);i++)
if(n%i==0)a[++t]=i,a[++t]=n/i;
for(int i=1;i<n;i++)f[i]=1;
scanf("%s",ch);
for(int i=1;i<=t;i++)
for(int j=0;j<a[i];j++)
{
int sum=0;
for(int k=j;k<n;k+=a[i])sum+=ch[k]-'0';
if(sum%2!=0)
{
f[a[i]]=0;
break;
}
}
int ans=0;
bool flag=true;
for(int i=0;i<n;i++)
if(ch[i]!='0')flag=false;
if(flag)ans++;
for(int i=1;i<n;i++)
{
int k=__gcd(i,n);
ans+=f[k];
}
printf("%d\n",ans);
return 0;
}