本文主要是介绍【HDU 1085】【母函数】Holding Bin-Laden Captive!【给你a1个一元硬币,a2个两元硬币,a3个五元硬币,问不能凑出来的第一个面额是多少】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
传送门:http://acm.split.hdu.edu.cn/showproblem.php?pid=1085
描述:
Holding Bin-Laden Captive!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20783 Accepted Submission(s): 9232
“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!
1 1 3 0 0 0
4
题意:
给你a1个一元硬币,a2个两元硬币,a3个五元硬币,问不能凑出来的第一个面额是多少。
思路:
母函数,公式为
(1+x+x^2+x^3+.........x^a1)∗(1+x^2+x^4+x^6+.........x^a2)∗(1+x^5+x^10+x^15+............x^a5)
直接模拟即可。
代码:
#include <bits/stdc++.h>
using namespace std;
#define mst(ss,b) memset(ss,b,sizeof(ss));
#define rep(i,k,n) for(int i=k;i<=n;i++)
const int N=10000;int c1[N], c2[N], mx;
int a[3], val[3] = {1, 2, 5};int cal(){mst(c1, 0);mst(c2, 0);mx=0;rep(i, 0, 2){mx += a[i] * val[i];// 可能组成的最大值}rep(i, 0, a[0]){//先处理第一种硬币c1[i] = 1;}rep(i, 1, 2){ // 对于1后面的每种硬币rep(j, 0, mx)if(c1[j]){ //枚举已知范围for(int k = 0; k <= a[i] * val[i]; k += val[i]){//枚举新增范围 if(j + k <= mx)c2[j + k] += c1[j]; }}// 把当前项保存在c1,清空c2 memcpy(c1, c2, sizeof(c1));mst(c2, 0);}rep(i, 1, mx)if(!c1[i])return i;
}int main(){while(~scanf("%d%d%d", &a[0], &a[1], &a[2]), a[0] + a[1] + a[2]){printf("%d\n", cal());}return 0;
}
这篇关于【HDU 1085】【母函数】Holding Bin-Laden Captive!【给你a1个一元硬币,a2个两元硬币,a3个五元硬币,问不能凑出来的第一个面额是多少】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!