[CF577B] Modulo Sum

Description

给定 (n) 个数,问能否选出一个非空子集使得和能被 (m le 10^3) 整除。

Solution

正经解法应该是先容斥原理再背包。

可是写了一发 bitset 背包就冲过去了。

#include <bits/stdc++.h>
using namespace std;

#define int long long 
const int N = 1000005;
const int M = 1005;

int n,m,a[N];
bitset<M> pre[N];

signed main()
{
    ios::sync_with_stdio(false);

    cin>>n>>m;

    int flag = 0;
    for(int i=1;i<=n;i++) cin>>a[i], a[i]%=m, flag |= a[i]==0;

    if(flag)
    {
        cout<<"YES"<<endl;
        return 0;
    }

    bitset<M> bs;

    for(int i=1;i<=n;i++)
    {
        bitset<M> tmp = (bs<<a[i]) | (bs>>(m-a[i]));
        bs |= tmp;
        bs[a[i]]=1;
    }

    if(bs[0]) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}
原文地址:https://www.cnblogs.com/mollnn/p/13986950.html