输入n,代表学院里面有n种设备,并且在下面输入n行,每一行输入v,m代表设备的价格为v,设备的数量是m.然后要求把这些设备的总价值分摊,尽量平分,使其总价值接近相等,最好是相等
比如样例二
(1+X10)(1+X20+X40)(1+X30)
展开后 从sum的一半开始向下枚举 找到第一有系数的X
Sample Input
2
10 1
20 1
3
10 1
20 2
30 1
-1
Sample Output
20 10
40 40
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <string> 6 # include <cmath> 7 # include <queue> 8 # include <list> 9 # define LL long long 10 using namespace std ; 11 12 int c1[250010], c2[250010]; 13 int w[55]; 14 int num[55]; 15 int main() 16 { 17 //freopen("in.txt","r",stdin) ; 18 int n; 19 while(scanf("%d", &n) && n>0) 20 { 21 memset(w, 0, sizeof(w)); 22 memset(num, 0, sizeof(num)); 23 memset(c1, 0, sizeof(c1)); 24 memset(c2, 0, sizeof(c2)); 25 26 int sum = 0; 27 for(int i = 1 ; i <= n ; ++i) 28 { 29 scanf("%d %d", &w[i], &num[i]); 30 sum += w[i]*num[i]; 31 } 32 33 34 for(int i=0; i<=w[1]*num[1]; i+=w[1]) 35 c1[i] = 1; 36 int len = w[1]*num[1]; 37 for(int i=2; i<=n; ++i) 38 { 39 for(int j=0; j<=len; ++j) 40 for(int k=0; k<=w[i]*num[i]; k+=w[i]) 41 { 42 c2[k+j] += c1[j]; 43 } 44 len += w[i]*num[i]; 45 for(int j=0; j<=len; ++j) 46 { 47 c1[j] = c2[j]; 48 c2[j] = 0; 49 } 50 } 51 for(int i= sum/2; i>=0; --i) 52 if(c1[i] != 0) 53 { 54 printf("%d %d ", sum-i, i); 55 break; 56 } 57 } 58 return 0; 59 }