結果

問題 No.2266 Fractions (hard)
ユーザー sotanishysotanishy
提出日時 2023-04-07 23:09:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,656 bytes
コンパイル時間 5,030 ms
コンパイル使用メモリ 264,684 KB
実行使用メモリ 10,620 KB
最終ジャッジ日時 2024-04-14 11:56:14
合計ジャッジ時間 12,812 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
6,812 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T>
bool chmax(T& a, const T& b) {
    return a < b ? (a = b, 1) : 0;
}
template <typename T>
bool chmin(T& a, const T& b) {
    return a > b ? (a = b, 1) : 0;
}

template <typename T>
std::vector<std::pair<T, T>> quotient_ranges(T n) {
    std::vector<std::pair<T, T>> ret;
    T i = 1;
    while (i <= n) {
        T q = n / i;
        T j = n / q + 1;
        ret.emplace_back(i, j);
        i = j;
    }
    return ret;
}

template <typename mint>
std::pair<std::vector<mint>, std::vector<mint>> totient_summatory_table(
    long long n) {
    if (n == 0) return {{0}, {0}};
    const int b = std::min(n, (long long)1e4);
    std::vector<mint> small(n / b + 1), large(b + 1);

    std::vector<int> totient(n / b + 1);
    std::iota(totient.begin(), totient.end(), 0);
    for (int i = 2; i <= n / b; ++i) {
        if (totient[i] != i) continue;
        for (int j = i; j <= n / b; j += i) {
            totient[j] = totient[j] / i * (i - 1);
        }
    }
    for (int i = 0; i < n / b; ++i) small[i + 1] = small[i] + totient[i + 1];

    for (int i = 1; i <= b; ++i) {
        mint k = n / i;
        large[i] = k * (k + 1) / 2;
    }
    for (long long i = b; i >= 1; --i) {
        for (long long l = 2; l <= n / i;) {
            long long q = n / (i * l), r = n / (i * q) + 1;
            large[i] -=
                (i * l <= b ? large[i * l] : small[n / (i * l)]) * (r - l);
            l = r;
        }
    }
    return {small, large};
}

std::pair<std::vector<int>, std::vector<int>> mertens_table(long long n) {
    if (n == 0) return {{0}, {0}};
    const int b = 1e4;
    std::vector<int> small(n / b + 1, 1), large(b + 1, 1);
    small[0] = large[0] = 0;

    std::vector<bool> prime(n / b + 1, true);
    for (int i = 2; i <= n / b; ++i) {
        if (!prime[i]) continue;
        for (int j = i; j <= n / b; j += i) {
            if (j > i) prime[j] = false;
            if ((j / i) % i == 0)
                small[j] = 0;
            else
                small[j] *= -1;
        }
    }
    for (int i = 1; i < n / b; ++i) small[i + 1] += small[i];

    for (long long i = b; i >= 1; --i) {
        for (long long l = 2; l <= n / i;) {
            long long q = n / (i * l), r = n / (i * q) + 1;
            large[i] -=
                (i * l <= b ? large[i * l] : small[n / (i * l)]) * (r - l);
            l = r;
        }
    }
    return {small, large};
}

using i128 = __int128_t;

i128 floor_sum(i128 n, i128 m, i128 a, i128 b) {
    i128 sum = 0;
    if (a >= m) {
        sum += (a / m) * n * (n - 1) / 2;
        a %= m;
    }
    if (b >= m) {
        sum += (b / m) * n;
        b %= m;
    }
    i128 y = (a * n + b) / m;
    if (y == 0) return sum;
    i128 x = (m * y - b + a - 1) / a;
    sum += (n - x) * y + floor_sum(y, a, m, a * x - m * y + b);
    return sum;
}

std::vector<int> moebius_table(int n) {
    std::vector<bool> prime(n + 1, true);
    std::vector<int> ret(n + 1, 1);
    for (int i = 2; i <= n; ++i) {
        if (!prime[i]) continue;
        for (int j = i; j <= n; j += i) {
            if (j > i) prime[j] = false;
            if ((j / i) % i == 0) ret[j] = 0;
            else ret[j] *= -1;
        }
    }
    return ret;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N;
    ll K;
    cin >> N >> K;

    ll total = totient_summatory_table<ll>(N).second[1];

    if (K >= 2 * total) {
        cout << -1 << endl;
        return 0;
    }
    bool flip = false;
    if (K > total) {
        K = 2 * total - K;
        flip = true;
    }

    auto qr = quotient_ranges(N);
    vector<int> small, large;
    tie(small, large) = mertens_table(N);

    auto mertens = [&](ll n) -> ll {
        if (n < small.size()) return small[n];
        return large[N / n];
    };

    const i128 D = 1e16 + 1;
    i128 lb = 0, ub = D + 1;
    while (ub - lb > 1) {
        ll m = (lb + ub) / 2;
        ll cnt = 0;

        for (auto [l, r] : qr) {
            cnt += (mertens(r - 1) - mertens(l - 1)) * floor_sum(N / l, D, m, m);
        }

        if (cnt < K) {
            lb = m;
        } else {
            ub = m;
        }
    }

    rep(d, 1, N + 1) {
        if ((d * lb + D - 1) / D <= d * ub / D) {
            int n = d * ub / D;
            if (flip) swap(d, n);
            cout << n << "/" << d << endl;
            return 0;
        }
    }
}
0