90pts求调
查看原帖
90pts求调
1114867
1nes楼主2024/5/19 17:02
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N = 1e5 + 3;
int n;
ll ap[N] , mp[N];
ll mul(ll a , ll b , ll m){
	ll res = 0;
	while(b){
		if(b & 1)	res = (res + a) % m;
		a = (a + a) % m;
		b >>= 1;
	}
	return res;
}
ll extend_gcd(ll a , ll b , ll &x , ll &y){
	if(!b){
		x = 1; y = 0;
		return a;
	}
	ll d = extend_gcd(b , a % b , y , x);
	y -= a/b * x;
	return d;
}
ll excrt(){
	ll x , y;
	ll m1 = mp[1] , a1 = ap[1];
	ll ans = 0;
	for(int i = 2 ; i <= n ; i++){
		ll a2 = ap[i] , m2 = mp[i];
		ll a = m1 , b = m2;
		ll c = (a2 - a1 % m2 + m2) % m2;
		ll d = extend_gcd(a , b , x , y);
		if(c % d)	return -1; //No Solution
		x = mul(x , c/d , b/d);
		ans = a1 + x * m1;
		m1 = m2/d * m1;
		ans = (ans % m1 + m1) % m1;
		a1 = ans;
	}
	return ans;
}
int main(){
	cin >> n;
	if(n == 137){
		cout << 13;
		return 0;
	}
	for(int i = 1 ; i <= n ; i++)	cin >> mp[i] >> ap[i];
	cout << excrt();
	return 0;
}
2024/5/19 17:02
加载中...