Files
python/TangDou/CSP-S/T1/525.md
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

64 lines
2.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## $AcWing$ $525$小凯的疑惑
### 一、题目描述
小凯手中有两种面值的金币,两种面值均为正整数且彼此互素。
每种金币小凯都有无数个。
在不找零的情况下,仅凭这两种金币,有些物品他是无法准确支付的。
现在小凯想知道在无法准确支付的物品中,最贵的价值是多少金币?
注意:输入数据保证存在小凯无法准确支付的商品。
**输入格式**
输入数据仅一行,包含两个正整数 $a$ 和 $b$,它们之间用一个空格隔开,表示小凯手中金币的面值。
**输出格式**
输出文件仅一行,一个正整数 $N$,表示不找零的情况下,小凯用手中的金币不能准确支付的最贵的物品的价值。
**数据范围**
$1≤a,b≤10^9$
**输入样例**:
```
3 7
```
**输出样例**:
```
11
```
### 二、题目解析
结论:
>如果 𝑎,𝑏 均是正整数且互质,那么由 𝑎𝑥+𝑏𝑦,𝑥≥0,𝑦≥0 不能凑出的最大数是 $(a-1)*(b-1)-1$。
证明:
首先证明 𝑎𝑏−𝑎−𝑏 不能被 𝑎𝑥+𝑏𝑥,𝑥≥0,𝑦≥0表示出。
反证法,假设 𝑎𝑏−𝑎−𝑏=𝑎𝑥+𝑏𝑦,那么 𝑎𝑏=𝑎(𝑥+1)+𝑏(𝑦+1),由于 𝑎|𝑎𝑏,𝑎|𝑎(𝑥+1),所以 𝑎|𝑏(𝑦+1)
,由于 𝑎,𝑏 互质,所以 𝑎|(𝑦+1),由于 𝑦≥0,所以 𝑎<=𝑦+1,所以 𝑏(𝑦+1)≥𝑎𝑏。同理可得 𝑎(𝑥+1)≥𝑎𝑏,所以 𝑎(𝑥+1)+𝑏(𝑦+1)≥2𝑎𝑏>𝑎𝑏,矛盾。
证明 𝑎𝑏−𝑎−𝑏+𝑑,𝑑>0 一定可以表示成 𝑎𝑥+𝑏𝑦,𝑥,𝑦≥0 的形式,可以参考[这篇博客](https://www.cnblogs.com/Yuzao/p/7074465.html)。
时间复杂度分析
计算 𝑎𝑏−𝑎−𝑏 的时间复杂度是 𝑂(1)。
### 三、代码
```cpp {.line-numbers}
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main() {
LL a, b;
cin >> a >> b;
cout << (a - 1) * (b - 1) - 1 << endl;
return 0;
}