Codeforces Round #242 (Div. 2) C. Magic Formulas

题目

C. Magic Formulasios

time limit per testexpress

2 secondsapp

memory limit per testui

256 megabytesspa

inputcode

standard inputorm

outputci

standard outputinput

People in the Tomskaya region like magic formulas very much. You can see some of them below.it

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=q 1 ^q 2....... ^q n = p 1 ^p 2 ......^p n ^((1%1)^....(1%n))^((2%1)^......(2%n))^....

故Q的求解过程分红两部分

第一部分是求p 1 ^p 2 ......^p n

第二部分是求((1%1)^....(1%n))^((2%1)^......(2%n))^....

将其化成矩形的形式

1%1   1%2  ...........  1%n

2%1   2%2  ............ 2%n

.....................................

n%1   n%2 ............. n%n

当i小于除数时,即 i%j = i (i<j)

故该矩形的上对角线变成                 

1%1     1  ...........      1              (n-1)个1

2%1   2%2  ............  2              (n-2)个2

.....................................             ........

(n-1)%1................   n-1

n%1   n%2 ............. n%n              0个n

   注意偶数个相同的元素异或结果为0,0与任何元素异或结果为该元素.

故对于矩形上三角 只须要考虑(n-i)的奇偶性 故这部分代码简化为

                  if(n-i是偶数) res^=i;

进一步分析的出下面几个规律和特色

  1. 矩阵每一列都是周期性的,且每个周期以下循环 1, 2, 3, i -1, 0

  2. 咱们的输入数据须要的循环和咱们的列一致


解答

#include <iostream>

using namespace std;

int table[1110000];

int n;

int tmp;

int ans = 0;

int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> tmp;
		ans ^= tmp;
		table[i] = table[i -1] ^ i;
		if((n/i)&1)
			ans ^= table[i - 1];
		ans ^= table[n%i];
			
	}
	cout << ans << endl;
	return 0;
}

参考文章

http://www.tuicool.com/articles/InYrm2M

相关文章
相关标签/搜索