卧薪尝胆,厚积薄发。
简单题
Date: Wed Sep 19 14:46:57 CST 2018 In Category: NoCategory

Description:

求子集算术和的异或和。
$1\le n \le 1000,1\le \sum a_i\le 2000000$

Solution:

题面限定了 $\sum a$ ,说明这道题可以背包,设 $f[i]$ 表示得到数字 $i$ 有多少种方案,这个可以背包做,最后把所有 $f[i]\&1=1$ 的数拿出来异或就行了,但背包 $1000\times 2000000$ 跑不过,于是用 $bitset$ 左移异或就行了。

Code:


#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<bitset>
#include<cstring>
using namespace std;
int n;
#define MAXN 1010
int a[MAXN];
bitset<2000001> b;
int main()
{
scanf("%d",&n);
int x;
b[0] = 1;
for(int i = 1;i <= n;++i)
{
scanf("%d",&x);
b ^= (b << x);
}
int ans = 0;
for(int i = 1;i <= 2000000;++i)
{
if(b[i])ans ^= i;
}
cout << ans << endl;
return 0;
}
In tag: 其他-bitset
Copyright © 2020 wjh15101051
ღゝ◡╹)ノ♡