bzoj 3687 简单题——bitset

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

关于 bitset :https://blog.csdn.net/snowy_smile/article/details/79120063

不知为何,上面的可以 A ,下面的会 WA 。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;
const int M=2e6+5;
int n,s,ans;
bitset<M> dp;
int main()
{
    scanf("%d",&n); dp[0]=1;
    for(int i=1,d;i<=n;i++)
    {
        scanf("%d",&d);
        dp^=(dp<<d); s+=d;
    }
    for(int i=1;i<=s;i++)if(dp[i])ans^=i;
    printf("%d\n",ans);
    return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;
const int N=1005,M=2e6+5;
int n,a[N];
bitset<M> dp;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    dp[0]=1; int s=0;
    for(int i=1;i<=n;i++)
        dp^=(dp<<a[i]),s+=a[i];
    int ans=0;
    for(int i=1;i<=s;i++)if(dp[i])ans^=i;
    printf("%d\n",ans);
    return 0;
}

 

上一篇:UVA - 242 Stamps and Envelope Size (完全背包+bitset)


下一篇:Codeforces 1097 Alex and a TV Show