結果

問題 No.2266 Fractions (hard)
ユーザー sotanishysotanishy
提出日時 2023-04-08 00:43:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 958 ms / 6,000 ms
コード長 3,925 bytes
コンパイル時間 2,640 ms
コンパイル使用メモリ 210,560 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 11:57:24
合計ジャッジ時間 13,477 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
6,812 KB
testcase_01 AC 480 ms
6,944 KB
testcase_02 AC 269 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 8 ms
6,944 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 33 ms
6,944 KB
testcase_07 AC 15 ms
6,940 KB
testcase_08 AC 4 ms
6,944 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 8 ms
6,944 KB
testcase_13 AC 400 ms
6,944 KB
testcase_14 AC 104 ms
6,940 KB
testcase_15 AC 119 ms
6,944 KB
testcase_16 AC 370 ms
6,944 KB
testcase_17 AC 308 ms
6,944 KB
testcase_18 AC 461 ms
6,940 KB
testcase_19 AC 591 ms
6,940 KB
testcase_20 AC 96 ms
6,940 KB
testcase_21 AC 479 ms
6,944 KB
testcase_22 AC 244 ms
6,940 KB
testcase_23 AC 135 ms
6,940 KB
testcase_24 AC 139 ms
6,940 KB
testcase_25 AC 708 ms
6,940 KB
testcase_26 AC 140 ms
6,944 KB
testcase_27 AC 505 ms
6,940 KB
testcase_28 AC 142 ms
6,940 KB
testcase_29 AC 137 ms
6,944 KB
testcase_30 AC 133 ms
6,944 KB
testcase_31 AC 132 ms
6,944 KB
testcase_32 AC 387 ms
6,940 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 650 ms
6,944 KB
testcase_35 AC 683 ms
6,944 KB
testcase_36 AC 958 ms
6,944 KB
testcase_37 AC 594 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
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;
}

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};
}

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

template <typename F>
std::pair<std::pair<long long, long long>, std::pair<long long, long long>>
stern_brocot_tree_search(long long n, F cond) {
    long long a = 0, b = 1, c = 1, d = 0;
    while (true) {
        long long num = a + c, den = b + d;
        if (num > n || den > n) break;
        if (cond(num, den)) {
            long long k = 2;
            while ((num = a + c * k) <= n && (den = b + d * k) <= n &&
                   cond(num, den))
                k *= 2;
            k /= 2;
            a = a + c * k;
            b = b + d * k;
        } else {
            long long k = 2;
            while ((num = a * k + c) <= n && (den = b * k + d) <= n &&
                   !cond(num, den))
                k *= 2;
            k /= 2;
            c = a * k + c;
            d = b * k + d;
        }
    }
    return {{a, b}, {c, d}};
}

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

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

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

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

    auto count = [&](ll num, ll den) {
        ll cnt = 0;
        for (auto [l, r] : qr) {
            cnt += (mertens(r - 1) - mertens(l - 1)) *
                   floor_sum(N / l, den, num, num);
        }
        return cnt;
    };

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

    auto check = [&](ll num, ll den) { return count(num, den) < K; };

    auto [num, den] = stern_brocot_tree_search(N, check).second;

    if (flip) swap(num, den);
    cout << num << "/" << den << endl;
}
0