快读加龟速乘TLE了,求调
查看原帖
快读加龟速乘TLE了,求调
806846
Jonas666楼主2023/7/12 16:55
/*
* By:Jonas666
* AC is my honor
* Please dont be WA
*/
#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
typedef long long ll;
const int N = 20;


ll exgcd(ll a, ll b, ll& x, ll& y) {
	if (!b) {
		x = 1;
		y = 0;
		return a;
	}
	ll d = exgcd(b, a % b, y, x);
	y -= a / b * x;
	return d;
}

ll mul(ll a, ll b, ll mod) {
	ll ans = 0;
	while (b) {
		if (b & 1) ans = (ans + a) % mod;
		a = (a + a) % mod;
		b >>= 1;
	}
	return ans;
}

ll read()
{
	ll f = 1, x = 0;
	char ss = getchar();
	while (ss < '0' || ss>'9') { if (ss == '-')f = -1; ss = getchar(); }
	while (ss >= '0' && ss <= '9') { x = x * 10 + ss - '0'; ss = getchar(); }
	return f * x;
}

int main()
{
	//fast;
	ll n;
	n = read();
	ll a1, m1;
	a1 = read();
	m1 = read();
	for (int i = 1; i < n; i++) {
		ll a2, m2, k1, k2;
		a2 = read();
		m2 = read();
		ll d = exgcd(a1, a2, k1, k2);
		ll t = a2 / d;
		k1 = mul(k1, (m2 - m1) / d, t);
		m1 = a1 * k1 + m1;
		a1 = abs(a1 / d * a2);
	}
	printf("%lld\n", (m1 % a1 + a1) % a1);
	return 0;
}
2023/7/12 16:55
加载中...