题意就是给你价值从1到6的物体的个数
求是否可以给他们平分
用多重背包解决
AC代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int dp[120005]; int w[120005],a[1000],b[1000]; int max(int x,int y) { return x>y?x:y; } void zero(int cost,int weight,int n) ///01背包 { for(int i = n; i >= cost; i--) dp[i] = max(dp[i],dp[i-cost] + weight); } void complete(int cost,int weight,int n) ///完全背包 { for(int i = cost; i <= n; i++) dp[i] = max(dp[i],dp[i-cost] + weight); } void multi(int v,int w,int num,int sum) { if(v*num > sum) ///如果取完了大于总的价值 用完全背包 complete(v,w,sum); else ///否则 01 背包 { int k = 1; while(k < num) { zero(k*v,k*w,sum); num-=k; k=k*2; } zero(num*v,num*w,sum); } } int main() { int t = 0; while(~scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6])) { t++; int v; int total = a[1]*1 + a[2]*2 + a[3]*3 + a[4]*4 + a[5]*5 + a[6]*6; if(total == 0) break; if(total%2!=0) { printf("Collection #%d:\nCan't be divided.\n\n",t); continue; } else { memset(dp,0,sizeof(dp)); v = total/2; for(int i = 1; i <= 6; i++) multi(i,i,a[i],v); if(dp[v]==v) printf("Collection #%d:\nCan be divided.\n\n",t); else printf("Collection #%d:\nCan't be divided.\n\n",t); } } return 0; }