HDU1171 Big Event in HDU (母函数)

来源:互联网 发布:人工智能入门书籍 编辑:程序博客网 时间:2024/04/29 21:41

Big Event in HDU

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40542    Accepted Submission(s): 13941


Problem Description
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.
The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
 

Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
A test case starting with a negative integer terminates input and this test case is not to be processed.
 

Output
For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.
 

Sample Input
210 120 1310 1 20 230 1-1
 

Sample Output
20 1040 40
 

Author
lcy
 



思路:一道母函数的题目,用多重背包和0-1背包也能够求解,母函数G(x) = (1+x^v[0] + x^v[0]*2+......)+(1+x^v[1]+x^v[1]*2+.....)+.......,最后就只需判断下sum/2的系数是否为0,如果为0,则查找小于sum/2的最大整数。


#include<cstdio> #include <cstring>using namespace std;int n,v[55],m[55],c1[1000005],c2[1000005];int main(){while(~scanf("%d",&n)){if(n < 0)break;int sum = 0;for(int i = 0; i < n; i ++){scanf("%d%d",&v[i],&m[i]);sum += v[i] * m[i];}memset(c1,0,sizeof(c1));memset(c2,0,sizeof(c2));for(int i = 0; i <= m[0]; i ++){//对第一个表达式进行初始化 c1[i * v[0]] = 1;}for(int i = 1; i < n; i ++){//对剩余的表达式进行计算 for(int j = 0; j <= sum; j ++){//选取c1[]多项式中的第j个元素 if(c1[j]){//幂指数为j的系数存在 for(int k = 0; k <= m[i] * v[i]; k += v[i]){//度第i个多项式选取元素进行计算 if(k + j <= sum)c2[k + j] += c1[j];//c2[k+j] = c2[k+j] + c1[j]*1,c2[k]的系数为1 }}}for(int j = 0; j <= sum; j ++){//更新 c1[j] = c2[j];c2[j] = 0;}}int aver = sum / 2;//判断平均值的系数是否唯一 for(int i = aver; i >= 0; i --){if(c1[i]){//找到最接近平均值的一个数 printf("%d %d\n",sum-i,i);break;}}}return 0;}


0 0