Integer factor decomposition problem

Source: Internet
Author: User
Title Description

A positive integer greater than 1 n can be decomposed into: N=X1*X2*...*XM.
For example, when n=12, there are 8 different types of decomposition:
12=12;
12=6*2;
12=4*3;
12=3*4;
12=3*2*2;
12=2*6;
12=2*3*2;
12=2*2*3.

For a given positive integer n, there are a number of different decomposition types for programming n. Input

The first row of each set of test data has 1 positive integer n (n<=10^10). Output

Will calculate the different number of exploded output. Sample input

12
Sample output
8


If you use a search, you need to use a memory search, save with a map, and then optimize to sqrt (n)

can be begged out.

Another kind of dynamic programming,

12 of the factors are

1 2 3 4 6 12

1 1 1 2 3 8

Then you just need to decompose the factor on the line.

#include <stdio.h>
#include <map>
using namespace std;
Map < int, int > vis;
int dfs (int n)
{
    if (n = = 1)
        return 1;
    if (Vis[n])   //Search over
        return vis[n];
    int s = 1;
    for (int i = 2; i*i <= N; i + +)   //optimized to SQRT (n)
        if (n%i = = 0)//factor
        {
            s = S+dfs (i);
            if (i! = n/i)//Unequal search for the next layer
                s = S+dfs (n/i);
        }
    Vis[n] = s;
    return s;
}
int main ()
{
    int n;
    scanf ("%d", &n);
    printf ("%d", DFS (n));
    return 0;
}


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.