結果

問題 No.2262 Fractions
ユーザー jianglyjiangly
提出日時 2023-04-07 22:22:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 2,722 bytes
コンパイル時間 2,552 ms
コンパイル使用メモリ 208,824 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2024-05-05 07:22:27
合計ジャッジ時間 10,727 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
9,344 KB
testcase_01 AC 51 ms
9,344 KB
testcase_02 AC 52 ms
9,216 KB
testcase_03 AC 55 ms
9,344 KB
testcase_04 AC 56 ms
9,216 KB
testcase_05 AC 52 ms
9,344 KB
testcase_06 AC 59 ms
9,216 KB
testcase_07 AC 60 ms
9,344 KB
testcase_08 AC 54 ms
9,344 KB
testcase_09 AC 58 ms
9,344 KB
testcase_10 AC 60 ms
9,344 KB
testcase_11 AC 176 ms
9,344 KB
testcase_12 AC 194 ms
9,344 KB
testcase_13 AC 175 ms
9,216 KB
testcase_14 AC 181 ms
9,344 KB
testcase_15 AC 171 ms
9,344 KB
testcase_16 AC 61 ms
9,216 KB
testcase_17 AC 71 ms
9,344 KB
testcase_18 AC 59 ms
9,344 KB
testcase_19 AC 252 ms
9,216 KB
testcase_20 AC 220 ms
9,216 KB
testcase_21 AC 243 ms
9,344 KB
testcase_22 AC 195 ms
9,344 KB
testcase_23 AC 203 ms
9,344 KB
testcase_24 AC 201 ms
9,344 KB
testcase_25 AC 232 ms
9,216 KB
testcase_26 AC 279 ms
9,344 KB
testcase_27 AC 254 ms
9,344 KB
testcase_28 AC 254 ms
9,344 KB
testcase_29 AC 254 ms
9,344 KB
testcase_30 AC 241 ms
9,216 KB
testcase_31 AC 249 ms
9,344 KB
testcase_32 AC 254 ms
9,216 KB
testcase_33 AC 241 ms
9,216 KB
testcase_34 AC 213 ms
9,344 KB
testcase_35 AC 150 ms
9,344 KB
testcase_36 AC 153 ms
9,344 KB
testcase_37 AC 9 ms
9,216 KB
testcase_38 AC 8 ms
9,216 KB
testcase_39 AC 192 ms
9,344 KB
testcase_40 AC 215 ms
9,344 KB
testcase_41 AC 229 ms
9,344 KB
testcase_42 AC 235 ms
9,344 KB
testcase_43 AC 182 ms
9,344 KB
testcase_44 AC 259 ms
9,344 KB
testcase_45 AC 232 ms
9,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i64 = long long;
std::vector<int> minp, primes, phi, mu;
std::vector<i64> sphi;

void sieve(int n) {
    minp.assign(n + 1, 0);
    phi.assign(n + 1, 0);
    sphi.assign(n + 1, 0);
    mu.assign(n + 1, 0);
    primes.clear();
    phi[1] = 1;
    mu[1] = 1;
    
    for (int i = 2; i <= n; i++) {
        if (minp[i] == 0) {
            minp[i] = i;
            phi[i] = i - 1;
            mu[i] = -1;
            primes.push_back(i);
        }
        
        for (auto p : primes) {
            if (i * p > n) {
                break;
            }
            minp[i * p] = p;
            if (p == minp[i]) {
                phi[i * p] = phi[i] * p;
                break;
            }
            phi[i * p] = phi[i] * (p - 1);
            mu[i * p] = -mu[i];
        }
    }
    
    for (int i = 1; i <= n; i++) {
        sphi[i] = sphi[i - 1] + phi[i];
        mu[i] += mu[i - 1];
    }
}

std::pair<i64, i64> get(int N, int x, int y) {
    if (x > y) {
        auto [lt, le] = get(N, y, x);
        std::tie(lt, le) = std::pair(2 * sphi[N] - 1 - le, 2 * sphi[N] - 1 - lt);
        return {lt, le};
    }
    i64 le = 0, lt;
    for (int i = 1; i <= N; i++) {
        le += 1LL * x * i / y * mu[N / i];
    }
    lt = le - (x <= N && y <= N && std::gcd(x, y) == 1);
    return {lt, le};
}

void solve() {
    int N;
    i64 K;
    std::cin >> N >> K;
    
    K--;
    
    if (K >= 2 * sphi[N] - 1) {
        std::cout << -1 << "\n";
        return;
    }
    
    int lox = 0, loy = 1, hix = 1, hiy = 0;
    while (true) {
        auto [lt, le] = get(N, lox + hix, loy + hiy);
        if (lt <= K && le > K) {
            std::cout << lox + hix << "/" << loy + hiy << "\n";
            return;
        }
        if (le <= K) {
            int t = 1;
            while (get(N, lox + hix * t, loy + hiy * t).second <= K) {
                t *= 2;
            }
            while (t > 1) {
                t /= 2;
                if (get(N, lox + hix * t, loy + hiy * t).second <= K) {
                    lox += hix * t;
                    loy += hiy * t;
                }
            }
        } else {
            int t = 1;
            while (get(N, lox * t + hix, loy * t + hiy).first > K) {
                t *= 2;
            }
            while (t > 1) {
                t /= 2;
                if (get(N, lox * t + hix, loy * t + hiy).first > K) {
                    hix += lox * t;
                    hiy += loy * t;
                }
            }
        }
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    sieve(3E5);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}
0