Counter: get integer X (subset of binary enumeration)

 

 

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
   int n,m;cin>>n>>m;
   int a[30];
   memset(a,0,sizeof(a));
   for(int i=0;i<n;i++)
    cin>>a[i];
   int ans = 0;
   for(int i=0;i<(1<<n);i++)
   {
       int num = 0;
       for(int j=0;j<n;j++)
       {
           if(i&(1<<j))
              num+=a[j];
       }
       if(num==m) ans++;
   }
   cout<<ans<<endl;
    return 0;
}

hdu 5616

It is basically the same as the above question. It should be noted that: enumerate all the possible components of the weight, and mark it, and then you can O(1) query, otherwise it will time out. .

#include <iostream>
#include <cstring>
using namespace std;
const int Max =(1<<20)+10;
int vis[Max];
int a[30],n;
void fun()
{
    memset(vis,0,sizeof(vis));
    int ans =0;
    for(int i=0;i<(1<<n);i++)
    {
        int num=0;
        for(int j=0;j<n;j++)
        {
            if(i&(1<<j)) num+=a[j];
        }
        vis[num]=1;
        for(int j=0; j<n; j++)
        {
            if(num-a[j]>=0)
            {
                 vis[num-a[j]]=1;
            }
        }
    }
}
int main()
{
    int T;cin>>T;
    int x;
    while(T--)
    {
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i];
        fun();
        int m;cin>>m;
        for(int i=0;i<m;i++)
        {
            cin>>x;
            if(vis[x]==1) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/qie_wei/article/details/89020281