China Remainder Theorem (also called Sun Tzu's theorem)

Source: Internet
Author: User

Today, I am bored occasionally. When I see something called the "China Surplus Theorem", I think it is fun to write a summary of the question first. Suppose that a number (1) is divided by 3 and 2, (2) divide by 5 and 4, (3) divide by 7 and 6, and obtain the minimum integer lcm (5, 7) that meets the conditions) 35 35*2 = 70 just divided by 3 remainder 1 lcm (3, 7) is 21 21 21*1 = 21 just divided by 5 remainder 1 lcm (5, 3) 15 15*1 = 15 just divided by more than 7 1 next (70*2 + 21*4 + 15*6) % lcm (70, 21, 15) = 104 final, 104 is the required result. The following is A mathematical formula: Assume that a number M is divided by the remainder of A, B, and C respectively, and the minimum integer (A, B, c, a, B, and c are all positive integers. Step 1: first solve LCM (B, C) = A' and multiply A' by an appropriate positive integer Ka (from 1 ), A '* Ka % A = 1 is terminated once it is established, so that a' = A' * Ka can be used to calculate B', C' is defined as above Step 2: result of solving LCM (A, B, C): The final result can be expressed as (a' * A + B '* B + C "* c) % LCM (, b, C); below, use C ++ to implement the complete code [cpp] # include <iostream> using namespace std; int GCD (int a, int B) {int tmp; if (a <B) return GCD (B, a); while (B) {tmp = B; B = a % B; a = tmp;} return ;} int LCM (int a, int B) {return a * B/GCD (a, B);} void GET2 (int & K2, int K) {int I = 1; while (1) {if (K2% K = 1) break; else K2 * = ++ I;} int main () {int A, A1, A2, B, b1, B2, C, C1, C2, a, B, c, I, D; cout <"Enter three divisor and remainder respectively :"; cin> A> a; cin> B> B; cin> C> c; // obtain the minimum public multiples of A', B ', C 'a2 = A1 = LCM (B, C); B2 = B1 = LCM (A, C); C2 = C1 = LCM (A, B ); D = LCM (A1, A); // The minimum public multiple of the three numbers. // you can call the following methods to obtain the minimum public multiples: A', B ', and C' GET2 (A2, ); GET2 (B2, B); GET2 (C2, C); cout <"Required number:" <(A2 * a + B2 * B + C2 * c) % D <endl; return 0 ;}

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.