Unable to buy the number [Blue Bridge Cup]

Topic link: Unavailable number
Time limit: 1 Sec Memory limit: 256 MB

Title description:
Xiao Ming opened a candy store. He is ingenious: pack fruit candy into two packs of 4 and 7 pieces. Candies cannot be sold unpacked.
When a child comes to buy sweets, he uses these two packages to combine. Of course, some candies cannot be combined, such as 10 candies.
You can test it with a computer. In this packaging case, the maximum unavailable quantity is 17. Any number greater than 17 can be combined with 4 and 7.
The requirement of this question is to find the largest number that cannot be combined when the quantities of two packages are known.

Input:
Two positive integers, indicating the number of sugars in each package (not more than 1000).
Input to ensure that the two positive integers are relatively prime.
Output:
A positive integer, indicating the maximum number of sugars that cannot be bought

Sample input:
4 7
Sample output:
17

Question: Find the largest number that cannot be combined.
Idea: almost violent, because the data range is not large, then we will mark one by one.

Because the data in the question says that the two numbers are not greater than 1000, then we define the array p to be as large as 1000000, and then mark from the beginning.

p[i]=1 means the number i can be combined
p[i]=0 means the number i cannot be combined

We assume that the two packaging quantities are a and b respectively. If a number i can be combined, then whether i+a and i+b can also be combined, we mark from small to large in this way, and then we start from large To the small traversal, if p[i]==0 (we are traversing backwards, so the first number p[i]==0 is the largest number that cannot be combined) then we just output it.

Insert picture description here

Look at the code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000000;
int p[maxn+1]={
    
    0};
int main()
{
    
    
    int a,b;
    while(cin>>a>>b)
    {
    
    
        //cout<<a*b-a-b<<endl;
        memset(p,0,sizeof(p)); // 每次初始化一下
        p[0]=1; 
        for(int i=1;i<=maxn;i++)
        {
    
    
            if(i>=a&&p[i-a])
                p[i]=1;
            else if(i>=b&&p[i-b])
                p[i]=1;
        }
        for(int i=maxn;i>=1;i--)
        {
    
    
            if(!p[i])
            {
    
    
                cout<<i<<endl;
                break;
            }
        }
    }
    return 0;
}

Careful little cute, did you see my commented code? Congratulations on discovering the ultimate solution to this question. Yes, just write that sentence.
As for this solution, which is number theory, I won’t explain it much. Those who are interested can try to prove it.

Ultimate code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
    int a,b;
    while(cin>>a>>b)
    {
    
    
        cout<<a*b-a-b<<endl;
    }
    return 0;
}

Don’t forget to like it after reading, thank you for your support!
If you are a computer terminal, you can also see the "one key triple connection" in the lower right corner, right click it [haha]
Insert picture description here

Come on!

Work together!

Keafmd

Guess you like

Origin blog.csdn.net/weixin_43883917/article/details/108855520