• HDU 1085 Holding Bin-Laden Captive!(母函数或背包DP)


    Holding Bin-Laden Captive!

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 23245    Accepted Submission(s): 10350


    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
     
    Author
    lcy
     
    Recommend
    We have carefully selected several similar problems for you:  1171 2152 2082 1709 2079 
     

    母函数:

    #include<iostream>
    #include <cstring>
    using namespace std;
    #define M 10010
    #define MAXN 11000      //MAXN为最大有多少项相乘
    int a[M],b[M];//a[M]中存最终项系数;b[M]中存取中间变量;
    int main()
    {
        int m,n1,n2,n5;
        int i,j,k;
      //  m=n1+n2*2+n5*5+1;
        while(scanf("%d%d%d",&n1,&n2,&n5)!=EOF)
        {
            memset(a,0,sizeof(a));
            if(n1==0&&n2==0&&n5==0)break;
            //可以加一个跳出循环的条件
            //n为所求的指数的值: ";
            //因为只求指数为n的系数的值:所以循环只到n就结束
            for(i=0;i<=n1;i++)//初始化第一个式子:(1+X^2+X^3+...) 所以将其系数分别存到a[n]
            {
                a[i]=1;
                b[i]=0;
            }
            for(i=2;i<=2;i++)//从第2项式子一直到第n项式子与原来累乘项的和继续相乘
            {
                for(j=0;j<=n1+n2*2+n5*5+1;j++)//从所累乘得到的式子中指数为0遍历到指数为n 分别与第i个多项式的每一项相乘
                     for(k=0;k+j<=n1+n2*2+n5*5+1&&k<=2*n2;k+=i)//第i个多项式的指数从0开始,后面的每项指数依次比前面的多i,比如当i=3时,第3项的表达式为(1+x^3+x^6+x^9+……),直到所得指数的值i+j>=n退出
                     {
                         b[j+k]+=a[j];//比如前面指数为1,系数为3,即a[1]=3 的一项和下一个表达式的指数为k=3的相乘,则得到的式子的系数为,b[j+k]=b[4]+=a[1],又a[1]=3,所以指数为4的系数为b[4]=3;
                     }
    
                 for(j=0;j<=n1+n2*2+n5*5+1;j++)//  然后将中间变量b数组中的值依次赋给数组a,然后将数组b清零,继续接收乘以下一个表达式所得的值
                  {
                      a[j]=b[j];
                      b[j]=0;
                  }
            }
    
                  for(i=5;i<=5;i++)//从第2项式子一直到第n项式子与原来累乘项的和继续相乘
            {
                for(j=0;j<=n1+n2*2+n5*5+1;j++)//从所累乘得到的式子中指数为0遍历到指数为n 分别与第i个多项式的每一项相乘
                     for(k=0;k+j<=n1+n2*2+n5*5+1&&k<=5*n5;k+=i)//第i个多项式的指数从0开始,后面的每项指数依次比前面的多i,比如当i=3时,第3项的表达式为(1+x^3+x^6+x^9+……),直到所得指数的值i+j>=n退出
                     {
                         b[j+k]+=a[j];//比如前面指数为1,系数为3,即a[1]=3 的一项和下一个表达式的指数为k=3的相乘,则得到的式子的系数为,b[j+k]=b[4]+=a[1],又a[1]=3,所以指数为4的系数为b[4]=3;
                     }
    
                 for(j=0;j<=n1+n2*2+n5*5+1;j++)//  然后将中间变量b数组中的值依次赋给数组a,然后将数组b清零,继续接收乘以下一个表达式所得的值
                  {
                      a[j]=b[j];
                      b[j]=0;
                  }
            }
            for(int i=1;i<=n1+n2*2+n5*5+1;i++)
            {
                if(a[i]==0)
                {
                  printf("%d
    ",i);
                  break;
                }
            }
        }
    
        return 0;
    }

    DP:

    #include<iostream>
    #include <algorithm>
    #include<cstring>
    using namespace std;
    #define INF 0x3f3f3f3f
    int a[3500];
    int dp[10010];
    int main()
    {
        int n1,n2,n5,ans;
        while(scanf("%d%d%d",&n1,&n2,&n5)!=EOF)
        {
            ans=0;
            fill(dp,dp+10002,INF);
            dp[0]=0;
            if(n1==0&&n2==0&&n5==0)break;
            for(int i=1;i<=n1;i++)
                a[i]=1;
            for(int i=n1+1;i<=n1+n2;i++)
                a[i]=2;
            for(int i=n1+n2+1;i<=n1+n2+n5;i++)
                a[i]=5;
            for(int i=1;i<=n1+n2+n5;i++)
                for(int j=n1+n2*2+n5*5;j>=a[i];j--)
            {
                dp[j]=min(dp[j],dp[j-a[i]]+a[i]);
            }
          for(int i=1;i<=n1+n2*2+n5*5+1;i++)
            {
                if(dp[i]>=INF)
                {
                    printf("%d
    ",i);
                    break;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    numpy 基础 —— np.linalg
    图像旋转后显示不完全
    opencv ---getRotationMatrix2D函数
    PS1--cannot be loaded because the execution of scripts is disabled on this system
    打开jnlp Faild to validate certificate, the application will not be executed.
    BATCH(BAT批处理命令语法)
    oracle vm virtualbox 如何让虚拟机可以上网
    merge 实现
    Windows batch,echo到文件不成功,只打印出ECHO is on.
    python2.7.6 , setuptools pip install, 报错:UnicodeDecodeError:'ascii' codec can't decode byte
  • 原文地址:https://www.cnblogs.com/a249189046/p/7526686.html
Copyright © 2020-2023  润新知