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

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

OutputFor 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 母函数模板题
#include<bits/stdc++.h>

using namespace std;
#define maxn 1250000 int ans[maxn], temp[maxn];
int v[],num[];
int n, sum; void init()
{
// memset(a,0,sizeof(a));
int mid = sum/;
ans[]=;
int i, j, k;
for(i=; i<=num[]; i++)
ans[i*v[]] = ;
for(i=; i<n; i++)
{
for(j=; j<=mid; j++)
for(k=; (k*v[i]+j)<=mid&&k<=num[i]; k++)
{
temp[j+k*v[i]] +=ans[j];
}
for(j=; j<=mid; j++)
{
ans[j] = temp[j];
temp[j] = ;
}
}
} int main()
{
int n;
while(cin >> n && n!= -)
{
sum = ;
memset(ans, , sizeof(ans));
for(int i = ; i < n; i++)
{
cin >> v[i] >> num[i];
sum += v[i]*num[i];
}
init();
for(int i = sum/; i>=; i--)
{
if(ans[i]) {cout << sum-i << " " << i << endl;break;}
}
}
return ;
}

上面的代码连样例都过不了,但我们只要把 上面的 void init() 函数写到主函数里面去, 就能AC, 楼主不太懂, 大佬能说一下吗???

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define MAXN 1001000
using namespace std; int value[200],number[200],ans[MAXN],temp[MAXN];
int main()
{
    int n,i,j,k;
    while(cin >> n&&n>=0)
    {
        int max = 0;
        for(i=0; i<n; i++)
        {
            cin >> value[i] >> number[i];
            max += value[i] * number[i];
        }
        int mid = max/2 ;
        memset(ans,0,sizeof(int)*mid+10);
        memset(temp,0,sizeof(int)*mid+10);
        for(i=0; i<=number[0]; i++)
            ans[i*value[0]] = 1;
        for(i=1; i<n; i++)
        {
            for(j=0; j<=mid; j++)
                for(k=0; (k*value[i]+j)<=mid&&k<=number[i]; k++)
                {
                    temp[j+k*value[i]] +=ans[j];
                }
            for(j=0; j<=mid; j++)
            {
                ans[j] = temp[j];
                temp[j] = 0;
            }
        }
        for(i=mid; i>=0; i--)
            if(ans[i]!=0)
                break;
        cout << max - i<<" "<< i << endl;
    }
    return 0;
}

Big Event in HDU (母函数, 玄学AC)的更多相关文章

  1. HDU1171——Big Event in HDU(母函数)

    Big Event in HDU DescriptionNowadays, we all know that Computer College is the biggest department in ...

  2. HDU 1171 Big Event in HDU 母函数

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory ...

  3. Big Event in HDU(HDU1171)可用背包和母函数求解

    Big Event in HDU  HDU1171 就是求一个简单的背包: 题意:就是给出一系列数,求把他们尽可能分成均匀的两堆 如:2 10 1 20 1     结果是:20 10.才最均匀! 三 ...

  4. 组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. Big Event in HDU(杭电1171)(多重背包)和(母函数)两种解法

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. 杭电1171 Big Event in HDU(母函数+多重背包解法)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  7. hdu 1171 Big Event in HDU (01背包, 母函数)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. Big Event in HDU(多重背包套用模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Java/Othe ...

  9. hdoj1171 Big Event in HDU

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. 【Python爬虫】Requests库的基本使用

    Requests库的基本使用 阅读目录 基本的GET请求 带参数的GET请求 解析Json 获取二进制数据 添加headers 基本的POST请求 response属性 文件上传 获取cookie 会 ...

  2. 这个zsh超级棒

    https://github.com/robbyrussell/oh-my-zsh wget用法: http://man.linuxde.net/wget https://pan.baidu.com/ ...

  3. 多线程-interrupt(),isInterrupted(),interrupted()(转)

    Content 背景 中断 相关方法 阻塞方法 不可中断的阻塞方法 处理不支持中断的线程中断的常用方法 处理InterruptedException 待决中断 实例1 实例2 参考资料 Top 背景 ...

  4. axios的特点有哪些?

    1.axios是一个基于promise的HTTP库,支持promise的所有API: 2.它可以拦截请求和响应: 3.它可以转换请求数据和响应数据,并对响应回来的内容自动转换为json类型的数据: 4 ...

  5. Visual Studio 10.0设置引用HalconDotNet.dll

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010435562/article/details/8858638 開始做Halcon的上位机.选用 ...

  6. 萌新接触前端的第三课——JavaScript

    JavaScript概述 一.JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase ...

  7. 结合python+selenium使用AutoIt V3实现文件、图片上传

    1.下载.安装AutoIt V3 下载官网:https://www.autoitscript.com/site/autoit/downloads/ 2.AutoIt V3组件介绍 AutoIt Win ...

  8. 003-linux命令-文件和目录、查看文件内容-文本处理

    文本处理 cat file1|command(sed,grep,awk,) > result.txt 合并一个文件的详细说明文本,并将简介写入一个新文件中 cat file1|command(s ...

  9. (转)Springboot日志配置(超详细,推荐)

    Spring Boot-日志配置(超详细) 更新日志: 20170810 更新通过 application.yml传递参数到 logback 中. Spring Boot-日志配置超详细 默认日志 L ...

  10. [LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...