C - 麦哲伦的异或
题目:
Time limit 1000 ms
Memory limit 65536 kB
OS Windows
Source 2015 Multi-University Training Contest 5
MZL loves xor very much.Now he gets an array A.The length of A is n.He wants to know the xor of all ( Ai + Aj )( 1 ≤ i , j ≤ n )
The xor of an array B is defined as B1 xor B2 …xor Bn
input
Multiple test cases, the first line contains an integer T(no more than 20), indicating the number of cases.
Each test case contains four integers: n , m , z , l
A1= 0 , Ai= ( Ai − 1∗ m + z) m o d l
1 ≤ m , z, l ≤ 5 ∗ 105 , n = 5 ∗ 105
output
For every test.print the answer.
Sample Input
26
88
0
Sample Output
2
8
思路:
在位异或运算中有n^n=0和0^n=n,所以当i不等于j的时候(ai+aj)^(aj+ai)=0,所以在求所有的(ai+aj)( 1 ≤ i , j ≤ n )的位异或运算时相当于只剩下i等于j的情况了,打个比方当1 ≤ i ≤ 2, 1 ≤ j ≤ 2时,要求的就是(a1+a1)^(a1+a2)^(a2+a1)^(a2+a2),而(a1+a2)^(a2+a1)=0所以最终答案为0^(a1+a1)^(a2+a2).
代码