Lanqiao Cup—[The 4th] Unable to Buy Number [Group A]

Resource limit
Memory limit: 256.0MB C/C++ time limit: 1.0s Java time limit: 3.0s Python time limit: 5.0s
——————————————————————— ————————————————
Xiao Ming opened a candy shop.
He
came up with an original idea: he packaged the fruit candies into a pack of 4 pieces and a pack of 7 pieces.
​Candy
cannot be sold unpacked. When
a child Of course, some numbers of candies cannot be combined, such as buying 10 candies. You can use a computer to test it. In this packaging situation, the maximum quantity that cannot be purchased is 17. Any number greater than The requirement of this ———————————————————————————————————————————— Input format Two positive integers n,m , indicating the number of sugar pieces in each package. ​The output format is a positive integer, indicating the maximum number of sugars that cannot be purchased. ​Data range


















2≤n,m≤1000,
it is guaranteed that the data must have a solution.
​————————————————————————————————————————————
User
input:
4 7
The program should Output:
17
User
input:
3 5
The program should output:
7
———————————————————————————————————— ————
Resource agreement:
Peak memory consumption <64M
CPU consumption <3000ms

According to the question, we can analyze x n + y m = ?. The question requires what is the largest number that cannot be represented by +ym] = 1 to mark, indicating that it can be expressed by xn + ym . When we traverse a, when a[i] == 0, it means that xn+ym cannot be used. Then the largest i is the number we want to request, considering that the data will explode. Therefore, the table method is used to find the rules and get the final result.

First write a program that can run a small part of the data according to the question: (Special attention is that, if there is a solution, then n and m must be co-prime)

#include<stdio.h>
intmain()
{
    longinta[100000];
    intp ,q;
    scanf("%d%d",&p,&q);
    for(intx=0; x<=1000;x++)
    {
        for(inty=0; y<=1000;y++)
        {
            a[x*p+y*q] =1;
        } 
    }
    intmax=0;
    for(inti=0; i<1000;i++)
        if(a[i] ==0)
        {
            max=i;
        }
    printf("%d",max); 
    return0;
}

Then enter the data to find patterns

m

n

x

m

x

3

5

7

4

3

5

3

7

11

4

5

11

3

8

13

4

7

17

Start looking for patterns:

When m = 3 , every time n increases by 1, x increases by 2. Consider the 2-fold relationship and find that x = 2n-3

When m = 4 , every time n increases by 2, x increases by 6. Consider the 3-fold relationship and find that x = 3n-4

Observe the relationship between m, n and x again and find that x = (m-1)n - m

Therefore, the following program can be written: perfect clearance

#include<stdio.h>
intmain()
{
    int m,n;
    scanf("%d%d",&m,&n);
    printf("%d",(m-1)*n-m); 
    return0;
}

Guess you like

Origin blog.csdn.net/qq_29992017/article/details/129644680