BZOJ3687: 简单题

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3687

小呆开始研究集合论了,他提出了关于一个数集四个问题:
1.子集的异或和的算术和。
2.子集的异或和的异或和。
3.子集的算术和的算术和。
4.子集的算术和的异或和。
    目前为止,小呆已经解决了前三个问题,还剩下最后一个问题还没有解决,他决定把
这个问题交给你,未来的集训队队员来实现。

题解:一看应该是个背包问题,但是直接做的话会T。

           然后发现了bitset这种东西。。。简直惊呆了f[i]表示i和是否达到奇数次,然后读入一个x,则f^=f<<x,也就是加或不加取异或,orz

代码:

 1 #include<cstdio>
 2 
 3 #include<cstdlib>
 4 
 5 #include<cmath>
 6 
 7 #include<cstring>
 8 
 9 #include<algorithm>
10 
11 #include<iostream>
12 
13 #include<vector>
14 
15 #include<map>
16 
17 #include<set>
18 
19 #include<queue>
20 
21 #include<string>
22 #include<bitset>
23 
24 #define inf 1000000000
25 
26 #define maxn 500+100
27 
28 #define maxm 500+100
29 
30 #define eps 1e-10
31 
32 #define ll long long
33 
34 #define pa pair<int,int>
35 
36 #define for0(i,n) for(int i=0;i<=(n);i++)
37 
38 #define for1(i,n) for(int i=1;i<=(n);i++)
39 
40 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
41 
42 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
43 
44 #define mod 1000000007
45 
46 using namespace std;
47 
48 inline int read()
49 
50 {
51 
52     int x=0,f=1;char ch=getchar();
53 
54     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
55 
56     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
57 
58     return x*f;
59 
60 }
61 bitset<2000000>f;
62 int x,n,sum,ans;
63 
64 int main()
65 
66 {
67 
68     freopen("input.txt","r",stdin);
69 
70     freopen("output.txt","w",stdout);
71 
72     n=read();f[0]=1;
73     for1(i,n)
74     {
75         scanf("%d",&x);
76         sum+=x;
77         f^=(f<<x);
78     }
79     for1(i,sum)if(f[i])ans^=i;
80     printf("%d\n",ans);
81 
82     return 0;
83 
84 }
View Code

 

 

 

posted @ 2014-11-12 13:27  ZYF-ZYF  Views(300)  Comments(0Edit  收藏  举报