AcWing 223. 阿九大战朱最学
原题链接
简单
作者:
zsfzmxl
,
2024-04-18 19:49:14
,
所有人可见
,
阅读 41
一眼中国剩余定理,秒!
C++ 代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 15;
int m[N], a[N];
int exgcd(int a, int b, int &x, int &y){
if (!b){
x = 1; y = 0;
return a;
}
int d = exgcd(b, a % b, y, x);
y -= (a / b) * x;
return d;
}
signed main(){
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) scanf("%intd%intd", &m[i], &a[i]);
int M = 1;
for (int i = 1; i <= n; i ++ ) M *= m[i];
int res = 0;
for (int i = 1; i <= n; i ++ ){
int x, y;
int MM = M / m[i];
exgcd(MM, m[i], x, y);
res = (res + a[i] * MM % M * x) % M;
}
res = (res + M) % M;
cout << res << endl;
return 0;
}