#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int maxn = 1e5 + 10; ll a[maxn], r[maxn]; int n; ll exgcd(ll a, ll b, ll& x, ll& y) { if (b == 0) { x = 1, y = 0; return a; } ll r = exgcd(b, a % b, y, x); y -= a/b * x; return r; } ll solve() { ll M = a[1], R = r[1], x, y; for (int i = 2; i <= n; i++) { ll d = exgcd(M, a[i], x, y); if ((R-r[i])%d != 0) return -1; x = (R-r[i])/d*x%(a[i]/d); R -= x*M; M = M / d * a[i]; R %= M; } return (R % M + M) % M; } int main() { while (~scanf("%d", &n)) { for (int i = 1; i <= n; i++) scanf("%lld%lld", &a[i], &r[i]); printf("%lld\n", solve()); } return 0; }
POJ - 2891 Strange Way to Express Integers——扩展欧几里得解同余方程
猜你喜欢
转载自blog.csdn.net/hao_zong_yin/article/details/80275771
今日推荐
周排行