BZOJ_3687_Simple questions_bitset

BZOJ_3687_Simple questions_bitset

Description

Daddy started to study set theory, he raised four questions about a number set:
1. Arithmetic sum of the XOR sum of subsets.
2. XOR and XOR of subsets.
3. Arithmetic sum of the arithmetic sum of subsets.
4. The XOR sum of the arithmetic sum of the subsets.
    So far, Daddy has solved the first three problems, and the last one has not been solved yet. He decided to give
this problem to you, and the future training team members will realize it.

Input

The first line, an integer n.
The second line, n positive integers, representing 01, a2.... , .

Output

 A line containing an integer representing the XOR sum of all subset sums.

Sample Input

2
1 3

Sample Output

6


Count the number of times the weight of each sum occurs, and add it to the contribution if it occurs an odd number of times.

Because the sum does not exceed 2000000.

Can be optimized with bitset, for each x, XOR on f<<x.

 

Code:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <bitset>
using namespace std;
bitset<2000050>f;
int main() {
    int n,k,x,i,sum=0,ans=0;
    f[0]=1;
    scanf("%d",&n);
    for(i=1;i<=n;i++) {
        scanf("%d",&x);
        f^=(f<<x);
        sum+=x;
    }
    for(i=1;i<=sum;i++) {
        if(f[i]) ans^=i;
    }
    printf("%d\n",ans);
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325016589&siteId=291194637