MZL's xor
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description
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
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) mod l
1≤m,z,l≤5∗105 , n=5∗105
Each test case contains four integers: n , m , z , l
A1=0 , Ai=(Ai−1∗m+z) mod l
1≤m,z,l≤5∗105 , n=5∗105
Output
For every test.print the answer.
Sample Input
2 3 5 5 7 6 8 8 9
Sample Output
14 16
Source
题意:
给你四个整数n,m,z,l,已知A1=0,利用公式Ai=(Ai-1*m+z)%l求出A2到An,题目要求计算所有(Ai+Aj)(1<=i,j<=n)异或的结果。
解题思路:
首先我们先假设n=3来分析一下这道题的解法,n=3的时候我们可以得到A1,A2,A3三个数
那我们要求的其实就是
(A1+A1)^(A1+A2)^(A1+A3)^(A2+A1)^(A2+A2)^(A2+A3)^(A3+A1)^(A3+A2)^(A3+A3)
我们移动一下位置或许题目会更加明朗
(A1+A1)^(A2+A2)^(A3+A3)^(A1+A2)^(A2+A1)^(A1+A3)^(A3+A1)^(A2+A3)^(A3+A2)
我们很容易发现红色部分两两异或之后的结果都为0,从中可以发现此题其实要求的就是求
(A1+A1)^(A2+A2)^……^(An+An)=(A1^A2^……^An)*2
还有一点要注意的是算Ai的过程中会超过int范围,相信大家都知道怎么处理这个问题,好了,闲话不多说,上代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 500005;
const int inf = 1000000000;
int main()
{
__int64 n,m,z,l,t,sum,i,a;
scanf("%I64d",&t);
while(t--)
{
sum=a=0;
scanf("%I64d%I64d%I64d%I64d",&n,&m,&z,&l);
for(i=2;i<=n;i++)
{
a=(a*m+z)%l;
sum^=a;
}
printf("%I64d\n",sum*2);
}
return 0;
}
欢迎大家点评
菜鸟成长记