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 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).
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, 2, 3, i -1, 0
咱們的輸入數據須要的循環和咱們的列一致
#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