[Algorithm / template] prime factorization of

[Algorithm / template] prime factorization of

First, the fundamental theorem of prime factorization of

\ (\ forall N \ in ( 1, \ infty) \) can be decomposed into a product of a finite number of unique prime number, can be written as:
\ [^ {N P_1 = ^ {P_2 c_1 and c_2}} ... {^ P_m C_m } \]
where \ (C_i \) is a positive integer, \ (P_i \) are prime numbers, satisfying \ (P_1 \ P_2 lt \ ... lt \ P_m lt \) .

We can scan ~ 2 \ (\ sqrt N \) number of each of the number k, if k divisible by N, then we remove all the factor k from N, while the accumulated removed k.
Because a certain number of factors before scanning bonded to the composite number N, it is removed from, and therefore to scan a certain number divisible N is a prime number.
Note that, when N is not the final [2, \ (\ sqrt N \) divisible], then N is a prime number, it is directly integrated.
In summary, prime factor decomposition of the time complexity is \ (O (\ sqrt N) \) .

Second, the template - prime factor decomposition

code show as below:

inline void Divide(){
    cnt=0;
    for(int i=2;i<=sqrt(n);i++){
        if(n%i==0){
            p[++cnt]=i;
            while(n%i==0){
                num[cnt]++;
                n/=i;
            }
        }
        if(n>1){
            p[++cnt]=n;num[cnt]=1;
        }
    }
}//其中p为底数,num为对应的指数 

Guess you like

Origin www.cnblogs.com/cyanigence-oi/p/11716934.html