C - Big Event in HDU(母函数)

题目:

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

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 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

2
10 1
20 1
3
10 1 
20 2
30 1
-1

Sample Output

20 10
40 40

题意:

给出不同物品的数量以及各种物品对应的数量(物品价值不会一样),问是否能够给两个学院分价值相等的物品,如果能输出两个一样的价值,如果不能在尽量相差小的情况下,给计算机学院多一点..........

分析:

求出物品的总价值,/2后以这个数值为最大价值进行母函数运算,从这个/2数值开始从后往前遍历找出第一个不为零的值,由于/2可能不是整数因此这个数值给软件学院

代码:

#include
#include
using namespace std;
int n;
int v[55],m[55];
int num1[250010],num2[250010];
int ans,cnt;
int solve()
{
    num1[0]=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=ans;j++)
        {
            for(int k=0;k<=ans-j&&k<=v[i]*m[i];k+=v[i])
            {
                num2[k+j]+=num1[j];
            }
        }
        for(int j=0;j<=ans;j++)
        {
            num1[j]=num2[j];
            num2[j]=0;
        }
    }
    for(int i=ans;i>=0;i--)
    {
        if(num1[i])
            return i;
    }
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n<=0)
            break;
        cnt=0;
        memset(num1,0,sizeof(num1));
        for(int i=1;i<=n;i++)
        {
            scanf("%d %d",&v[i],&m[i]);
            cnt+=v[i]*m[i];
        }
        ans=cnt/2;
        int b=solve();
        int a=cnt-b;
        printf("%d %d\n",a,b);
    }
    return 0;
}

 

你可能感兴趣的:(母函数)