首页 > 其他分享 >hdu:Holding Bin-Laden Captive(母函数,数学)

hdu:Holding Bin-Laden Captive(母函数,数学)

时间:2023-01-19 12:02:43浏览次数:48  
标签:Bin hdu Laden int num n1 n2 n3 he

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.


输入样例

1 1 3
0 0 0

输出样例

4
方法一:母函数,找到第一个为0的次幂
方法二:数学,若1存在则能组成1,2之和的所有数,设其和为s则s以内的偶数显然都可以组成,
此时将某个2改为1或增加1,则能组成其邻近的奇数,故若1存在则区间s,5+s,10+s,...所以需要判断s==4是否成立
附ac代码
#include<bits/stdc++.h>
using namespace std;
int val[3]={1,2,5};
int cnt[3];
int c1[8010],c2[8010];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n1,n2,n3;
    while(cin>>n1>>n2>>n3,n1!=0||n2!=0||n3!=0)//注意读入结束的条件判断,其中某些数可以为0
    {
        cnt[0]=n1,cnt[1]=n2*2,cnt[2]=n3*5;
        int n=n1+2*n2+5*n3;
        for(int i=0;i<=n;++i)
        {c1[i]=0,c2[i]=0;}
        for(int i=0;i<=cnt[0];i++)
        c1[i]=1;
        for(int i=1;i<=2;++i)
        {
          for(int j=0;j<=n;++j)
           for(int k=0;k<=cnt[i]&&k+j<=n;k+=val[i])
            //注意加k+j<=n的限制条件
           {
               c2[j+k]+=c1[j];
           }
          for(int j=0;j<=n;++j)
            c1[j]=c2[j],c2[j]=0;
        }
        int ans=0;
        while(c1[ans]&&ans<=n) ans++;
        cout<<ans<<'\n';
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n1,n2,n3;
    while(cin>>n1>>n2>>n3,n1!=0||n2!=0||n3!=0)
    {
        if(n1==0)
        {cout<<'1'<<'\n';continue;}
        if(n1+n2*2<4)
        {cout<<n1+n2*2+1<<'\n';continue;}
        cout<<n1+n2*2+n3*5+1<<'\n';
    }
    return 0;
}

 



标签:Bin,hdu,Laden,int,num,n1,n2,n3,he
From: https://www.cnblogs.com/ruoye123456/p/17061264.html

相关文章