(DP) The number that cannot be bought [Blue Bridge Cup] (Pei Shu Theorem)

The amount that cannot be bought

Xiao Ming opened a candy store.

He is ingenious: wrap the fruit candy into two packs of 4 and 7 packs.

Candy cannot be sold unpacked.

When a child comes to buy candy, he uses these two packages to combine.

Of course, the number of candies cannot be combined, such as buying 10 candies.

You can test it with a computer. In this case, the maximum quantity that cannot be bought 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 quantity of two packages is known.

input format

Two positive integers n,m, representing the number of sugar in each package.

output format

A positive integer representing the maximum number of sugars that cannot be bought.

data range

2≤n, m≤1000, to ensure that the data must have a solution.

Input sample:

4 7

Sample output:

17

Precondition : Given a and b, if d=gcd(a,b)>1 (that is, the greatest common divisor>1), the largest number must not be made up.

Because if d>1, then a and b must be multiples of d, then the number combined by a and b must also be a multiple of d, so there must not be a maximum number, so that the numbers after this number can be a get together with b

Conclusion: If a and b are both positive integers and relatively prime, then the largest number that cannot be made from ax+by, x≥0, y≥0 is (a−1)(b−1)−1 (this is the theorem, It is difficult to prove, just memorize the theorem) .

Coprime: the greatest common divisor is 1

Pei Shu's theorem : If the greatest common divisor of a and b is d, how can there be two integers p and q such that ap+bq=d, as long as ab is relatively prime, there must be a solution

If ab is relatively prime, there must be ap+bq=1, and both sides are multiplied by m => apm+bqm=m => (am-q)p+(bm+p)q=m

Method 1. Violent search (check the watch to find the pattern, it will time out, AC50%)

        When the number to be made is reduced to 0, it means that it is made up
        , first try to use p to make up, the number to be made up becomes mp
        and then try to use q to make up, the number to be made up becomes mq,
        if you can't make up, then returns false

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int n,m,ans;
bool dfs(int m,int p,int q){
	if(!m) return true;
	if(m>=p&&dfs(m-p,p,q)) return true;
	if(m>=q&&dfs(m-q,p,q)) return true;
	return false;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=1000;i++){
		if(!dfs(i,n,m)) ans=i;
	}
	cout<<ans<<endl;
	return 0;
}

According to this violent search, we can find the rules by typing the table, as follows:

3 2 1
3 4 5
3 5 7
3 7 11
3 8 13


It can be roughly found that when n=3, m+1, ans+2,
so ans=2m+x
is substituted into the data to get x=-3,
and the formula is ans=2m-3; (n=3)
similarly, push more several formulas

4 7 17
4 9 23
4 11 29

ans=3m-4(n=4)
Finally, the approximate formula ans=(n-1)*(m-1)-1 is obtained.

Method 2. Use the formula to directly output the answer: (p-1)(q-1)-1

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m;
int main(){
	cin>>n>>m;
	cout<<(n-1)*(m-1)-1<<endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_56501550/article/details/129860128