Summary of common algorithm templates - fast, maximum common divisor, minimum common multiple, sum of all factors of a number, prime judgment

Keywords: Algorithm

1. Quick template.

The template of fast power should be familiar to you. I always remember the template directly before. Today, let's explain the meaning of fast power template in detail.

The template without taking the mold is as follows: (modify the mold by yourself)

ll fp(ll a,ll b){
	ll ans=1;
	ll base=a;
	while(b!=0){
		if(b&1!=0)
           ans=ans*base;
        base*=base;
        b>>=1;
	}
	return ans;
} 

How to understand this template:

First, the basic idea of fast power is divide and conquer and binary. We directly bring an example to better understand, such as calculating the 11th power of 2.

First, ans=1,base=2,b=11; We know that the binary of 11 is 1011; if(b&1!=0) ans=ans*base; The function of this statement is to decide whether to multiply the answer according to the binary of the power, base*=base; The function of is to double the cardinality;

    Take the 11th power of 2, ① b=1011,ans=1,base=2   ②ans=1*2,base=2*2,b=101,   ③ans=2*2*2,base=2*2*2*2,b=10   ④ Ans = 2 * 2 * 2, base=2*2*2*2*2*2*2*2,b=1 ⑤ ans = 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2, B = 0 ⑥ jump out of the for loop and end.

In this way, the template of fast power no longer needs four levels of hard back, and it can be handy.

It can be seen from the above that the purpose of calculating the 11th power of 2 is achieved, and the worst complexity is only O(logn), while the complexity of violence is O(n). The fast power is a perfect lover!

2. Template for finding the maximum common divisor of two numbers

(1) Classical Euclidean algorithm (rolling Division) generally uses this enough

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

How to understand memory:

The most annoying thing to write questions is to find code templates, so we should make these common codes printed in our hearts!

In fact, it is the repeated transposition of two numbers and mutual remainder. When b is 0, a is the maximum common factor.

For example, calculate the maximum common factor of 8 and 12, gcd(8,12),gcd(12,8), gcd(8,4),gcd(4,0).

It's easy to write it down once you deduce it. Euclidean algorithm is still an excellent algorithm with complexity O (logn).

(2) stein algorithm (if you can't solve problem A, use this to improve it)

Aiming at the defect that Euclidean algorithm needs trial quotient to increase the operation time when calculating large integers, an improved algorithm is proposed.

ll gcd(ll u,ll v)
{
    if (u == 0) return v;
    if (v == 0) return u;
    if (~u & 1) 
    {
        if (v & 1) 
            return gcd(u >> 1, v);
        else 
            return gcd(u >> 1, v >> 1) << 1;
    }
    if (~v & 1) 
        return gcd(u, v >> 1);
    if (u > v)
        return gcd((u - v) >> 1, v);
    return gcd((v - u) >> 1, u);
}

Directly paste the code, because it is not commonly used, it is not understood, and there are few opportunities to use it.

3. Least common multiple.

The algorithm of finding the least common multiple of two numbers is based on gcd algorithm.

int lcm(int a,int b){
	return a/gcd(a,b)*b;
}

How to understand memory:

This is still relatively simple, that is, remove the result obtained by the maximum common factor from one number and multiply it by another number to obtain the minimum common multiple of the two numbers.

4. Find the sum of all factors of a number.

(including one, not including itself) the explanation of the algorithm is in the code, which is easy to understand.

int factorSum(int n){
	    int sum=0;
    for (int i=1;i*i<=n;i++){//Halve to reduce complexity
        if (n%i==0){
            if (i==1||i*i==n) sum+=i;//If the factor is 1 or just the square root of the number, just add one.
            else	sum+=i+n/i;//Add the factor and the correlation factor of the factor.
        }
    }
    return sum;
}
    

 

5. Prime judgment

(1) In general, simple judgment prime code.

bool is_prime(int n){
	if(n<=1) return false;
	for(int i=2;i*i<=n;i++)
	   if(n%i==0) return false;
	return true; 
}

This is easy to understand. As long as there is a factor other than 1, it directly returns false, otherwise it returns true

Complexity O (root n). The time complexity of this template has no problem in judging the number within the 12th power of 10

(2) Faster method

The comparison between the two methods is not obvious when the data is not large, and the superiority of method 2 can be reflected when the data is large. The code is as follows:

bool isPrime(int a) {
    if(a == 1) return 0;
    if(a == 2 || a == 3) return 1;
    if(a%6 != 1 && a%6 != 5) return 0;
    int sqrtA = sqrt(a);
    for(int i = 5; i <= sqrtA; i += 6) {
        if(!(a%i)|| !(a%(i+2))) return 0;
    }
    return 1;
}

How to understand memory:

The process of understanding this is relatively long. [determination of prime numbers - from violence to efficiency] - C + + - Fishing sauce - blog Garden (cnblogs.com)

You can go to the blog park to see this more detailed blog and move it.

See here, thank you for watching. After writing for so long, give me a praise. Hey, hey, see you next time.

Posted by waq2062 on Tue, 02 Nov 2021 07:38:42 -0700