AcWing 1294. 樱花

题目链接

因为要满足 \(\frac{1}{x} + \frac{1}{y}=\frac{1}{n!}\) 这个式子,所以很显然 \(x > n!, y > n!\),那么我们假设 \(y=n!+k(k\in N^*)\)

那么代入式子得 \(\frac{1}{x}+\frac{1}{n!+k}=\frac{1}{n!}\)

化简得 \(\frac{n!^2}{k}+n!\cdot k=x\)

显然只需要求出左边的数 \(\frac{n!^2}{k}\) 有多少种情况就行,即约数个数的求法。

考虑到时间复杂度,如果不用线性筛会 TLE ,所以用线性筛求出它的最小质因子(虽然说也可以是这个数其它的质因子),然后再倒推。

时间复杂度 \(O(n)\)

\(\mathscr{Code:}\)

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a, END##i = b; i <= END##i; i++)
#define per(i, a, b) for (int i = a, END##i = b; i >= END##i; i--)
#define DEBUG(x) cerr << #x << " = " << x << '\n'
using LL = long long;
using ULL = unsigned long long;
typedef pair<int, int> PII;

inline LL read() {
    LL s = 0, fu = 1; char ch = getchar();
    while (ch < '0' || ch > '9') ch == '-' ? fu = -1 : 0, ch = getchar();
    while (ch >= '0' && ch <= '9') s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
    return s * fu;
}

const int Mod = 1e9 + 7;
const int Inf = 0x3f3f3f3f;
const LL InfLL = 0x3f3f3f3f3f3f3f3f;

const int N = 1e6 + 10;
int n, primes[N], cnt;
bool vis[N];
LL mp[N], ans = 1, t[N];

void get_primes(int n) { // 求出每个数的最小质因子
    rep(i, 2, n) {
        if (!vis[i]) primes[++cnt] = i, mp[i] = i;
        for (int j = 1; primes[j] <= n / i; j++) {
            vis[i * primes[j]] = true;
            mp[i * primes[j]] = primes[j];
            if (i % primes[j] == 0) break;
        }
    }
}

int main() {
    n = read();
    get_primes(n);
    rep(i, 1, n) t[i] = 2;
    per(i, n, 2) {
        if (!vis[i]) continue;
        t[mp[i]] += t[i];
        t[i / mp[i]] += t[i];
        t[i] = 0;
    }
    rep(i, 2, n) ans = ans * (t[i] + 1) % Mod;
    printf("%lld\n", ans);
    return 0;
}
posted @ 2025-05-04 08:41  wh2011  阅读(4)  评论(0)    收藏  举报