codeforces1323D Present

codeforces1323D / codeforces1322B Present
elderly retired players playing CF play, found himself reduced intelligence too, almost did not do it DIV2 even D
meaning of the questions: There is an integer n, \ (A_ A_ {1} {2} A_ {...... } n-\) , seeking \ ((a_1 + a_2) \ oplus (a_1 + a_3) \ oplus ...... (a_1 + a_n) \ oplus (a_2 + a_3) \ oplus ...... (a_2 + a_n) \ oplus ...... ( a_n-1 + a_n) \)
where \ ((2 \ leq n \ leq 400,000) (1 \ leq a_i \ leq 10 ^ 7) \)

Portal

Consider the nature or different, so \ (B_ {I, J} = (a_i + a_j) \) , for every one bit needs to be all \ (b_ {i, j} \) different on that one or and.

Consider \ (a_i, a_j \) of \ (b_ {i, j} \) effects.
For \ (a_i, a_j \) of the same bit,
without considering the carry bit when a is 0, a is 1, \ (B_ {I, J} \) to give a 1 to the corresponding position,
considering the carry bit, we find whether a carry bit, we have a relationship with a lot of bits to the right, apparently unable to record the state of DP.
At this time found a property, and for the two binary numbers, i-th bit is a carry, with only 1 ~ i-1 bits and relevant.
So ask the carry becomes very simple, we only need each for all \ (a_i \) to take before i, and then calculate i + 1 digit occurred many times carry on the line (because or the nature of the differences, which occurrence carry we are not concerned)
calculation: take the first i bits, then the new generating a number of sort maintains a monotonically away from the end to the beginning of the tail pointer, the total number of occurrences of the carry bit is obtained

On the last contribution put into place without taking into account the contribution plus the carry of the result on the line

#include <bits/stdc++.h>
#include <tr1/unordered_map>
using namespace std;
inline void R (int &v) {
    static char ch;
    v = 0;
    bool p = 0;
    do {
        ch = getchar();
        if (ch == '-') p = 1;
    } while (!isdigit(ch));
    while (isdigit(ch)) {
        v = (v + (v << 2) << 1) + (ch ^ '0');
        ch = getchar();
    }
    if (p) v = -v;
}
inline void R (long long &v) {
    static char ch;
    v = 0;
    bool p = 0;
    do {
        ch = getchar();
        if (ch == '-') p = 1;
    } while (!isdigit(ch));
    while (isdigit(ch)) {
        v = (v + (v << 2) << 1) + (ch ^ '0');
        ch = getchar();
    }
    if (p) v = -v;
}
int n; 
int a[400005];
int t[32];
int c;
inline bool cmp(const int a, const int b) {
    return (a & t[c]) < (b & t[c]);
}
int one[32], zero[32];
long long newone[32];
inline void count(int x) {
    int now = 1; 
    for(int i = 0; i <= 25; ++i) {
        if(now & x) {
            one[i]++;
        } else {
            zero[i]++;
        }
        now <<= 1;
    }
}
int jinwei[32];
int main() {
    R(n);
    for(int i = 1; i <= n; ++i) {
        R(a[i]);
    }
    for(int i = 1; i <= n; ++i) {
        count(a[i]);
    }
    t[0] = 1;
    for(int i = 1; i <= 31; ++i) t[i] = (t[i - 1] << 1) + 1;
    for(int i = 0; i <= 26; ++i) {
        c = i;
        sort(a + 1, a + n + 1, cmp);
        int tail = n + 1;
        int now = 0;
        for(register int j = 1; j <= n; ++j) {
            if(j >= tail) {
                jinwei[i + 1] += n - j;
                continue;
            } else {
                while(((a[j] & t[c]) + (a[tail - 1] &t[c])) & (1 << i + 1) && (tail - 1 > j)) {
                    --tail;
                    ++now;
                }
                jinwei[i + 1] += now;
            }
            
        }
    }
    for(int i = 0; i <= 27; ++i) {
        newone[i] += (long long)one[i] * zero[i] + jinwei[i]; 
    }
    int now = 1;
    int daan = 0;
    for(int i = 0; i <= 25; ++i) {
        
        if(newone[i] % 2) {
            daan += now;
        }
        now <<= 1;
    }
    cout << daan << '\n';
    return 0;
    
}

Guess you like

Origin www.cnblogs.com/thhyj/p/12441541.html