现在的位置: 首页 > 综合 > 正文

cf#242div.2 C. Magic Formulas ——by rfy

2018年05月02日 ⁄ 综合 ⁄ 共 1340字 ⁄ 字号 评论关闭

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

xor满足交换律,问题可以转化为如何快速求(1 mod i)xor(2 mod i)xor(3 mod i)...(n mod i),结果是1xor2xor3...xor i xor1xor2xor3......

相同的可以抵消,要用到前缀和。code:

var
n,i,x,ans:longint;
an,sum:array[0..1000000] of longint;


begin
readln(n);
for i:=1 to n do
begin
  read(x);
  ans:=ans xor x;
end;
for i:=1 to n do
  sum[i]:=sum[i-1] xor i;

for i:=1 to n do
begin
  if n mod i=0 then
  begin
    if (n div i) mod 2=1 then
      an[i]:=sum[i-1]
    else an[i]:=0;
    continue;
  end;
  if (n div i) mod 2=1 then
  begin
    an[i]:=sum[i-1] xor sum[n mod i];
  end
  else
  begin
    an[i]:=sum[n mod i];
  end;
end;

for i:=1 to n do
  ans:=ans xor an[i];
write(ans);
end.

抱歉!评论已关闭.