组合数学 - 母函数的运用 --- 模板题

简介: Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15064    Accepted ...

Holding Bin-Laden Captive!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15064    Accepted Submission(s): 6750


Problem Description
We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But recently, it is reported that he hides in Hang Zhou of China!
“Oh, God! How terrible! ”



Don’t be so afraid, guys. Although he hides in a cave of Hang Zhou, he dares not to go out. Laden is so bored recent years that he fling himself into some math problems, and he said that if anyone can solve his problem, he will give himself up!
Ha-ha! Obviously, Laden is too proud of his intelligence! But, what is his problem?
“Given some Chinese Coins (硬币) (three kinds-- 1, 2, 5), and their number is num_1, num_2 and num_5 respectively, please output the minimum value that you cannot pay with given coins.”
You, super ACMer, should solve the problem easily, and don’t forget to take $25000000 from Bush!
 

 

Input
Input contains multiple test cases. Each test case contains 3 positive integers num_1, num_2 and num_5 (0<=num_i<=1000). A test case containing 0 0 0 terminates the input and this test case is not to be processed.
 

 

Output
Output the minimum positive value that one cannot pay with given coins, one line for one case.
 

 

Sample Input
3
0 0
 
Sample Output
4
 

 

Author
lcy
 

 

 

 

Mean: 

 略

analyse:

 经典的母函数的运用题。

该模板是个数有限制的时候的模板。

Time complexity:O(n^3)

 

Source code:

 

// Memory   Time
// 1347K     0MS
// by : Snarl_jsb
// 2014-09-18-15.35
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 1000010
#define LL long long
using namespace std;

int cnt[4],c1[123456],c2[123456];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
//    freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);
//    freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);
    int val[4]={0,1,2,5};
    int x,y,z;
    while(cin>>x>>y>>z&&(x+y+z))
    {
        int sum=x+2*y+5*z;
        memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));
        int cnt[4]={0,x,y,z};
        for(int i=0;i<=cnt[1];++i)
        {
            c1[i]=1;
        }
        for(int i=2;i<=3;++i)
        {
            for(int j=0;j<=sum;++j)
            {
                for(int k=0;k<=cnt[i];k++)
                    c2[k*val[i]+j]+=c1[j];
            }
            for(int j=0;j<=sum;++j)
            {
                c1[j]=c2[j];
                c2[j]=0;
            }
        }
        for(int i=1;i<=sum+1;++i)
            if(!c1[i])
            {
                cout<<i<<endl;
                break;
            }
    }
    return 0;
}

  

目录
相关文章
|
8月前
|
算法
算法每日一题---两数之和
算法每日一题---两数之和
35 0
|
8月前
|
算法 测试技术 C++
【数学归纳法 组合数学】容斥原理
【数学归纳法 组合数学】容斥原理
|
8月前
考研高数之无穷级数题型三:将函数展开成幂级数和傅里叶级数(题目讲解)
考研高数之无穷级数题型三:将函数展开成幂级数和傅里叶级数(题目讲解)
133 0
|
8月前
考研高数之无穷级数题型二:求和函数(题目讲解)
考研高数之无穷级数题型二:求和函数(题目讲解)
138 0
|
存储 算法 图计算
数学知识:容斥原理
复习acwing算法基础课的内容,本篇为讲解数学知识:容斥原理,关于时间复杂度:目前博主不太会计算,先鸽了,日后一定补上。
167 0
数学知识:容斥原理
[Codeforces] 1557 C Moamen and XOR(组合数学)
题意: 用 < 2k的数填充到长度为n的数组中,要使得数组中所有数& >= ^,问方案数 显然,当k == 1的时候,答案为1,只有当所有的数全为1的情况才可以满足题意 对于其他的情况{ 用小于2k的数进行填充,那么说明填充的数字的二进制位数最多可以有k kk位, 从数的个数的角度来说,奇数个1^ 之后的结果是1,偶数个1^ 之后的结果是0,而对于0来讲,不管多少个0,^起来结果都是0 只要有一个0,那么说这一位上&之后就是0,而为了让^为0,那么只能够选择偶数个1 如果某一位上&为1,那么^为1的情况需要讨论n的奇偶性
131 0
|
C语言
浙大版《C语言程序设计(第3版)》题目集 - 习题5-7 使用函数求余弦函数的近似值(15 分)
浙大版《C语言程序设计(第3版)》题目集 - 习题5-7 使用函数求余弦函数的近似值(15 分)
225 0
|
C语言
浙大版《C语言程序设计(第3版)》题目集 - 习题10-4 递归求简单交错幂级数的部分和(15 分)
浙大版《C语言程序设计(第3版)》题目集 - 习题10-4 递归求简单交错幂级数的部分和(15 分)
175 0
|
机器学习/深度学习
【组合数学】递推方程 ( 特解形式 | 特解求法 | 特解示例 )
【组合数学】递推方程 ( 特解形式 | 特解求法 | 特解示例 )
306 0