[Divide and conquer algorithm] factorization

Integer factorization problem

http://acm.sdut.edu.cn/onlinejudge2.dev/index.php/Home/Index/problemdetail/pid/1722.html

Time Limit: 1000 ms Memory Limit: 65536 KiB
 
 

Problem Description

The positive integer n greater than 1 can be decomposed as: n = x1 * x2 * ... * xm. For example, when n = 12, the total of 8 different decomposition formula:
12 = 12;
12 = 6 * 2;
12 = 4 * 3;
12 = 3 * 4;
12 = 3 * 2 * 2;
12 = 2 * 6;
12 = 2 * 2 * 3;
12 = 2 * 2 * 3.
For a given positive integer n, the number of different n calculated total decomposition.

Input

Only one line of input data, there is a positive integer n (1≤n≤2000000000).

Output

The calculated number of different factorization output.

Sample Input

12

Sample Output

8

Algorithm a (time-out) 

algorithm ideas:

 

   Such as in Example 12, the case 1) and case 2) should be calculated in the Count but Case 2) based on the case 1) is generated. Thus recursively, each function i traversed again, if the temp / i == 0, indicating that the number of layers may be decomposed recursively to the next level.

Code:

. 1 #include <the iostream>
 2 #include <algorithm>
 . 3  the using  namespace STD;
 . 4  
. 5  int the Count;
 . 6  
. 7  // calculates the integer factorization problem 
. 8  void FUNC ( int TEMP) {
 . 9      
10      for ( int I = 2 ; I <TEMP; I ++ ) {
 . 11          IF (TEMP% I == 0 ) {
 12 is              the Count ++ ;
 13 is              FUNC (TEMP / I);
 14          }
 15      }
 16 }
17 
18 int main() {
19 
20     Count = 0;
21     int temp;
22     cin >> temp;
23     func(temp);
24     cout << Count+1 << endl;
25 }

Two algorithms (optimization)

Algorithm for a Drawbacks: We seek (i, temp / i) If the number factorization temp / i, the calculation is repeated (see below)

 

 

A policy resolution algorithm: we use a factor of 6 for storing digital array directly, if it is found that there are several arr [6], the direct use to avoid double counting.

Algorithms ideas:

Still recursive, T = I * J, the count (t) = count (i ) + count (j); in order to avoid double counting (t = i * j = j * i), should be limited to i <j, i.e., for (i; i <sqrt (t ); i ++).

 

 

Guess you like

Origin www.cnblogs.com/onetrainee/p/11665864.html