POJ1014 Diving bitset优化背包
dp小水题,结果我还WA了一遍qwq
1 #include <cstdio> 2 #include <bitset> 3 #define maxn 120010 4 using namespace std; 5 bitset<maxn> bit; 6 int a[20],tot,T; 7 void work(){ 8 bit.reset(); 9 bit[0]=1,tot/=2; 10 for(int i=1;i<=6;i++){ 11 int k=1; 12 while(a[i]-k>=0){ 13 bit|=bit<<(k*i); 14 a[i]-=k; 15 k<<=1; 16 if(bit[tot]){printf("Can be divided.\n\n");return;} 17 } 18 if(a[i]) bit|=bit<<(a[i]*i); 19 } 20 printf("Can't be divided.\n\n"); 21 } 22 int main(){ 23 while(1){ 24 tot=0; 25 for(int i=1;i<=6;i++) scanf("%d",&a[i]),tot+=a[i]*i; 26 if(!tot) break; 27 printf("Collection #%d:\n",++T); 28 if(tot&1) printf("Can't be divided.\n\n"); 29 else work(); 30 } 31 }