Poj 1006 bioryhthms

Source: Internet
Author: User

Application of Chinese Remainder Theorem

 

23,28, 33

X = P (mod 23)

X = E (mod 28)

X = I (mod 33) (the equal sign of the two lines replaces the equal sign of the three lines ...)

 

The last X-D modulo 21252 again. Note that X-D may be negative or 0.

 

Since 23 28 33 has been fixed, pre-processing can be done first to calculate Mi and Mi-1, M * M-1 = 1 (mod Ni)

Code:

# Include <iostream> <br/> using namespace STD; <br/> struct node <br/> {<br/> int D; <br/> int X; <br/> int y; <br/>}; <br/> int M [4], Mm [4], a [4]; // A [I] x = m [I] (mod n [I]) <br/> int N [4] = {23,28, 33,0 }; <br/> struct node extended_eculid (int A, int B) <br/>{< br/> struct node T1, T2; <br/> If (B = 0) <br/>{< br/> t1.d = A; <br/> t1.x = 1; <br/> t1.y = 0; <br/> return T1; <br/>}< br/> T1 = extended_eculid (B, A % B); <br/> t2.d = t1.d; <br/> t2.x = t1.y; <br/> t2.y = t1.x-(A/B) * t1.y; <br/> return T2; <br/>}< br/> int mles (int, int B, int N) <br/>{< br/> struct node T; <br/> T = extended_eculid (A, n); <br/> return T. x * (B/T. d); <br/>}< br/> int main () <br/> {<br/> int I, ANS, Mul, CA = 0; <br/> Mul = 23*28*33; <br/> while (scanf ("% d", & A [0], & A [1], & A [2], & A [3]) <br/> {<br/> if (a [0] =-1 & A [1] =-1 & A [2] =-1 & & A [3] =-1) <br/> break; <br/> for (I = 0; I <3; I ++) <br/>{< br/> A [I] % = N [I]; <br/> M [I] = Mul/n [I]; <br/>}< br/> ans = 0; <br/> for (I = 0; I <3; I ++) <br/>{< br/> int Te; <br/> Te = mles (M [I], 1, n [I]); <br/> Te = tE + N [I]; <br/> ans + = tE * A [I] * m [I]; </P> <p >}< br/> ANS-= A [3]; <br/> ans = (ANS + 21252) % 21252; <br/> // ans % = 21252; <br/> If (ANS = 0) <br/> ans = 21252; <br/> Ca ++; <br/> printf ("case % d: The next Triple peak occurs in % d days. /n ", CA, ANS); <br/>}< br/> return 0; <br/>}

Related Keywords:

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.