People in the Tomskaya region like magic formulas very much. You can see some of them below.
Imagine you are given a sequence of positive integer numbers p1, p2, ..., pn. Lets write down some magic formulas:
![](https://i-blog.csdnimg.cn/blog_migrate/63275ed8a4e30c3254ceb6e6b06b8cf2.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6f9c3f684443d5a02842c9ce8cee6531.png)
Here, "mod" means the operation of taking the residue after dividing.
The expression means applying the bitwise xor (excluding "OR") operation to integers x and y. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by "^", in Pascal — by "xor".
People in the Tomskaya region like magic formulas very much, but they don't like to calculate them! Therefore you are given the sequence p, calculate the value of Q.
Input
The first line of the input contains the only integer n (1 ≤ n ≤ 106). The next line contains n integers: p1, p2, ..., pn (0 ≤ pi ≤ 2·109).
题目大意
给定一串数列p1~pn。求Q。公式如上图,
表示x异或y。
Output
The only line of output should contain a single integer — the value of Q.
Sample test(s)
Input
3
1 2 3
Output
3
题解
以下用^表示异或。
设
为公式1,
为公式2.
2=p1^p2^……^pn……^(1^1)^(1^2)^……^(1^n)^……^(1^1)^(1^2)^……^(1^n).又因为公式1中,i的范围为1~n,所以红色这段可化为(1^1)^(2^1)^……^(n^1)^……^(1^n)^(2^n)^……^(n^n).
对于一个数k,1~n mod k只会有1~k-1与0这k种可能,所以对于(1^k)……(n^k)的结果是有规律的,假设n=15,k=7
结果如图:
1
2
3
4
5
6
0
1
2
3
4
5
6
0
1
而根据异或原则,a^a=0;0^a=a; 所以图中红色两行异或结果为0。(1^7)^(2^7)^……^(15^7)=1;
还有一种情况当n=25,k=7时。如图,(1^7)^(2^7)^……^(25^7)=5^6=3.
1
2
3
4
5
6
0
1
2
3
4
5
6
0
1
2
3
4
5
6
0
1
2
3
4
综上,我们可以先用前缀“异或”纪律1异或到n的结果,再枚举k=1~n,使t=n mod (2*k), 在根据t与k的关系判断结果。具体见代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
int n,a[1000002],ans,s[1000002];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)//预处理
{scanf("%d",&a[i]);
ans=ans^a[i];
}
for(int i=1;i<=n;i++)//前缀“异或”——表示0异或到n的结果
s[i]=s[i-1]^i;
for(int i=1;i<=n;i++)
{int t=n%(i<<1);
if(t==i) ans=ans^s[i-1];
else if(t>i) ans=ans^(s[i-1]^s[t-i]);
else ans=ans^s[t];
}
printf("%d",ans);
return 0;
}