Magic Formulas

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
3
1 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 1000010
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值