萌新求助exCRT
查看原帖
萌新求助exCRT
361308
Stinger楼主2021/3/27 11:06

这是咕了一个月的坑了(

虽然我知道肯定不会有人帮我调

对于测试点1输出无解。

测试点1:

Input:
32
27 14
44 29
27 14
28 1
22 7
9 5
47 44
14 1
7 1
47 44
5 2
4 1
13 5
32 21
39 5
44 29
30 17
14 1
16 5
40 37
37 27
18 5
38 15
10 7
26 5
10 7
2 1
43 28
3 2
14 1
12 5
12 5

Output:
1877

菜鸡的带马:

#include <cstdio>
#define int long long

int a[100005], b[100005], n;
int mul(int a, int b, const int mod) {
	int ret(0LL);
	bool flag(false);
	if (b < 0) b = -b, flag = true;
	while (b) {
		if (b & 1) ret = (ret + a) % mod;
		a = (a << 1) % mod;
		b >>= 1;
	}
	return flag ? -ret : ret;
}
int qpow(int a, int b, const int mod) {
	int ret(1LL);
	while (b) {
		if (b & 1) ret = mul(ret, a, mod);
		a = mul(a, a, mod);
		b >>= 1;
	}
	return ret;
}
int exgcd(const int a, const int b, int& x, int &y) {
	if (!b) return x = 1, y = 0, a;
	else {
		int g(exgcd(b, a % b, y, x));
		return y -= a / b * x, g;
	}
}
int inv(const int a, const int mod) {
	int x, y;
	exgcd(a, mod, x, y);
	return (x + mod) % mod;
}

void exCRT() {
	for (int i(2); i <= n; ++ i) {
		int x, y, g;
		g = exgcd(b[i - 1], b[i], x, y);
//		printf("%lld\n", g);
		if ((a[i] - a[i - 1]) % g) {puts("-1"); return;}
		b[i] *= b[i - 1] / g;
		x = x / g * (a[i] - a[i - 1]) % b[i] / g;
		a[i] = (mul(b[i - 1], x, b[i]) + a[i - 1] % b[i]) % b[i];
//		printf("x %% %lld = %lld\n", b[i], a[i]);
	}
	printf("%lld", (a[n] + b[n]) % b[n]);
}

signed main() {
	scanf("%lld", &n);
	for (int i(1); i <= n; ++ i) scanf("%lld%lld", b + i, a + i);
	exCRT();
}
2021/3/27 11:06
加载中...