码迷,mamicode.com
首页 > 其他好文 > 详细

整数因子分解问题

时间:2019-11-25 20:11:06      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:time   limit   sqrt   相等   enter   main   map   namespace   等于   

整数因子分解问题

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

大于1的正整数n可以分解为:n=x1*x2*…*xm。例如,当n=12 时,共有8 种不同的分解式:
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。
对于给定的正整数n,计算n共有多少种不同的分解式。

Input

输入数据只有一行,有1个正整数n (1≤n≤2000000000)。

Output

将计算出的不同的分解式数输出。

Sample Input

12

Sample Output

8

Hint

 

Source

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 map<int,int> mp;
 4 int fun(int n){
 5     if(mp[n]) return mp[n];//如果这个数已经计算过了,可以直接返回
 6     if(n==1) return 1;
 7     int sum=1;//个数为1
 8     for(int i=2;i<=sqrt(n);i++){// 等于 sqrt(n);
 9         if(n%i==0){//需成为 因子 才可以继续计算
10              sum+=fun(i);
11              if(i!=n/i) sum+=fun(n/i);//若i== n/i 那么 只算一次,不相等,两边都算
12         }
13 
14     }
15     mp[n]=sum;//跟新mp[n]
16     return sum;
17 }
18 int main()
19 {
20     int n;
21     cin>>n;
22     cout<<fun(n)<<endl;
23     return  0;
24 }

 

整数因子分解问题

标签:time   limit   sqrt   相等   enter   main   map   namespace   等于   

原文地址:https://www.cnblogs.com/NirobertEinteson/p/11930054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!