Let's talk about China's residual theorem, Extended Euclidean and the same-remainder equations.

Source: Internet
Author: User
E-solutions for homogeneous linear equations 1 Time limit:1000 ms Memory limit:32768kb 64bit Io format:% I64d & % i64usubmit status

Description

Andy and Mary have a lot of pig. They want to give pig a home. However, Andy does not have enough pigsty circles. Many pig can only settle in one pigsty. For example, if there were 16 pig pigs and Andy built three pig rings, and to ensure fairness, there would be no place to settle the rest of the pig. Mary got angry and scolded Andy for having no brains and asked him to build a pigsty again. This time Andy built five pigsty circles, but there was still one pig with no place to go. Then Andy built seven pigsty circles, but there were two pig with no place to go. Andy is going crazy. You are interested in this matter. You want to know how many pig have been raised by Andy by building pig through andy.

Input

The input contains multiple groups of test data. The first row of each group of data contains an integer n (n <= 10)-the number of times Andy builds a pigsty. Then, N rows are parsed, and each row has two integers, AI, bi (Bi <= AI <= 1000) indicates that Andy has built an AI pigsty, AND Bi pig has no place to go. You can assume (AI, AJ) = 1.

Output

The output contains a positive integer, that is, the minimum number of pigs in the Andy family.

Sample Input

33 15 17 2

Sample output

16



I have already mentioned in my previous blog that I have used the Chinese Remainder Theorem to find the same equations. This time I have gained some benefits, so I will discuss it in detail.
Before talking about the Chinese residue theorem, let's talk about Euclidean Algorithm and Extended Euclidean Algorithm:
First, the Euclidean algorithm calculates the maximum common divisor of two numbers using recursion. The basis is the theorem: gcd (a, B) = gcd (B, A mod B) (A> B and A mod B is not 0) the code is easy:
int gcd(int a,int b)  {      if(b==0)return a;         else return gcd(b,a%b);   }

Again, the Euclidean algorithm is extended. Let's look at the content first: for non-negative integers a, B, gcd (A, B) with an incomplete value of 0, it indicates the maximum approximate number of A and B, there must be an integer pair X and Y, making gcd (a, B) = AX +. It is also implemented by recursion. Why is it implemented by recursion ?? This is determined by its derivation process: Understanding the Method for Solving X and Y
Set A> B.
1. Obviously when B = 0, gcd (a, B) =. At this time, x = 1, y = 0;
2, AB <> 0
Set ax1 + by1 = gcd (A, B );
Bx2 + (a mod B) y2 = gcd (B, A mod B );
According to the simple Euclidean principle, gcd (a, B) = gcd (B, A mod B );
Then: ax1 + by1 = bx2 + (a mod B) y2;
That is, ax1 + by1 = bx2 + (a-[A/B] * B) y2 = ay2 + bx2-[A/B] * by2;
That is, ax1 + by1 = ay2 + B (x2-[A/B] * Y2 );
According to the constant theorem, X1 = Y2; Y1 = x2-[A/B] * Y2;
In this way, we obtain the Method for Solving X1 and Y1: The values of X1 and Y1 are based on X2 and y2.
Because GCD's continuous recursive solution will always have B = 0, so recursion can end (return ).
Look at the code again:
int Egcd(int a,int b,int& x,int& y){int d,t;if(!b){x=1;y=0;return a;}else{Egcd(b,a%b,y,x);y-=x*(a/b);}}

This function has four parameters, which correspond to gcd (a, B) = AX +) (In fact, the result is not related to gcd (a, B), so we do not need to pass its parameters). We set these four parameters respectively as follows: A: ①, b: ②, X: ③, Y: ④: ① * ③ + ② * ④ = K (constant). Therefore, to calculate ③, put ①, ②, ③, and ④ into the parameters of the egcd Function in order, 3. Euclidean is over, and the next step is to talk about the Chinese surplus theorem.
In short, the Chinese Remainder Theorem is: a = AI (mod Ni), and the unknown A is obtained.
Set n = N1 * N2... NK, where the two factors are mutually qualitative. including a ----- (A1, A2 ,..., AK), where Ai = a mod Ni, then a and (A1, A2 ,..., AK) relationship is one-to-one correspondence. that is to say, it can be obtained by a (A1, A2 ,..., AK), or by (A1, A2 ,..., AK) to obtain. The following describes how to calculate a by (A1, A2,..., ak:
Define MI = N1 * N2 *... NK/Ni; CI = mi * (MF mod Ni) among them, mi * MF mod ni = 1;
Then a = (A1 * C1 + A2 * C2 +... + AK * ck) (mod n) (Note: from this equation, a % N can be obtained, when n is very large ). By A = (A1 * C1 + A2 * C2 +... + AK * ck) (mod n): it is known that a is multiplied by the corresponding values of AI and Ci and then added together. In the question, AI should be the remainder (known ), ni is a divisor (known), and CI needs to be calculated, and CI = mi * (MF mod Ni), and MI = N1 * N2 *... NK/Ni (known), so you only need to obtain MF with the key !! How does MF work? Come on, let's look at it. The Extended Euclidean has talked so much about it. It's not just a matter of white!

Mf method: If you understand the Extended Euclidean AX + by = D, you can think:
Mi * MF mod ni = 1 => mi * MF + Ni * y = 1;

I should be familiar with this formula mi * MF + Ni * y = 1. That's right, I want to use egcd for MF!

①: MI, ②: Ni, ③: MF, ④: Y. Don't you want MF ?! Put it in ③!

Next, let's take a look at the Code:

For (I = 0; I <n; I ++) S * = A [I]; for (I = 0; I <n; I ++) {M = S/A [I]; <span style = "white-space: pre"> </span> // M indicates migcd (m, a [I], x, Y); X = (X % A [I] + A [I]) % A [I]; sum = (sum + M * B [I] * x % s) % s; <span style = "white-space: pre "> </span> // use Bi to represent the remainder}

All right, after that, all the code for this question is as follows:
# Include <stdio. h> # include <string. h> # include <math. h> typedef _ int64 int64; // use 64-bit int64 A [11], B [11], int64 egcd (int64 A, int64 B, int64 & X, int64 & Y) {int64 D, T; If (! B) {x = 1; y = 0; return a;} else {egcd (B, A % B, Y, X ); y-= x * (a/B) ;}} int main () {int64 sum, M, S, X, Y; int N, I; while (scanf ("% d", & N )! = EOF) {sum = 0; S = 1; for (I = 0; I <n; I ++) scanf ("% i64d % i64d ", & A [I], & B [I]); for (I = 0; I <n; I ++) S * = A [I]; for (I = 0; I <n; I ++) {M = S/A [I]; egcd (m, a [I], x, y ); X = (X % A [I] + A [I]) % A [I]; sum = (sum + M * B [I] * x % s) % s;} printf ("% i64d \ n", sum); // Since 64-bit is used, % i64d is used for input and output, otherwise, % d will be used for submission ....} Return 0 ;}

Here, the int in the Code is changed to int64, mainly to ensure that some relatively large numbers can pass through.

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.