結果

問題 No.2262 Fractions
ユーザー jianglyjiangly
提出日時 2023-04-07 22:22:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 294 ms / 2,000 ms
コード長 2,722 bytes
コンパイル時間 2,196 ms
コンパイル使用メモリ 206,884 KB
実行使用メモリ 9,280 KB
最終ジャッジ日時 2023-08-18 00:31:45
合計ジャッジ時間 11,874 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
9,028 KB
testcase_01 AC 59 ms
9,152 KB
testcase_02 AC 58 ms
9,032 KB
testcase_03 AC 58 ms
9,188 KB
testcase_04 AC 64 ms
9,276 KB
testcase_05 AC 59 ms
9,160 KB
testcase_06 AC 66 ms
9,096 KB
testcase_07 AC 63 ms
9,112 KB
testcase_08 AC 60 ms
9,024 KB
testcase_09 AC 66 ms
9,152 KB
testcase_10 AC 65 ms
9,148 KB
testcase_11 AC 189 ms
9,280 KB
testcase_12 AC 190 ms
9,184 KB
testcase_13 AC 188 ms
9,212 KB
testcase_14 AC 192 ms
9,036 KB
testcase_15 AC 184 ms
9,028 KB
testcase_16 AC 65 ms
9,024 KB
testcase_17 AC 65 ms
9,036 KB
testcase_18 AC 65 ms
9,212 KB
testcase_19 AC 276 ms
9,152 KB
testcase_20 AC 245 ms
9,024 KB
testcase_21 AC 265 ms
9,192 KB
testcase_22 AC 216 ms
9,212 KB
testcase_23 AC 223 ms
9,232 KB
testcase_24 AC 214 ms
9,068 KB
testcase_25 AC 255 ms
9,088 KB
testcase_26 AC 294 ms
9,028 KB
testcase_27 AC 272 ms
9,088 KB
testcase_28 AC 277 ms
9,116 KB
testcase_29 AC 278 ms
9,200 KB
testcase_30 AC 248 ms
9,192 KB
testcase_31 AC 270 ms
9,280 KB
testcase_32 AC 279 ms
9,184 KB
testcase_33 AC 263 ms
9,184 KB
testcase_34 AC 233 ms
9,092 KB
testcase_35 AC 165 ms
9,036 KB
testcase_36 AC 164 ms
9,032 KB
testcase_37 AC 9 ms
9,156 KB
testcase_38 AC 9 ms
9,200 KB
testcase_39 AC 209 ms
9,244 KB
testcase_40 AC 233 ms
9,152 KB
testcase_41 AC 247 ms
9,112 KB
testcase_42 AC 248 ms
9,164 KB
testcase_43 AC 195 ms
9,200 KB
testcase_44 AC 278 ms
9,036 KB
testcase_45 AC 248 ms
9,116 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