HDU 5344 – MZL’s xor [异或性质]

HDU题目链接

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)(1i,jn)
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=(Ai1m+z) mod l
1m,z,l5105,n=5105

Output

For every test.print the answer.
题意:这道题又坑啊,题意表达超级有问题,首先n是输入的,是n<=510^5,然后n是数组A的,跟B没有关系,其次是i和j没有大小关系。很多人不明白样例是怎么出来的,我就不说了,直接看代码就明白了:
#include <cstdio>
#include <set>
#include <algorithm>
#include <iterator>
#include <iostream>
using namespace std;
typedef long long ll;

const int maxn = 500000;
ll aa[maxn+10];


int main() {
    ll T; cin >> T;
    while(T--) {
        ll n, m, z, l;
        cin >> n >> m >> z >> l;
        for(int i = 2; i <= n; ++i) {
            aa[i] = (aa[i-1] * m + z) % l;
        }
        ll res = 0;
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
                res ^= (aa[i]+aa[j]);
        cout << res << endl;
    }
    return 0;
}

但是这样会超时,两层循环。然后考虑异或的性质,就是0异或任何书都是那个数,任意两个相同的数异或结果是0.所以(Ai+Aj)^(Aj+Ai)=0,这样就抵消了,所以求的时候只要求所有的(Ai+Ai)的异或即可。最后算aa[i-1] * m可能会超出int,需要用long long.
 

#include <cstdio>
#include <set>
#include <algorithm>
#include <iterator>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;

const int maxn = 500010;
ll  aa[maxn];
int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        ll n, m, z, l;
        aa[1] = 0;
        cin >> n >> m >> z >> l;
        for(int i = 2; i <= n; ++i)
        {
            aa[i] = (aa[i-1] * m + z) % l;
        }
        ll res = 0;
        for(int i = 1; i <= n; ++i)
                res ^= (aa[i]+aa[i]);
        cout << res << endl;
    }
    return 0;
}

欢迎留言