Here are the official tutorial:
A2. Medium
Let us first find a method to check whether a shift by a fixed number
k
k
k yields a solution or not. For the sake of simplicity let us work with the case of
n
=
12
n = 12
n=12, let also
k
=
3
k = 3
k=3. We can start by writing a set of equations
(all modulo
2
2
2)
(
1
)
(1)
(1)
{
x
0
+
x
3
=
y
0
x
3
+
x
6
=
y
3
x
6
+
x
9
=
y
6
x
9
+
x
0
=
y
9
\begin{cases} x_0 + x_3 = y_0\\ x_3 + x_6 = y_3\\ x_6 + x_9 = y_6\\ x_9 + x_0 = y_9\\ \end{cases}
⎩⎪⎪⎪⎨⎪⎪⎪⎧x0+x3=y0x3+x6=y3x6+x9=y6x9+x0=y9
Note that for
x
x
x 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
3
3
3 first equations can be always satisfied, and that by taking the sum of all these equations we obtain
0
=
y
0
+
y
3
+
y
6
+
y
9
0 = y_0 + y_3 + y_6 + y_9
0=y0+y3+y6+y9
which means that
y
0
+
y
3
+
y
6
+
y
9
y_0 + y_3 + y_6 + y_9
y0+y3+y6+y9 should be even. This can be easily seen to be a necessary and sufficient condition for
1
1
1 to have a solution
x
x
x. Similarly we can write an analogous system for bits
x
1
,
x
4
,
x
7
,
x
10
x_1, x_4, x_7, x_{10}
x1,x4,x7,x10
(
2
)
(2)
(2)
{
x
1
+
x
4
=
y
1
x
4
+
x
7
=
y
4
x
7
+
x
10
=
y
7
x
10
+
x
1
=
y
10
\begin{cases} x_1 + x_4 = y_1\\ x_4 + x_7 = y_4\\ x_7 + x_{10} = y_7\\ x_{10} + x_1 = y_{10}\\ \end{cases}
⎩⎪⎪⎪⎨⎪⎪⎪⎧x1+x4=y1x4+x7=y4x7+x10=y7x10+x1=y10
which is solvable if
y
1
+
y
4
+
y
7
+
y
10
y_1 + y_4 + y_7 + y_{10}
y1+y4+y7+y10 is even. Finally we obtain that there is a solution x to the
k
k
k-xor-shift equation.
(
3
)
(3)
(3)
{
y
0
+
y
3
+
y
6
+
y
9
=
0
y
1
+
y
4
+
y
7
+
y
10
=
0
y
2
+
y
5
+
y
8
+
y
11
=
0
\begin{cases} y_0 + y_3 + y_6 + y_9 = 0\\ y_1 + y_4 + y_7 + y_{10} = 0\\ y_2 + y_5 + y_8 + y_{11} = 0\\ \end{cases}
⎩⎪⎨⎪⎧y0+y3+y6+y9=0y1+y4+y7+y10=0y2+y5+y8+y11=0
Note also that the set of equations corresponding to the value
k
=
9
k = 9
k=9 would be exactly the same as above!This observation is quite easy to generalize for all values of
n
n
n and
k
k
k. More precisely, whenever
g
c
d
(
n
,
k
1
)
=
g
c
d
(
n
,
k
2
)
=
d
gcd(n, k1) =gcd(n, k2) = d
gcd(n,k1)=gcd(n,k2)=d then one can write
d
d
d equations for
y
y
y that determine whether the xor-shift equations has a
solution for
k
1
k_1
k1 (and equivalently for
k
2
k_2
k2).
The complete solution is: first precompute the answer for every
k
k
k being a divisor of
n
n
n, then for every other
k
k
k, compute
g
c
d
(
n
,
k
)
gcd(n, k)
gcd(n,k) to reduce to one of the precomputed cases. The complexity is
O
(
n
⋅
d
(
n
)
)
=
O
(
n
3
2
)
O(n · d(n)) = O(n^{\frac 3 2})
O(n⋅d(n))=O(n23) where
d
(
n
)
d(n)
d(n) is the number of divisors of
n
n
n.
就是分层数1的个数,必须每一层都有偶数个1才合法。
C
o
d
e
:
Code:
Code:
#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;
}