数论F - Strange Way to Express Integers(不互素的的中国剩余定理)

F - Strange Way to Express Integers
Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u
Submit   Status

Description

Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:

Choose k different positive integers a1a2…, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1a2, …, ak are properly chosen, m can be determined, then the pairs (airi) can be used to express m.

“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”

Since Elina is new to programming, this problem is too difficult for her. Can you help her?

Input

The input contains multiple test cases. Each test cases consists of some lines.

  • Line 1: Contains the integer k.
  • Lines 2 ~ k + 1: Each contains a pair of integers airi (1 ≤ i ≤ k).

Output

Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.

Sample Input

2
8 7
11 9

Sample Output

31

Hint

All integers in the input and the output are non-negative and can be represented by 64-bit integral types.

题意很简单,给出k组 a r  每组代表 x ≡ r (mod a) ;其中要注意的就是所有的a不一定互素,因为a不互素就不能直接用中国剩余定理来做,查了很多资料,感觉数学家的思维不是凡人可以理解的,还是自己写一下计算的过程

首先来计算两组 x ≡ r1 ( mod a1 ) ; x ≡  r2 ( mod a2 ) ; 定义变量 k1 k2 得到 x = k1 * a1 + r1 ; x = k2 * a2 + r2 ; 由上面的等式得到 k1 * a1 + r1 = k2 * a2 + r2 ; 转化为 k1*a1 = (r2 - r1) + k2 *a2 ; 对左右取模a2,因为 (k2*a2)%s2 = 0 ,所以等式转化为  k1 * a1 ≡ ( r2 - r1 ) (mod a2) ;使用扩展欧几里得可以求解到 k1的值(判断是否存在k1的值),将k1带回到 x1 = k1 * a1 + r1 ;得到同时满足于{ x = k1 * a1 + r1 ; x = k2 * a2 + r2 ; }的一个特解 , 所以 x ≡ x1 (mod lcm(a1,a2) ) ; 也就是 x ≡ ( k1*a1+r1 ) ( mod ( a1*a2/d ) );这样也就将两个同余式转化为了一个,通过不断的转化,将k个等式合并为一个 ,用扩展欧几里得求出最小的正解x 


#include 
#include 
#include 
#define LL __int64
using namespace std;
void gcd(LL a,LL b,LL &d,LL &x,LL &y)
{
    if(b == 0)
    {
        d = a ;
        x = 1 ;
        y = 0 ;
    }
    else
    {
        gcd(b,a%b,d,y,x);
        x = -x ;
        y = -y ;
        y += (a/b)*x ;
    }
    return ;
}
int main()
{
    LL k , a1 , a2 , r1 , r2 , d , x , y ;
    while(scanf("%I64d", &k)!=EOF)
    {
        LL flag = 1 ;
        scanf("%I64d %I64d", &a1, &r1);
        k-- ;
        while(k--)
        {
            scanf("%I64d %I64d", &a2, &r2);
            gcd(a1,a2,d,x,y);
            if( (r2-r1)%d )
                flag = 0 ;
            if( flag )
            {
                x = (r2-r1)/d*x ;
                y = a2/d ;
                x = ( x%y +y)%y ;
                r1 = x*a1 + r1 ;
                a1 = (a1*a2)/d ;
            }
        }
        gcd(1,a1,d,x,y);
        if( r1%d )
            flag = 0 ;
        if(flag == 0)
            printf("-1\n");
        else
        {
            x = r1/d*x ;
            y = a1 / d ;
            x = ( x%y+y )%y ;
            printf("%I64d\n", x);
        }
    }
    return 0;
}


你可能感兴趣的:(数学)