这里每两个a[i]之间都互素 , 所以必然存在一个解 , 是一般模线性方程组中的一种特殊情况

 

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 using namespace std;
 5 const int N = 15;
 6 
 7 #define ll long long
 8 ll a[N] , b[N];
 9 
10 ll ex_gcd(ll a , ll &x , ll b , ll &y)
11 {
12     if(b == 0){
13         x = 1 , y = 0;
14         return a;
15     }
16     ll ans = ex_gcd(b , x , a%b , y);
17     ll t = x;
18     x= y , y = t - (a/b)*y;
19     return ans;
20 }
21 //因为均互素所以不必要计算比值
22 ll mod_line(int n)
23 {
24     ll r = b[0] , lcm = a[0] , x , y;
25     for(int i = 1 ; i<n ; i++)
26     {
27         ll del = b[i] - r;
28         ll g = ex_gcd(lcm , x , a[i] , y);
29 
30         x = ((x*del/g % a[i]) + a[i])%a[i];
31         r = r + lcm*x;
32         lcm = lcm*a[i];
33     }
34     return r;
35 }
36 
37 int main()
38 {
39   //  freopen("a.in" , "r" , stdin);
40     int n;
41     while(scanf("%d" , &n) == 1)
42     {
43         for(int i= 0 ; i<n ; i++)
44             scanf("%I64d%I64d" , a+i , b+i);
45         printf("%I64d\n" , mod_line(n));
46     }
47     return 0;
48 }

 

 posted on 2015-01-17 21:09  Love风吟  阅读(225)  评论(0编辑  收藏  举报