Solution to a problem [Luo Gu P3951] [NOIP2017] Oscar doubts

Title Description

There are two kinds of face value of coins in the hands of Oscar's two par value are positive integers and prime to each other. Each has numerous Oscar gold. Without the change, the two gold alone, some items he is unable to accurately paid. Now Oscar would like to know the exact items can not pay, the most expensive value is how much gold? Note: input data to ensure the presence of goods not paid for Oscar accurate.

Input and output formats

Input Format

Two positive integers \ (A \) and \ (B \) , separated by a space therebetween, showing the coins in the denomination Oscar.

Output Format

A positive integer \ (N \) in the case, said they did not give change, the value of the most expensive items in the hands of Oscar gold can not be accurately paid.

Sample input and output

Input Sample # 1

3 7

Sample Output # 1

11

Explanation

Sample Description 1 O

Oscar hands face value \ (3 \) and \ (7 \) gold numerous, without the change can not pay the value of accurate \ (1,2,4,5,8,11 \) of items, including the most expensive items value \ (11 \) , than \ (11 \) you can buy items, such as:

\(12 = 3 \times 4 + 7 \times 0\)

\(13 = 3 \times 2 + 7 \times 1\)

\(14 = 3 \times 0 + 7 \times 2\)

\(15 = 3 \times 5 + 7 \times 0\)

Data range and Conventions

For \ (30 \% \) Data: \ (. 1 \ Le A, B \ Le 50 \) .

For \ (60 \% \) Data: \ (. 1 \ Le A, B \ Le ^ 10. 4 \) .

For \ (100 \% \) Data: \ (. 1 \ Le A, B \ ^. 9 Le 10 \) .

answer

This is a goodGuess ConclusionNumber of topics.

See examples, not \ (3 \ times 7 - 10 = 11 \) do? Thus, we boldly presented the code corresponding to the just conclusion.

Huh? How no \ (AC \) ? A closer look at the data range: \ (1 \ Le A, b \ Le 10 ^ 9 \) , \ (10 ^ 9 \ Times 10 ^ 9 \) is not a burst \ (int \) do? So quickly put \ (int \) instead \ (Long \) \ (Long \) .

So we \ (AC \) up!

However, we still have to prove it just conclusion:

Currency set are \ (A \) , \ (B \) .

Easy to get: \ (A \ Times b \) certainly represented by these two currencies.

\ (a \ times b - a \) it? Of course also be, by - \ (1 \ b) Double \ (A \) on it.

\ (a \ times b - b \) it? Of course also be, by - \ (1 \ a) Zhang \ (B \) on it.

\ (a \ times b - a - b \) it? The seemingly can not.

Factorization about:

\(a \times b - a - b\)

\(= a(b - 1) - b\)

\(= a(b - 1) - (b - 1) - 1\)

\(=(a - 1)(b - 1) - 1\)

It can not be divided, indicating \ (a \ times b - a - b \) can not be used as currency \ (A \) , \ (b \) money to scrape out.

Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>

using namespace std;

inline long long gi()
{
    long long f = 1, x = 0; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar();}
    while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar();}
    return f * x;
}

long long a, b;

int main(void)
{
    a = gi(), b = gi();
    cout << a * b - a - b;
    return 0;
}

Guess you like

Origin www.cnblogs.com/xsl19/p/11104968.html