题目
题解思路
m1 m2 m3 都互质
这个定理是构造出来的答案 。
如何验证?
我们取第一个式子,首先 i = 1 以外的层数肯定可以整除 m1 因为 大Mi 中留下了m1这个因子。 只需考虑 i = 1 时的情况了 t1 是 M1关于 m1余1的逆元
所以他们俩相等就相等于 M1/y == 1 mod mi 。这时我们再乘个 a1进去
就和一式完全一样了。
这题就是直接套用中国剩余定理的板子题,再加上用扩展欧几里得求逆元 。
AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
const int INF = 0x3f3f3f3f;
long long A[110] , B [110] ;
void exgcd(long long a, long long b, long long &x, long long &y) {
if ( !b )
x = 1, y = 0;
else
{
exgcd(b, a % b, y, x);
y -= a / b * x;
}
}
int main ()
{
ios::sync_with_stdio(false);
int n ;
long long M = 1 ;
cin >> n ;
for (int i = 1 ; i <= n ; i++ )
{
cin >> B[i] >> A[i] ;
M *= B[i] ;
}
long long ans = 0 ;
for (int i = 1 ; i <= n ; i++ )
{
long long t1 = M / B[i] ;
long long x , y ;
exgcd( t1 , B[i] , x , y ) ;
ans += t1*A[i]*x ;
}
cout << ( ans%M + M ) % M << "\n" ;
return 0 ;
}