Mathematics - Second divisor

definition:

When the remainder of an integer by an integer n d is 0, i.e., d divisible by n, d is a number from about called n, n being a multiple of d, denoted by d | n.

Fundamental Theorem of Arithmetic reasoning

A positive integer greater than N 1, and if it is a standard factorization:

Then it is because of the number of number

It is the sum of all reason and

N number of positive demand about the set - trial division

If d> = sqrt (N) is a divisor of N, the N / d <= N is a divisor of N. In other words, the divisor always come in pairs (except for the perfect square, sqrt (N) appears separately).

Therefore, only need to scan 1 ~ sqrt (N), d can try divisible N, if divisible, the N / N d is the divisor. The time complexity of O (sqrt (N)).

Implementation code:

#include<bits/stdc++.h>
using namespace std;
const int N=1000010;
int h[N];
int main(){
        int cnt=0;
        int x;
        cin>>x;
        int y=sqrt(x);
        for(int i=1;i<=y;i++){
            if(x%i==0){
                h[cnt++]=i;
                if(i!=x/i) h[cnt++]=x/i;
            }
        }
        sort(h,h+cnt);
        for(int i=0;i<cnt;i++)cout<<h[i]<<" ";
        cout<<endl;
    return 0;
}

Try inference division:

The upper limit a submultiple integer N is 2 * sqrt (N).

Positive about 1 ~ N logarithmically each number set - COLORED

When seeking to use "trial division", the time complexity is too high, is O (N * sqrt (N)).

How to deal with it?

It can be considered for each number d, 1 ~ N d is the number to the number of d is approximately a multiple of d, 2d, 3d ......, floor (N / d) * d.

Implementation code:

vector<int> factor[500010];
for(int i=1;i<=n;i++){
    for(int j=1;j<=n/i;j++)
        factor[i*j].push_back(i);
}
for(int i=1;i<=n;i++){
    for(int j=0;j<factor[i].size();j++)
        printf("%d ",factor[i][j]);
     puts("");   
}

The time complexity of O (N * logN);

Multiple methods of inference:

1 to the total number of each divisor number N is approximately N * logN.

The greatest common divisor least common multiple &

definition

The greatest common divisor, refers to two or more integer numbers of about a maximum. a, b referred to as the greatest common divisor gcd (a, b), the same, a, b, c referred to as the greatest common divisor gcd (a, b, c), the greatest common divisor of integers have the same sign .

Two or more integer multiples of the public is called a common multiple thereof, wherein other than 0 smallest common multiple of a least common multiple of these is called an integer. Integer a, b is referred to as the least common multiple LCM (a, b), the same, a, b, c is referred to as the least common multiple lcm (a, b, c), the least common multiple of the plurality of integer symbols have the same.

theorem

对于任意的自然数a,b,有:lcm(a,b)*gcd(a,b)=a*b

prove

设d=gcd(a,b),a0=a/d,b0=b/d。那么gcd(a0,b0)=1。(a0,b0互质)

所以lcm(a0,b0)=a0 * b0。

a0,b0同时扩大d倍,它们的最小公倍数也扩大d倍。(小学知识,相信你一定会 >_<)

于是lcm(a,b)=lcm(a0 * d,b0 * d)=lcm(a0,b0) * d=a0 * b0 * d=a * b/d。

QED

Greatest common divisor

Arithmetic in Nine - Decreases Technique

original:

Only half of the available half, half were not, the sub-set the denominator, the number of sub, much less to reduce, Decreases, which, also seeking. An equal number of covenant.

translation:

(If you need to about scores points, then) be halved, then it is binary (that is, about 2 minutes to use). If not binary, then to compare the size of the numerator and denominator by subtracting the decimal large numbers, each subtraction to subtract, until the difference is equal to the subtrahend far, with this number to approximately equal minutes.

That is:
for any natural numbers a, b and a> = b, there are: gcd (a, b) = gcd (b, ab) = gcd (a, ab)

For any natural numbers a, b, there are: gcd (2a, 2b) = 2gcd (a, b)

prove:

Provided gcd (x, y) = d , then K1 = satisfies X D, Y = K2 D, readily available k1 | k2.

Case 1: x = y. Obviously, gcd (x, y) = x = gcd (x, 0) = gcd (x, yx).

Case 2: wish to make x <y, so there k1 <k2, so that y '= yx = (k2- k1) * d. So should permit k1 | (k2-k1).
By contradiction. So k3 = k2-k1.

Convention assumed that there are m number (m> 1) k1, k3 , i.e. P2 = K2 m, K3 = K2 = K1-P3 m = (P1-P2) * m.

Therefore: K1 = P1 m, K2 = P2 m = (P1 + P3) * m and p1 | (p3 + p1).

For k1 | k2, so m = 1, and contradicts the assumption, so k1 | (k2-k1).
So the original proposition is proved.

In summary, gcd (x, y) = gcd (x, yx).

Of course, this conclusion can be used to promote the general mathematical induction, the properties of the integers are more established.

Code:

#include<iostream>
using namespace std;
int main(){ 
    int a,b; 
    cin>>a>>b; 
    while(a != b){ 
        if(a > b) 
            a -= b; 
        else 
            b -= a; 
     } 
     cout<<a<<endl; 
     return 0;
}

Euclidean algorithm (AKA: Euclidean algorithm)

For any natural numbers a, b, there are: gcd (a, b) = gcd (b, a mod b)

prove:

If a <b, the gcd (a, b) = gcd (b, a mod b) = gcd (b, a), statement is true.

If a> = b, then:

(from: Baidu Encyclopedia)

Comparison:
Decreases Technique, and the main difference is that the Euclidean algorithm arithmetic former used is "minus", which is "addition." From the algorithm ideological point of view, the two are not fundamentally different, but in the calculation process, if you encounter a large number, another number is relatively small, the subtraction may be performed many times in order to achieve the effect of a division, so that the time complexity of the algorithm reduces to O (N), where N is the number of the previous two larger one. In contrast, the Euclidean algorithm stability in time complexity O (logN).

However, since high-precision division modulo difficult to realize, in the case of high accuracy, priority Decreases cytometry.

Guess you like

Origin www.cnblogs.com/zhukaiyuan/p/11600602.html
Recommended