http://acm.hdu.edu.cn/showproblem.php?pid=1059
很弱....时间是750ms....
View Code
1 #include <iostream> 2 #define n 7 3 #define maxn 125005 4 using namespace std; 5 int main() 6 { 7 long t=0, i, j, l, sum, ans[maxn], v[n], num[n], m; 8 while(1) 9 { 10 t++; 11 sum = 0; 12 for(i = 1; i < n; i++) 13 { 14 cin >> num[i]; 15 v[i] = i; 16 sum += v[i]*num[i]; 17 } 18 if(sum == 0) break; 19 m = sum/2; 20 for(i = 0; i <= m; i++) 21 ans[i] = 0; 22 for(i = 1; i < n; i++) 23 { 24 if(v[i]*num[i] >= m ) 25 { 26 for(j = 0; j <= m; j++) 27 if(j >= v[i] && ans[j-v[i]]+v[i] > ans[j]) 28 ans[j] = ans[j-v[i]]+v[i]; 29 } 30 else 31 { 32 for(j = 1; j < num[i];) 33 { 34 for(l = m; l >= j*v[i]; l--) 35 if(ans[l-j*v[i]]+j*v[i] > ans[l]) 36 ans[l] = ans[l-j*v[i]]+j*v[i]; 37 num[i] -= j; 38 j = j * 2; 39 } 40 for(l = m; l >= num[i]*v[i]; l--) 41 if(ans[l-num[i]*v[i]]+num[i]*v[i] > ans[l]) 42 ans[l] = ans[l-num[i]*v[i]]+num[i]*v[i]; 43 } 44 } 45 cout << "Collection #" << t << ":" << endl; 46 if(ans[m]*2 == sum && m*2 == sum ) cout << "Can be divided." << endl; 47 else cout << "Can't be divided." << endl; 48 cout << endl; 49 } 50 return 0; 51 }