Blue Bridge Cup Daily One Question 1.10 2017 Provincial Competition Group A 8. Buns make up the number [gcd to determine whether there is a solution to the binary linear equation] [DP]

Title description

http://oj.ecustacm.cn/problem.php?id=1322

Xiao Ming eats breakfast at a bun shop almost every morning. This steamed bun shop has N kinds of steamers, of which the i-th steamer can hold Ai buns. 
Each steamer has a lot of baskets, which can be regarded as infinite. 
Whenever a customer wants to buy X steamed buns, the uncle who sells steamed buns will select a number of steamed steamed buns, so that there are exactly X steamed steamed buns in these several cages. 
For example, there are 3 kinds of steamers, which can hold 3, 4, and 5 buns. When a customer wants to buy 11 buns, the uncle will choose 2 cages of 3 plus 1 cage of 5 (or maybe 1 cage of 3 plus 2 cages of 4). 
Of course, sometimes Uncle Baozi can't make up the amount the customer wants to buy. 
For example, there are 3 kinds of steamers, which can hold 4, 5, and 6 buns. When the customer wanted to buy 7 buns, the uncle couldn't get it together. 
Xiao Ming wanted to know how many kinds of numbers Uncle Bao could not make up. 

enter

The first line contains an integer N. (1 <= N <= 100) 
Each of the following N lines contains an integer Ai. (1 <= Ai <= 100)   

Output

The output line contains an integer representing the answer. If the number that cannot be made up is infinite, output INF. 

Sample input Copy

2
4
5

Sample output Copy

6

prompt

For the sample, the numbers that cannot be made up include: 1, 2, 3, 6, 7, 11.   

answer:

https://blog.csdn.net/weixin_43914593/article/details/112405425

(1) Is it INF?

         Determine whether a[i] is used as the coefficient of a binary linear equation, whether the equation has an integer solution, with an integer solution gcd==1; without an integer solution, gcd!=1, it is INF

(2) Use dp[i]=1 to indicate that the i-th integer has been calculated, and finally count dp[i] that has not been calculated.

         

for(int i=0;i<n;i++)

        {

            dp[a[i]]=1;

            for(int j=0;j+a[i]<10000;j++)

            {

                if(dp[j])

                {

                    dp[a[i]+j]=1;

                }

            }

        }

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 10000;

int a[maxn];
int dp[maxn]={0};

ll gcd(ll a,ll b)
{
    ll m;
    m=a%b;
    while(m!=0)
    {
        a=b;
        b=m;
        m=a%b;
    }
    return b;
}

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    int g=a[0];
    for(int i=1;i<n;i++)
    {
        g=gcd(g,a[i]);
    }
    if(g!=1)
    {
        cout<<"INF";
    }
    else
    {
        for(int i=0;i<n;i++)
        {
            dp[a[i]]=1;
            for(int j=0;j+a[i]<10000;j++)
            {
                if(dp[j])
                {
                    dp[a[i]+j]=1;
                }
            }
        }
        int ans=0;
        for(int i=1;i<10000;i++)
        {
            if(dp[i]==0)
                ans++;
        }
        cout<<ans;
    }
    return 0;
}


         

Guess you like

Origin blog.csdn.net/qq_43660826/article/details/112986144