forked from rajatjain-21/Algorithmic-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBITSET(Classical).cpp
More file actions
52 lines (46 loc) · 960 Bytes
/
Copy pathBITSET(Classical).cpp
File metadata and controls
52 lines (46 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n,q;
cin>>n>>q;
while(n>0)
{
long long arr[n+1];
long long sum=0;
for(long long i=0;i<n;i++)
{
cin>>arr[i];
sum+=arr[i];
}
vector< bitset<61> > dp(sum+1LL);
dp[0][0]=1;
for(long long i=0;i<n;i++)
{
for(long long j=sum;j>=arr[i];j--)
{
dp[j]|=((dp[j-arr[i]])<<1LL);
}
}
while(q--)
{
long long w;
cin>>w;
//cout<<dp[w]<<"\n";
long long flag=0LL;
for(long long i=1;i<=60;i++)
{
if(w>sum || w<=0LL)
break;
if(dp[w][i]!=0)
{flag=1LL;
cout<<i<<" ";
}
}
if(flag==0LL)
cout<<"That's impossible!";
cout<<"\n";
}
cin>>n>>q;
}
}