整數因子分解問題
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
代碼:
#include<bits/stdc++.h>
using namespace std;
int sum = 1;
void solve(int n)
{
for(int i = 2; i <= sqrt(n); i++)
{
if(n % i == 0)
{
solve(i);
if(i * i == n)
{
sum++;
}
else
{
sum += 2;
solve(n / i);
}
}
}
}
int main()
{
int n;
cin >> n;
solve(n);
cout << sum << endl;
return 0;
}