uva711 Dividing up(DP)

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=652

 

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int c[7],sum,sumw;
bool in(){
    sum=0,sumw=0;
    for(int i=1;i<=6;i++) cin>>c[i],sum+=c[i],sumw+=i*c[i];
    for(int i=1;i<=6;i++){
        if(c[i]) return 1;
    }
    return 0;
}
int dp[150000],z[20];
int main(){
    int ca=1;
    while(in()){
        memset(dp,0,sizeof dp);
        dp[0]=1;
        for(int i=1;i<=6;i++){
            int k=0,amount=c[i];
            while(1<<k <= amount){
                z[k]=1<<k;
                amount-=(1<<k);
                k++;
            }
            if(amount) z[k++]=amount;
            for(int j=k-1;j>=0;j--){
               for(int w=sumw;w>=0;w--){
                  if(dp[w]) dp[w+z[j]*i]=1;
               }
            }
        }
        printf("Collection #%d:\n",ca++);
        if(dp[sumw/2]&&sumw/2*2==sumw) cout<<"Can be divided."<<endl;
        else cout<<"Can't be divided."<<endl;
        puts("");
    }
    return 0;
}
uva711

 

posted @ 2014-02-09 20:38  wonderzy  阅读(257)  评论(0编辑  收藏  举报