Solution Report: POJ2891 Strange Way to Express Integers

Source: Internet
Author: User

Solution Report: POJ2891 Strange Way to Express Integers
Solution Report: POJ2891 Strange Way to Express Integers

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 integer K. Lines 2 ~ K+ 1: Each contains a pair of integers Ai, 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

 

The number is now expressed as a new form, that is, using a number to remove multiple mk numbers and obtain the remainder rk respectively) to uniquely identify the original number. With num and m1 ~ Mn is easily expressed in this form, but now, in turn, give you n (mk, rk) pairs, let you determine the number of num? Output-1 does not exist.

 

Analysis: the bare Solution of Linear homogeneous equations, to be honest, I did not understand the principle of this Code, especially the m2/gcd, so I should use the Code as an interface first. There is a better explanation of the blog you can look at the principle (http://blog.csdn.net/orpinex/article/details/6972654), but I do not understand, wait for the study of the remaining Chinese back to see it. This question is equivalent to solving the equation:

X forward r1 (mod m1)

X ipvr2 (mod m2)

......

X ≡ rn (mod mn)

It is a linear homogeneous equations. I have used a template, but I still don't understand the principle... Orz .. If you are willing to tell me something, please join me on QQ 452884244. Thank you.

 

Code:

 

# Include
 
  
# Define deusing namespace std; typedef long ll; ll exgcd (ll a, ll B, ll & x, ll & y) // extend gcd {if (B = 0) {x = 1; y = 0; return a;} ll gcd = exgcd (B, a % B, x, y); ll tem = x; x = y; y = tem-a/B * y; return gcd;} int main () {int n; ll r1, m1, r2, m2, x0, y0; while (cin> n) {bool flag = 1; cin> m1> r1; // use the first homogeneous equation as the basis for (int I = 1; I <n; I ++) {cin> m2> r2; // process the remainder equations ll a = m1, B = m2, c = r2-r1; ll d = exg Cd (a, B, x0, y0); // calculate the basic solution X0 and d = GCDif (c % d! = 0) flag = false; // no solution condition ll t = B/d; // you cannot understand it. It seems that some coefficients are being updated. X0 = (x0 * (c/d) % t + t) % t; // it seems that some coefficients are being updated. R1 = m1 * x0 + r1; // it seems that some coefficients are being updated. M1 = m1 * (m2/d); // do not understand, it seems that some coefficients are being updated .} If (! Flag) {cout <-1 <endl; continue;} cout <r1 <endl;} return 0 ;}
 

Number theory is hard to understand ....

 


Related Article

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.