Magic Formulas

来源:互联网 发布:授权回调域名出错 编辑:程序博客网 时间:2024/05/17 17:59

C. Magic Formulas
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 p1p2, ..., pn. Lets write down some magic formulas:

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).

Output

The only line of output should contain a single integer — the value of Q.

Sample test(s)
input
31 2 3
output
3


题意:给出一个式子,求出它的值,用的是异或运算。

方法:因为异或运算是可以交换的,所以可以将Q的表达式改成p1^p2^...^pn^(1 mod 1)^(2 mod 1)^...^(1 mod 2)^(2 mod 2).....

            可以发现模2 的余数的特点是1,0,1,0.。。。模3 的余数的特点是1,2,0,1,2,0.。。。所以可以用d[i]=1^2^...^(i-1)^0,d[i+1]=d[i]^i;

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define M 1000010using namespace std;int d[M],n,pp;int main(){        int i,mm;        while(scanf("%d",&n)!=EOF)        {                mm=0;                for(i=1;i<=n;i++)                {                        scanf("%d",&pp);                        mm=mm^pp;                }                d[1]=0;                for(i=2;i<=n;i++)                        d[i]=d[i-1]^(i-1);                for(i=1;i<=n;i++)                {                        int qq=n/i;                        int rr=n%i;                        //如果qq是偶数,由于a^a=0,所以mm不变                        if(rr==0&&qq%2!=0)                                mm=mm^d[i];                        //当有余数的时候,要注意是把0也当成循环体的一部分,但余数部分一定不含有0,所以是和d[rr+1]做异或运算                        if(rr!=0&&qq%2==0)                                mm=mm^d[rr+1];                        if(rr!=0&&qq%2!=0)                                mm=mm^d[rr+1]^d[i];                }                printf("%d\n",mm);        }        return 0;}

0 0