CF1542C Strange Function

· · 题解

算法分析

注意到,f(k) = x 意味着 1, 2, \cdots, x - 1 都是 k 的因子,所以我们可以得到 {\rm lcm}(1, \cdots, x - 1) \mid k,而 x \nmid k,那么在 f(1) \sim f(n) 中使得 f(k) = xk 的个数是 \lfloor \frac{n}{{\rm lcm}(1, \cdots, x - 1)}\rfloor - \lfloor \frac{n}{{\rm lcm}(1, \cdots, x)}\rfloor。 然后计算不同的 f(k) 对答案的贡献即可。

C++ 代码

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using std::cin;
using std::cout;
using std::istream;
using std::ostream;
using std::vector;
using ll = long long;

// const int mod = 998244353;
const int mod = 1000000007;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod) {}
    mint operator-() const {
        return mint(-x);
    }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        return mint(*this) += a;
    }
    mint operator-(const mint a) const {
        return mint(*this) -= a;
    }
    mint operator*(const mint a) const {
        return mint(*this) *= a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return *this *= a.inv();
    }
    mint operator/(const mint a) const {
        return mint(*this) /= a;
    }
};
istream& operator>>(istream& is, mint& a) {
    return is >> a.x;
}
ostream& operator<<(ostream& os, const mint& a) {
    return os << a.x;
}

int main() {
    int t;
    cin >> t;

    while (t--) {
        ll n;
        cin >> n;
        vector<ll> a(2, 1);
        while (a.back() <= n) {
            ll next = std::lcm(a.back(), a.size());
            a.push_back(next);
        }
        mint ans;
        for (auto x : a) ans += n / x;
        cout << ans << '\n';
    }

    return 0;
}