【题目描述】
给出一个正整数a,要求分解成若干个正整数的乘积,即a=a1×a2×a3×…×an,并且1<a1≤a2≤a3≤…≤an,问这样的分解的种数有多少。注意到a=a也是一种分解。
【输入】
第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个正整数a(1<a<32768)。
【输出】
n行,每行输出对应一个输入。输出应是一个正整数,指明满足要求的分解的种数。
【输入样例】
2
2
20
【输出样例】
1
4
【方法1】
#include<bits/stdc++.h>
using namespace std;
int tot;
int t=2;
void dfs(int x){
if(x == 1){
tot++;
return;
}
for(int i=t;i<=x;i++){
if(x%i == 0){
t = i;
dfs(x/i);
}
}
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
int x;
cin>>x;
tot = 0;
t=2;
dfs(x);
cout<<tot<<endl;
}
}
【方法2】
#include<bits/stdc++.h>
using namespace std;
int a[100];
int ans;
void dfs(int x,int step){
if(x == 1){
ans++;
// for(int j=1;j<step;j++){
// cout<<a[j]<<" ";
// }
// cout<<endl;
}
for(int i=a[step-1];i<=x;i++){
if(x%i == 0){
a[step++] = i;
dfs(x/i,step);
step -- ;
}
}
}
int main(){
int n;
a[0] = 2;
cin>>n;
dfs(n,1);
cout<<endl<<ans;
}