整数因子分解

#include<bits/stdc++.h>
using namespace std;
const int N = 5e5;
const int M = 2e4 + 10;
const int INF = 0x3f3f3f3f;
typedef long long LL;
/*
记忆化递归 s存储x分解式的数量
不必过大,这里是5e5
关键理解:
以12为例,i,12/i。因为这里的因子是有顺序的,那么以i开头的因式
数量就是 1* 12/i的因数数量。

*/
int s[N];
LL f(int x) {
    LL ans = 1;
    if (x < N && s[x] != 0) {
        return s[x];
    }
    for (int i = 2; i <= sqrt(x); i++) {
        if (x % i == 0) {
            ans += f(i);
            if (i * i != x) {
                ans += f(x / i);
            }
        }
    }
    if (x < N) s[x] = ans;
    return ans;
}
int main()
{
    int n;
    cin >> n;
    cout << f(n) << endl;
    return 0;
}

posted on 2025-09-28 16:58  Hoshino1  阅读(6)  评论(0)    收藏  举报