You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.1 KiB
85 lines
2.1 KiB
2 years ago
|
## [$AcWing$ $1298$. 曹冲养猪](https://www.acwing.com/problem/content/1300/)
|
||
|
|
||
|
|
||
|
|
||
|
### 一、题目描述
|
||
|
自从曹冲搞定了大象以后,曹操就开始琢磨让儿子干些事业,于是派他到中原养猪场养猪,可是曹冲很不高兴,于是在工作中马马虎虎,有一次曹操想知道母猪的数量,于是曹冲想狠狠耍曹操一把。
|
||
|
|
||
|
举个例子,假如有 $16$ 头母猪,如果建了 $3$ 个猪圈,剩下 $1$ 头猪就没有地方安家了;
|
||
|
|
||
|
如果建造了 $5$ 个猪圈,但是仍然有 $1$ 头猪没有地方去;
|
||
|
|
||
|
如果建造了 $7$ 个猪圈,还有 $2$ 头没有地方去。
|
||
|
|
||
|
你作为曹总的私人秘书理所当然要将准确的猪数报给曹总,你该怎么办?
|
||
|
|
||
|
**输入格式**
|
||
|
第一行包含一个整数 $n$,表示建立猪圈的次数;
|
||
|
|
||
|
接下来 $n$ 行,每行两个整数 $a_i,b_i$,表示建立了 $a_i$ 个猪圈,有 $b_i$ 头猪没有去处。
|
||
|
|
||
|
你可以假定 $a_i$,$a_j$ **互质**。
|
||
|
|
||
|
**输出格式**
|
||
|
输出仅包含一个正整数,即为曹冲 **至少养猪的数目**。
|
||
|
|
||
|
**数据范围**
|
||
|
$1≤n≤10,1≤b_i≤a_i≤1100000$
|
||
|
|
||
|
所有$a_i$的乘积不超过 $10^{18}$
|
||
|
|
||
|
**输入样例:**
|
||
|
```cpp {.line-numbers}
|
||
|
3
|
||
|
3 1
|
||
|
5 1
|
||
|
7 2
|
||
|
```
|
||
|
**输出样例:**
|
||
|
```cpp {.line-numbers}
|
||
|
16
|
||
|
```
|
||
|
|
||
|
### 二、实现思路
|
||
|
[中国剩余定理祼题](https://www.cnblogs.com/littlehb/p/17073570.html)
|
||
|
|
||
|
|
||
|
### 三、实现代码
|
||
|
```cpp {.line-numbers}
|
||
|
#include <bits/stdc++.h>
|
||
|
using namespace std;
|
||
|
#define int long long
|
||
|
#define endl "\n"
|
||
|
const int N = 10;
|
||
|
|
||
|
int n;
|
||
|
int a[N], m[N];
|
||
|
|
||
|
void exgcd(int a, int b, int &x, int &y) {
|
||
|
if (!b)
|
||
|
x = 1, y = 0;
|
||
|
else {
|
||
|
exgcd(b, a % b, y, x);
|
||
|
y -= a / b * x;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
signed main() {
|
||
|
cin >> n;
|
||
|
int M = 1;
|
||
|
|
||
|
for (int i = 1; i <= n; i++) {
|
||
|
cin >> m[i] >> a[i];
|
||
|
M *= m[i];
|
||
|
}
|
||
|
|
||
|
int res = 0, x, y;
|
||
|
for (int i = 1; i <= n; i++) {
|
||
|
exgcd(M / m[i], m[i], x, y);
|
||
|
res += a[i] * M / m[i] * x;
|
||
|
}
|
||
|
|
||
|
cout << (res % M + M) % M << endl;
|
||
|
}
|
||
|
```
|