社区讨论
萌新80pts求助
P1495【模板】中国剩余定理(CRT)/ 曹冲养猪参与者 3已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @locxhg3a
- 此快照首次捕获于
- 2023/10/30 21:20 2 年前
- 此快照最后确认于
- 2023/11/05 07:44 2 年前
萌新初学中国剩余定理,不懂为什么80分
代码如下
CPP#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define R register int
const int MAXN = 10 + 1;
LL a[MAXN], b[MAXN], n, ans = 0;
LL ex_gcd (LL a, LL b, LL &x, LL &y) {
if(b == 0) {x = 1, y = 0; return a;}
LL now = ex_gcd (b, a % b, x, y);
int t = x; x = y, y = t - a / b * y;
return now;
}
int main () {
LL m = 1; scanf("%lld", &n);
for(R i = 1; i <= n; i ++)
scanf("%lld%lld", &a[i], &b[i]);
for(R i = 1; i <= n; i ++) m *= a[i];
for(R i = 1; i <= n; i ++) {
int now = m / a[i];
LL x, y; ex_gcd(now, a[i], x, y);
x = (x % a[i] + a[i]) % a[i];
ans += (now * x) % m * b[i] % m;
}
printf("%lld\n", ans % m);
return 0;
}
回复
共 5 条回复,欢迎继续交流。
正在加载回复...