GCD, extending Euclid, Chinese remainder theorem

Source: Internet
Author: User
Tags greatest common divisor

1.GCD:

int gcd (int A,int  b) {    return b==0? a:gcd (b,a%b);}

2. Chinese remainder theorem:

Title: Student a gives n integers a[], student B gives n positive integers m[] and 22, the teacher asks a question: there is a positive integer ans, for each logarithm, there are: (Ans-a[i]) mod m[i]=0. Find out what the minimum is.

Input Sample:

1Ten231 2 32 3 581 2 3 4 5 6 7 8 the  the  the  A  -  -  -  the A1 2 3 4 5 6 7 8 9 Ten  One  A2 3 5 7  One  -  -  +  at  in  to Panax Notoginseng2-2 0999999999 10000000003-10000-20000-300009999 10000 100010

Implementation code:

1#include <fstream>2#include <iostream>3#include <algorithm>4#include <cstdio>5#include <cstring>6#include <cmath>7#include <cstdlib>8 9 using namespacestd;Ten  One #defineEPS 1e-6 A #definell Long Long - #defineINF 0x7fffffff -  the intN; -ll a[ *],m[ *]; -  -ll EXTENDGCD (ll a,ll b,ll &x,ll &y);//Expand Euclid +ll Crt (ll A[],ll m[],intn);//Chinese remainder theorem -  + intMain () A { at     //freopen ("d:\\input.in", "R", stdin); -     //freopen ("D:\\output.out", "w", stdout); -      while(SCANF ("%d",&N), N) { -          for(intI=0; i<n;i++) scanf ("%lld",&a[i]); -          for(intI=0; i<n;i++) scanf ("%lld",&m[i]); -printf"%lld\n", Crt (a,m,n)); in     } -     return 0; to } +ll EXTENDGCD (ll a,ll b,ll &x,ll &y) { -     if(!b) { thex=1, y=0; *         returnA; $}Else{Panax Notoginsengll R=EXTENDGCD (b,a%b,y,x); -Y-=x* (A/b); the         returnR; +     } A } thell Crt (ll A[],ll m[],intN) { +ll mm=1; -      for(intI=0; i<n;i++) mm*=M[i]; $ll ret=0; $      for(intI=0; i<n;i++){ - ll x, y; -ll tm=mm/M[i]; the EXTENDGCD (tm,m[i],x,y); -Ret= (Ret+tm*x*a[i])%mm;Wuyi     } the     return(ret+mm)%mm; -}
View Code

Here's a brief extension of Euclid's derivation:

Basic algorithm: For a non-negative integer A,B,GCD (A, B) that is not exactly 0, the greatest common divisor of A/b is bound to have an integer pair of x, Y, which makes gcd (A, b) =ax+by.

Proof: Set A>b.

1, obviously when B=0,GCD (A, b) =a. At this time x=1,y=0;

When 2,ab!=0

Set AX1+BY1=GCD (A, b);

bx2+ (a mod b) y2=gcd (b,a mod b);

According to the naïve Euclid principle there is gcd (A, B) =gcd (b,a MoD);

Then: ax1+by1=bx2+ (a mod b) y2;

namely: ax1+by1=bx2+ (A-(A/b) *b) y2=ay2+bx2-(A/b) *by2;

According to the identity theorem: x1=y2; Y1=x2-(A/b) *y2;

This gives us a way to solve x1,y1: The value of X1,y1 is based on X2,y2.

The idea above is defined recursively, because the recursive solution of GCD will always have a time b=0, so recursion can end.

By the way, the Chinese remainder theorem:

GCD, extending Euclid, 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.