The application of the remainder theorem in China

Source: Internet
Author: User

Set N>=2,m1,m2,.... mn, is a positive integer of 22 coprime, recorded M =∏mi, MI = m/mi.

The same residual equations

X≡A1 (mod M1)

X≡A2 (mod m2)

X≡an (mod mn)

There is a unique solution to modulo m

X≡∑aimimi ' (mod M)

The above is the Chinese remainder theorem

The code for solving the minimum nonnegative integer solution of the similar remainder equation is given below. LL represents a long long

1 ll China (ll R)2 {3LL M =2;4LL I,mi,x0,y0,d,ans =0;5      for(i=1; i<=r;i++){6M *=M[i];7     }8      for(i=1; i,=r;i++){9Mi = m/M[i];Ten EX_GCD (mi,m[i],x0,y0); OneAns = (ans+mi*x0*a[i])%M; A     } -     if(Ans <0) ans + =M; -     returnans; the}
View Code

Then take poj1006 this topic to practice the bracelet to connect http://poj.org/problem?id=1006

The congruence equations can be x≡p according to test instructions (mod 23)

X≡e (MoD 28)

X≡i (mod 33)

Note that 23 28 33 is a 22 coprime integer that satisfies the conditions of use of the remainder theorem in China, which is easily solved according to the code given above, and the minimum integer solution of the problem is greater than D.

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <stack>5#include <queue>6#include <map>7#include <algorithm>8#include <vector>9 Ten using namespacestd; One  A Const intMAXN =1000005; -  -typedefLong LongLL; the  -LL a[ -],m[8],m; -  -ll EX_GCD (ll a,ll b,ll &x,ll &y) + { -    if(b = =0){ +x =1; Ay =0; at         returnA; -    } -LL r = EX_GCD (b,a%b,x,y); -LL T =x; -x =y; -y = t-a/b*y; in       returnR; - } to ll China (ll R) + { -M =1; theLL I,mi,x0,y0,d,ans =0; *      for(i=1; i<=r;i++){ $M *=M[i];Panax Notoginseng     } -      for(i=1; i<=r;i++){ theMi = m/M[i]; + EX_GCD (mi,m[i],x0,y0); AAns = (ans+mi*x0*a[i])%M; the     } +     if(Ans <0) ans + =M; -     returnans; $ } $  - intMain () - { the     intCAS =0; - LL p,e,i,d,temp;Wuyi      while(SCANF ("%lld%lld%lld%lld", &p,&e,&i,&d)! =EOF) { thecas++; -         if(p==-1&&e==-1&&i==-1&&d==-1) Break; Wua[1] =p; -a[2] =e; Abouta[3] =i; $m[1] = at; -m[2] = -; -m[3] = -; -LL r =3; ALL ans =China (r); +          while(ans <= d) ans + =M; theprintf"Case %d:the Next triple peak occurs in%lld days.\n", cas,ans-d); -      } $  the  the     return 0; the}
View Code

The application of the remainder theorem in China

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.