Holding Bin-Laden Captive!
题目链接:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1326
Holding Bin-Laden Captive!
Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByte
Total Submit: 136 Accepted: 64

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!
View Code
Total Submit: 136 Accepted: 64
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
1 1 3
0 0 0
Sample Output
4
分析:
简单的母函数题目。
代码:
View Code
#include<iostream>
using namespace std;
const int MAXN=10005;
int c1[MAXN], c2[MAXN]; //c1存放目前所有函数的乘积, c2存放两个函数的临时乘积
int maxn;
void genfun(int sum, int *n, int *v)
{
int i, j, cnt=0;
maxn=n[0]*v[0]; //存放当前x的最大次数,相当于x^maxn
for (i=0; i<=n[0]; i+=v[0]) //计算第一个子函数。
{
c1[i]=1;
}
for (cnt=1; cnt<sum; cnt++) //计算其它的子函数
{
maxn+=n[cnt]*v[cnt];
for (i=0; i<=maxn; i++) //计算两个函数的乘积
{
for (j=0; j<=n[cnt]*v[cnt]; j+=v[cnt])
{
c2[i+j]+=c1[i];
}
}
for (i=0; i<=maxn; i++)
{
c1[i]=c2[i];
c2[i]=0;
}
}
}
int main()
{
int n[MAXN], v[MAXN];
while (cin>>n[0]>>n[1]>>n[2], n[0]||n[1]||n[2])
{
int i;
v[0]=1; v[1]=2; v[2]=5;
memset(c2, 0, sizeof(c2));
memset(c1, 0, sizeof(c1));
genfun(3, n, v);
for (i=0; i<=maxn; i++)
{
if (c1[i]==0)
{
cout<<i<<endl;
break;
}
}
if (i>maxn) cout<<maxn+1<<endl;
}
return 0;
}

浙公网安备 33010602011771号