背包dp(多重)
http://acm.hdu.edu.cn/showproblem.php?pid=1059
多重背包题;
如果sum奇数直接continue;不是奇数则判断dp[sum/2]能不能到达;
即dp[sum/2]的方案数是否为0;
注意输出格式!!!
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 const ll inf=0x3f3f3f3f; 5 const int maxn=60000; 6 long long dp[maxn],sum=0,val[maxn],cnt=0; 7 signed main() 8 { 9 while(1) 10 { 11 int k=0; 12 sum=0;++cnt; 13 memset(val,0,sizeof(val)); 14 memset(dp,0,sizeof(dp)); 15 for(int i=1;i<=6;i++) 16 { 17 cin>>val[i]; 18 sum+=val[i]*i; 19 if(val[i]) ++k; 20 } 21 if(!k) break; 22 cout<<"Collection #"<<cnt<<':'<<endl; 23 if(sum%2) {cout<<"Can't be divided."<<endl<<endl;continue;} 24 dp[0]=1; 25 for(ll i=1;i<=6;i++) 26 { 27 ll num=val[i]; 28 for(ll h=1;num>0;h<<=1) 29 { 30 if(h>num) h=num; 31 num-=h; 32 for(int j=sum/2;j>=i*h;j--) 33 dp[j]+=dp[j-i*h]; 34 } 35 } 36 if(dp[sum/2]) cout<<"Can be divided."<<endl; 37 else cout<<"Can't be divided."<<endl; 38 cout<<endl; 39 } 40 }