Vijos 1164 Cao Chong Pig (Chinese remainder theorem)

Source: Internet
Author: User
Tags greatest common divisor

P1164Cao Chong Pig accepted label: Three Kingdoms hegemony [show label]<textarea id="code" class="textbox" style="font-family:‘Lucida Grande‘,Lucida,‘Lucida Sans Unicode‘,‘Lucida Sans‘,Tahoma,‘Segoe UI‘,Verdana,微软雅黑,‘Microsoft YaHei‘,宋体; margin:0px; height:500px; line-height:16px; outline:0px; padding:4px; border-color:rgb(221,221,221); width:710px; max-width:710px; font-size:13px"></textarea>Describe

Since Cao Chong finished the elephant, Caocao began to fathom let son do some career, so sent him to Zhongyuan pig farm pig, but Cao Chong full not happy, so in the work so-so, there was a time when Caocao want to know the number of sows, so Cao Chong want to play Caocao a. For example, if there are 16 sows, if 3 pigsty are built, there is no place for the 1 pigs left. If you build 5 pigsty, but there are still 1 pigs, there is no place to go, and if you build 7 pigsty, there are 2 heads to go. You as Cao General's personal secretary of course to the accurate number of pigs to amid, what should you do?

Format input Format

The first line contains an integer n (n <= 10) – The number of times to build a pigsty, the solution down n rows, two integers per line of AI, bi (bi <= ai <= 1000), the creation of an AI pigsty, there is no place for bi pigs. You can assume Ai,aj coprime.

Output format

The output contains a positive integer, which is the number of sows at least to be raised by Cao Chong.

Example 1 sample input 1[copy]
33 15 17 2
Sample output 1[Copy]
16

Main topic:

find the smallest x to make x%m[0]=r[0],x%m[1]=r[1] ....

Problem Solving Ideas:
The Chinese remainder theorem (also known as grandson's theorem) is used to solve the following equations:
X% m[0] = r[0]
X% m[1] = r[1]
X% m[2] = r[2]
X% m[3] = r[3]
......
(The condition of the solution is m array 22 coprime )

Then we set M=m[0]*m[1]*m[2]*......*m[n], because M array 22 coprime, so m/m[i] and m[i] greatest common divisor is 1, that is GCD (m/m[i],m[i]) =1.

So we're going to find a number V to make:
(M/m[i]) * v% m[i] = 1
(M/m[i]) * v = 1 (mod m[i]) ........ ........ ①
As a result of the title, modulo M[i] results are not 1, but R[i]
So both sides of the equation have to multiply r[i] to:
(M/m[i]) * v * r[i]= r[i] (mod m[i])
Then this (m/m[i]) * v * r[i] is part of the final answer X.

(M/m[i]) * v * r[i], we only need v. So how do we find v?
Observe the ① and discover that we only need to meet
(M/m[i]) * v + m[i] * t = 1..... ..... ........ Ii
The V value (where T is any integer).

Observe the ② type, found that in fact ② is (m/m[i]) * v + m[i] * t = gcd (M/m[i],m[i]), so the use of expanding Euclidean can be obtained by the V.

Reference code:

#include <map> #include <stack> #include <queue> #include <cmath> #include <vector># include<cctype> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm>using namespace Std;const double eps=1e-10;const int inf=0x3f3f3f3f;const int mod=1e9+7;const int MAXN=50; typedef long Long ll;int N,M[MAXN],R[MAXN]; ll EXGCD (ll a,ll b,ll& x,ll& y) {    if (b==0)    {        x=1;        y=0;        return A;    }    LL R=EXGCD (b,a%b,x,y);    LL t=y;    Y=x-a/b*y;    x=t;    return r;} LL China (int* m,int* r,int N) {    ll m=1,re=0,x,y;    for (int i=0;i<n;i++)        m*=m[i];    for (int i=0;i<n;i++)    {        LL w=m/m[i];        EXGCD (w,m[i],x,y);        Re= (Re+x*w*r[i])%M;    }    Return (RE+M)%M;} int main () {    scanf ("%d", &n);    for (int i=0; i<n; i++)        scanf ("%d%d", &m[i],&r[i]);    printf ("%lld\n", China (M,r,n));    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Vijos 1164 Cao Chong Pig (Chinese remainder theorem)

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.