Holding Bin-Laden Captive! (HDU-1085)
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
1 1 3
0 0 0
Sample Output
4
題意:每組給出 1 角、2 角、5 角硬幣的個數 num1、num2、num5,問最小的不能組成的錢數
思路:普通母函數
根據題意,使用模版 2 較為方便,只需考慮 n2 數組的影響即可
首先 k=3,v[0]=1,v[1]=2,v[2]=3,n2[0]=num1,n2[1]=num2,n2[2]=num5,n1[0]=n1[1]=n1[2]=0
其次 P 可忽略,那么?last2 改為 last2=last+n[i]*v[i]
由于要求最小的不能組成錢數,那么在跑完模版后,枚舉所有的冪次從 0?到 last,尋找 a[i]=0 的序號 i 即可。
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define EPS 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long const int MOD = 1E9+7; const int N = 10000+5; const int dx[] = {0,0,-1,1,-1,-1,1,1}; const int dy[] = {-1,1,0,0,-1,1,-1,1}; using namespace std;int a[N];//權重為i的組合數 int b[N];//臨時數組 int v[N],n1[N],n2[N]; int main(){v[0]=1;v[1]=2;v[2]=5;while(scanf("%d%d%d",&n2[0],&n2[1],&n2[2])!=EOF&&(n2[0]+n2[1]+n2[2])){a[0]=1;memset(n1,0,sizeof(n1));int last=0;for(int i=0; i<3; i++) { // int last2=min(last+n2[i]*v[i],P);//計算下一次的lastint last2=last+n2[i]*v[i];memset(b,0,sizeof(int)*(last2+1));//只清空b[0..last2]for(int j=n1[i]; j<=n2[i]&&j*v[i]<=last2; j++) //last2for(int k=0; k<=last&&k+j*v[i]<=last2; k++) //一個是last,一個是last2b[k+j*v[i]]+=a[k];memcpy(a,b,sizeof(int)*(last2+1));//b賦值給a,只賦值0..last2last=last2;//更新last}int i;for(i=0;i<=last;i++)if(a[i]==0)break;printf("%d\n",i);}return 0; }?
總結
以上是生活随笔為你收集整理的Holding Bin-Laden Captive! (HDU-1085)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZAP-Queries(洛谷-P3455
- 下一篇: String Problem(HDU-3