hdu 1085 Holding Bin-Laden Captive!

来源:互联网 发布:js 清除所有cookie 编辑:程序博客网 时间:2024/06/11 13:57
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 30 0 0
 
Sample Output
4

--------------------------------------------------------------------------------------------------

#include<stdio.h>
#include<string.h>
int c1[500000],c2[500000];
int main()
{
 
 int b[]={1,2,5};
 int a[3];
 int n,i,j,k;
 while(scanf("%d%d%d",&a[0],&a[1],&a[2])&&(a[0]||a[1]||a[2]))
 {
  n = a[0] + 2*a[1] + 5*a[2];  // 当每种面值的硬币数量有限制时,
  memset(c1,0,sizeof(c1));
  memset(c2,0,sizeof(c2));
  //if(a[0]==0)
 // {
 //  printf("1\n");
 //  continue;
 // }
  for(i=0;i<=a[0];i++)//这里的初始化,就是初始化第一个括号内的多项式,也就是第一种硬币的,所以是a[0]
     c1[i] = 1;
 
  for(i=1;i<3;i++)//共有三种硬币,第一种已经算过,所以这里只有两个循环;
  {
   for(j=0;j<=n;j++)
   {
    for(k=0;k<=a[i]&&k*b[i]+j<=n;k++)  // 因为每种硬币的个数有限制,所以 k 应该小于其对应的个数

             n时要兑换的总钱数,k*b[i] +j,是x的指数所以要小于n

              //每种硬币,拿一个还是两个,x的指数(例如当拿面值为2的拿0个x指数就是0(x^0),
             //一个就是2(x^2),两个就是4(x^(2*2)即x^4))
             //由k来控制;所以k加的是每种硬币
     c2[j+k*b[i]] += c1[j];
   }
   for(j=0;j<=n;j++)
   {
    c1[j] = c2[j];
    c2[j] = 0;
   }
  }
  for(i=0;i<200000;i++) // 这里从0开始检索,上面就不用特别判断当 a[0]==0 的情况了
   if(c1[i]==0)
    break;
  printf("%d\n",i);
 }
 return 0;
}

---------------------------------------------

另一种写法;

就是将2 和5 分开计算

#include<iostream>
#include<string>
using namespace std;
#define M 11000
int a[M],b[M];
int main(){
 int x,y,z,i,j,times;
 while(cin>>x>>y>>z){
  if(x==0&&y==0&&z==0) return 0;
 for(i=0;i<=x;i++) a[i]=1;
 for(i=0;i<=2*y;i+=2){
  for(j=0;j<M;j++){
   b[i+j]+=a[j];
  }
 }
 for(i=0;i<M;i++) a[i]=b[i];
 memset(b,0,sizeof(b));
 for(i=0;i<=5*z;i+=5){
  for(j=0;j<M;j++){
   b[i+j]+=a[j];
  }
 }
 for(i=0;i<M;i++) a[i]=b[i];
 memset(b,0,sizeof(b));


 for(i=0;i<M;i++) if(a[i]==0) break;
 cout<<i<<endl;
 memset(a,0,sizeof(a));
}}
 

 

0 0