HDU1059

/*
 * 母函数+优化
 */
#include<cstdio>
#include<cstring>
#define max 20000
int c1[max],c2[max];
int a[7];

int main(){
    for(int i=1;;i++){
        int sum=0;
        for(int j=1;j<7;j++){
            scanf("%d",&a[j]);
            a[j]%=60;//此处模一个偶数,只要剩下的可以平分,那么就可以。。
            sum+=j*a[j];
        }
        if(!a[1]&&!a[2]&&!a[3]&&!a[4]&&!a[5]&&!a[6]){
            break;
        }
        printf("Collection #%d: ",i);
        if(sum%2){        
            puts("Can't be divided. ");
            continue;
        }
        memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));
                for(int k=0;k<=sum/2&&k<=a[1];k++){
                    c1[k]=1;
                }
        for(int j=2;j<7;j++){
            if(a[j]==0){
                continue;
            }
            for(int h=0;h<=sum/2;h++){
                for(int k=0;k<=a[j]*j&&k+h<=sum/2;k+=j){
                    c2[k+h]+=c1[h];
                }
            }
            for(int k=0;k<=sum/2;k++){
                c1[k]=c2[k];
                c2[k]=0;
            }
        }
        if(c1[sum/2]){
        puts("Can be divided.");
        }
        else{
            puts("Can't be divided.");
        }
        puts("");
    }
}

原文地址:https://www.cnblogs.com/Stomach-ache/p/3703233.html