Holding Bin-Laden Captive!
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
题目大意:
给定三种币种,1,2,5块钱,
分别输入这三个币种的数量,让你给出不能组合出来的最小的钱数
案例给出 1个1块,1个2块,3个5块, 无法组合成4块钱。
思路:
利用生成函数的思路,对于给定的一块钱,可设为多项式(1,x,x^2,x^3,x^4,x^5,,,,x^n1) 其中n1为所对应的给定数量。
同样的,将2块钱设为多项式 (1,x^2,x^4,x^6,x^8,,,,x^n2),因为2块钱的贡献是一块钱的两倍
同样的,将5块钱。。。。(1,x^5,x^10,x^15,x^20,,,,x^n3,),.n3 为五块钱的数量
将这个多项式进行计算,最后最小的多项式的系数为0 的值就是答案 具体思路在代码
code:
#include<iostream>
#include<cstring>
using namespace std;
//用于多项式系数 c[i]表示 第i项的系数 因为所有系数的最高次项不超过8000
int c1[10000],c2[10000];
int k_arr[] = {1,2,5};
//三个币种的数量
int k_num[3];
int main(){
while(scanf("%d%d%d",&k_num[0],&k_num[1],&k_num[2])!=EOF){
if( k_num[0]==0 && k_num[1]==0 && k_num[2]==0 ) break;
//初始化
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
//计算最大的项数,可以理解为能够组合的最大的数。
int max_num = k_num[2]*5+k_num[1]*2+k_num[0];
//先对第一个多项式进行计算
for(int i=0;i<=k_num[0];i++) c1[i] = 1;
//1->n 个币种,这里只有三个币种
for(int i=1;i<3;i++){
int k=k_arr[i];//k 表示 第 i 个币种的币值
//第 i 个币种的数量
for(int j=0;j<=k_num[i];j++)
//逐一进行多项式计算
//对当前的某一项 x^j*k 对目前所有的项数进行计算,因为有的项数为0
for(int z=0;j*k+z<=max_num;z++)
c2[j*k+z] += c1[z];
memcpy(c1,c2,sizeof(c2));
memset(c2,0,sizeof(c2));
}
for(int i=1;;i++)
if(c1[i]==0){
//第一个为0即为答案
printf("%d\n",i);
break;
}
}
return 0;
}