[POJ1014]Dividing(二进制优化多重背包)

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int T,A[10];
bool f[100010];

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int main(){
	for(;;){
		int s=0,fg=0;
		for(int i=1;i<=6;++i)A[i]=read(),s+=A[i]*i;
		if(!s)break;
		printf("Collection #%d:\n",++T);
		if(s&1^1){//原来打成s^1&1WA了233
			s>>=1,memset(f,0,sizeof(f)),f[0]=1;
			for(int i=1;i<=6;++i){
				for(int j=1;j<=A[i];j*=2){
					for(int k=s;k>=j*i;--k)if(f[k-j*i])f[k]=1;
					A[i]-=j; 
				}
				if(A[i])for(int k=s;k>=A[i]*i;--k)if(f[k-A[i]*i])f[k]=1;
			}
			if(f[s])fg=1;
		}
		if(fg)printf("Can be divided.\n\n");else printf("Can't be divided.\n\n");
	}
	return 0;
}

 

posted @ 2018-08-08 20:34  void_f  阅读(181)  评论(1编辑  收藏  举报