CF 424C Magic Formulas

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:

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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值