link:http://acm.hdu.edu.cn/showproblem.php?pid=1059
最简单的那种
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <queue> 9 #include <deque> 10 #include <queue> 11 #include <list> 12 #include <map> 13 #include <set> 14 #include <vector> 15 #include <utility> 16 #include <functional> 17 #include <fstream> 18 #include <iomanip> 19 #include <sstream> 20 #include <numeric> 21 #include <cassert> 22 #include <ctime> 23 #include <iterator> 24 const int INF = 0x3f3f3f3f; 25 const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}}; 26 using namespace std; 27 int V,m[6],c[6],w[6],f[66666]; 28 void zeropack(int c, int w) 29 { 30 for (int v=V;v>=c;--v) f[v]=max(f[v],f[v-c]+w); 31 return; 32 } 33 void completepack(int c,int w) 34 { 35 for(int v=c;v<=V;++v) f[v]=max(f[v],f[v-c]+w); 36 return; 37 } 38 void multipack(int c,int w,int m) 39 { 40 if(c*m>=V) 41 { 42 completepack(c,w); return; 43 } 44 int k=1; 45 while (k<m) 46 { 47 zeropack(k*c,k*w); m-=k; k*=2; 48 } 49 zeropack(c*m,w*m); 50 return; 51 } 52 int main(void) 53 { 54 ios::sync_with_stdio(false); 55 #ifndef ONLINE_JUDGE 56 freopen("in.txt", "r", stdin); 57 #endif // ONLINE_JUDGE 58 int t=1; for(int i=0;i<6;++i) c[i]=w[i]=i+1; 59 while (1) 60 { 61 int sum=0; 62 for(int i=0;i<6;++i) cin>>m[i], sum+=m[i]*(i+1); 63 if(!sum) break; 64 // if(t!=1) cout<<endl; 65 cout<< "Collection #"<<t<<":\n"; 66 t++; 67 if(sum&1) {cout<< "Can't be divided."<<endl<<endl; continue;} 68 V=sum/2; 69 memset(f,-INF,sizeof(f)); f[0]=0; 70 for(int i=0;i<6;++i) multipack(c[i],w[i],m[i]); 71 if(f[V]<0) cout<< "Can't be divided."<<endl; 72 else cout<< "Can be divided."<<endl; 73 cout<<endl; 74 } 75 return 0; 76 }
其实还是要理解一下这个思想……
不能单靠记忆