分解因数

|DFS|递归|

本题的dfs特点在于搜索的空间是动态的,因此需要找到可以利用到限制下一步递归的条件来进行空
间范围的缩小与框定。本题利用的是分解的最小因数,可以对下一步的遍历框定范围

#include<iostream>
#include<algorithm>

using namespace std;

int a;

int factor(int t,int m){
    //t为目标数,m为最小数
    int res = 0;
    for(int i=m;i*i<=t;i++){
        if(t%i==0){
            res += factor(t/i,i);
            //由于强调是非递减序列
        }
    }
    res+=1;    //条件允许自己本身为一种分解
    return res;
}

int main(){
    int n;
    cin>>n;
    while (n--)
    {
        cin>>a;
        int ans = factor(a,2);
        cout<<ans<<endl;
    }
    return 0;
}
posted @ 2025-11-17 22:11  channy_zheng  阅读(8)  评论(0)    收藏  举报