萌新80pts求助
查看原帖
萌新80pts求助
86973
花园Serena楼主2020/11/19 15:49

萌新初学中国剩余定理,不懂为什么80分 代码如下

#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;
}
2020/11/19 15:49
加载中...