Mathematical Problems (I.)

Source: Internet
Author: User
Tags greatest common divisor

1. The method of Euclidean algorithm

The greatest common divisor of two integers is obtained by using the method of dividing the two. Note GCD (A, A, b) is a greatest common divisor of two numbers a and a. The theoretical basis for the Euclidean method is: gcd (A, b) = gcd (b, a% b).
Because the T is greatest common divisor of a and B, then A = MT, B = NT, M and n mutually exclusive, a = K*b + a%b, k = A/b. Then Mt = K*nt +a%b, so a%b can be divided by T, then T is b and a%b convention number, set a%b = Rt.
t = gcd(b, a%b) <==> n 和 r 互斥
by MT = k*nt + RT, if N and R are not mutually exclusive, then set n = ps, r = QS, s! = 1, so that M can be divisible by s, that is, M and N is not mutually exclusive, and the previous conclusion contradiction!
So gcd(a, b) = gcd(b, a%b)

Algorithm implementation

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

2. Extended Euclidean method

Integers A and B, greatest common divisor is gcd (b), there must be ax + by = gcd(a, b) . How do you find X and y?
Set bx‘ + a%b*y‘ = gcd(b, a%b) , according to Euclidean theorem has bx ' + a%b*y ' = gcd (b, a%b) = GCD (A, b) = ax + by, then there is bx ' + a%b*y ' = ax + by.
Since a%b = a-a/b, bx ' + (a-a/b) y ' = ax + by, i.e. ay ' + B (x '-a/b*y ') = ax + by, then there are
x = y‘
y = x‘ - a/b*y‘
Therefore, can be satisfied bx ' + a%b*y ' = gcd (b, a%b) x ' and Y ' before continuing to find X and y, when B = 0 o'clock, x = 1, y = 0 can be returned.

Algorithm implementation

Returns the greatest common divisor of A and B, while returning X and Y to meet ax + by = gcd (A, b) int extgcd (int a, int b, int& x, int& y) {    if (b = = 0) {        x = 1;        y = 0;        return A;    }    int d = EXTGCD (b, a% B, y, x);    Y-= (A/b) *x;    return D;}

3. Modulo Operation

Note that in some circumstances, if a is negative, the result of a%m is also negative, so 若想要得到a%m在[0, m-1)范围内的值,可以使用 (m + a % m) %m。
A and B are equal to M and can be expressed as A = B (mod m). Assuming A=c (mod m), B = d (mod m), then there are the following basic rules:
a + b = c + d(mod m) 
a - b = c - d(mod m) 
a x b = c x d(mod m)

4. Fast Power algorithm

Ask for an integer x of n-Th square xn , if n is represented as 2 binaryN=KTk t−1.k1 k0 (b ) /span> ,N=2 kii1 +2 ki2 +.
XN=X(2 ki1+ 2 ki2+ ) /span> =X2KI1∗X2ki2 ∗x 2 ki3 ∗.
So, you can calculate the power from low to high in sequence.x2ki , and then in the binary representation of N if Ki is 1, then the final result needs to be multiplied by x2ki .

Long Long Mod_pow (long long x, long long n, long long m) {    long long res = 1;    while (n >  0) {        if (N & 1)            res = res * x% m;        x = x * x% m;        n >= 1;    }    return res;}

5. Power of The Matrix

typedef vector<int> VEC;TYPEDEF vector<vec> mat;typedef long long int ll;const int M = 10000;//calculation A*bmat mul (m at& A, mat& B) {    Mat C (A.size (), VEC (B[0].size ()));    for (int i = 0; i < a.size (); i + +) {for        (int k = 0; k < b.size (); k + +) {for            (int j = 0; J < B[0].size (); j + +) {                C[i][j] = (C[i][j] + a[i][k]*b[k][j])% M            ;    }}} Return c:}//calculates the A^nmat Pow (Mat A, ll N) {    mat B (A.size (), VEC (A.size ()));    for (int i = 0; i < a.size (); i + +) {        b[i][i] = 1;    }    while (n > 0) {        if (N & 1)            B = Mul (b, A);        A = Mul (A, a)        n >= 1;    }    return B;}

Mathematical Problems (I.)

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.