Blue Bridge Cup---a number that can't be bought

The number you can't buy for the Lanqiao Cup Zhenti

Number theory

Resource limit
Time limit: 1.0s Memory limit: 256.0MB
Problem description

Xiao Ming opened a candy store. He is ingenious: Pack the fruit candy into two packs of 4 pieces and 7 pieces. Candies cannot be sold unpacked.

When a child comes to buy sweets, he uses these two kinds of packaging to combine. Of course, the number of 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 format

Two positive integers, indicating the number of sugar in each package (not more than 1000)

Output format

A positive integer, indicating the maximum amount of sugar that cannot be bought

Sample input 1

4 7

Sample output 1

17

Sample input 2

3 5

Sample output 2

7

(66%)code1:

#include<stdio.h>

int main()
{
    
    
	int m,n;
	scanf("%d %d",&m,&n);
	
	int min;
	int max;
	
	if(m>n){
    
    
		min=n;
		max=m;
	}else{
    
    
		min=m;
		max=n;
	}
	
	
	int ans=max-1;
	
	int arr[99999]={
    
    0};//足够大的数组先全部赋值为0;
	
	for(int r=1;r<=m*n-max+1;r++){
    
    //r用来控制问题的规模,从1开始
		for(int x=0;x<=r;x++){
    
    
			arr[m*x+n*(r-x)]=1;
		}
	}
	for(int xx=max+1;xx<=m*n;xx++){
    
    //xx用于控制m糖的数量,进而n糖数量为r-x;
		if(arr[xx]==0){
    
    
			ans=xx;
		}
	}
	printf("%d",ans);
	return 0;
}

Idea 1 : What I thought was that the maximum number that cannot be represented must be less than or equal to m*n;
(100%) code2:

#include<stdio.h>

using namespace std;

int main()
{
    
    
	int m,n;
	scanf("%d %d",&m,&n);
	printf("%d",(m*n-m-n));
	return 0;
}

**Thinking 2: After checking, two relatively prime numbers m, n cannot be expressed as m x+n y (x, y are natural numbers) ** The maximum number is:
(m*nmn) ; number theory Knowledge, accumulate, there is no way.

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/114702904