結果

問題 No.2645 Sum of Divisors?
ユーザー KKT89
提出日時 2024-11-05 01:48:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 277 ms / 2,000 ms
コード長 1,635 bytes
コンパイル時間 2,643 ms
コンパイル使用メモリ 219,380 KB
最終ジャッジ日時 2025-02-25 02:17:45
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

typedef long double ld;
ld Harmonic(ll n) {
    static vector<ld> dp;
    if (dp.empty()) {
        dp.resize(1000);
        for (int i = 1; i < 1000; ++i) {
            dp[i] = dp[i - 1] + ld(1) / i;
        }
    }
    if (n < 1000) return dp[n];
    return logl(n) + 0.57721566490153286060651209 + ld(1) / (2LL * n) - ld(1) / 12 / n / n + ld(1) / 120 / n / n / n / n;
}

// {x,{l,r}} : n/i = x (l <= i <= r) を{l,r}の昇順に返す
template <typename T> vector<pair<T, pair<T, T>>> quotient_range(T n) {
    vector<pair<T, pair<T, T>>> res;
    T m = 1;
    while (m * m <= n) {
        res.push_back({n / m, {m, m}});
        m += 1;
    }
    for (int i = m; i >= 1; --i) {
        T l = n / (i + 1) + 1;
        T r = n / i;
        if (l <= r and res.back().second.second < l) res.push_back({n / l, {l, r}});
    }
    return res;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    ll n;
    cin >> n;
    auto q = quotient_range(n);
    ld res = 0;
    for (auto [x, lr] : q) {
        ld h1 = Harmonic(lr.second);
        ld h2 = Harmonic(lr.first - 1);
        ld hx = Harmonic(x);
        res += (h1 - h2) * hx;
    }
    cout << fixed << setprecision(10) << res << '\n';
}
0