Number Theory F-strange way to express integers (Chinese Remainder Theorem of non-reciprocal elements)

Source: Internet
Author: User

F-strange way to express Integers Time limit:1000 ms Memory limit:131072kb 64bit Io format:% I64d & % i64usubmit 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:

ChooseKDifferent Positive IntegersA1,A2 ,...,AK. For some non-negativeM, Divide it by everyAI(1 ≤IK) To find the remainderRi. IfA1,A2 ,...,AKAre properly chosen, m can be determined, then the pairs (AI,Ri) Can be used to expressM.

"It is easy to calculate the pairs fromM, "Said Elina." But how can I findMFrom 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 integerK.
  • Lines 2 ~K+ 1: Each contains a pair of IntegersAI,Ri(1 ≤IK).

Output

Output the non-negative integerMOn 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

28 711 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.

The meaning of the question is very easy. Given K group a r, each group represents x branch R (mod A). Note that all A is not necessarily the same, since a does not interact with each other, it cannot directly use the Chinese surplus theorem to do it. After reading a lot of data, I feel that the mathematician's thinking is not understandable to any other person. I still need to write the computation process on my own.

First, calculate the two groups of X forward r1 (mod A1); X forward R2 (mod A2); Define the variable K1 K2 to get x = K1 * A1 + R1; X = k2 * A2 + R2; obtained from the above equation K1 * A1 + R1 = k2 * A2 + R2; converted to K1 * a1 = (R2-R1) + K2 * A2; for Left and Right modulo A2, since (K2 * A2) % S2 = 0, the equation is converted to K1 * A1 partition (R2-R1) (mod A2 ); use Extended Euclidean to solve the value of K1 (to infer whether there is a value of K1), and return K1 to X1 = K1 * A1 + R1; obtain a specific solution that meets {x = K1 * A1 + R1; X = k2 * A2 + R2;} at the same time. Therefore, X forward x1 (mod lcm (A1, a2); that is, X round (K1 * A1 + R1) (mod (A1 * A2/D); in this way, the two cooperators are converted into one, merge K equations into one, and use Extended Euclidean to obtain the smallest positive solution x


#include <cstdio>#include <cstring>#include <algorithm>#define LL __int64using 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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.