MZL's xor
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 354 Accepted Submission(s): 254
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,根据公式求出A1到An,然后定义Bi+j = Ai+Aj ,这里需要你看清楚一点,就是那个提示:(1 <= i , j <= n)那么就应该是排列而不是组合,
A1+A2和A2+A1分别对应的是B12和B21,然后就是异或的一个特性:异或同一个数偶数次,然并卵~~~所有的 i != j构成的 Bij 和 Bji 数值上是相同的,都异或起来不就是
然并卵了?剩下的就只要把 i == j的异或起来就行了。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define Max(a,b) (a>b?a:b) #define Min(a,b) (a<b?a:b) #define LL long long using namespace std; int main() { LL t,n,m,i,j,k,l,z,sum; scanf("%lld",&t); while(t--&&scanf("%lld%lld%lld%lld",&n,&m,&z,&l)) { sum=0; j=k=0; for(i=2;i<=n;i++) { k=(j*m+z)%l;//求出Ai j=k; sum^=2*k;//异或Bii } printf("%lld\n",sum); } return 0; }