結果

問題 No.2262 Fractions
ユーザー karinohitokarinohito
提出日時 2023-03-11 12:00:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 690 ms / 2,000 ms
コード長 2,170 bytes
コンパイル時間 2,954 ms
コンパイル使用メモリ 208,488 KB
実行使用メモリ 13,248 KB
最終ジャッジ日時 2023-08-18 00:26:12
合計ジャッジ時間 23,682 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 389 ms
6,880 KB
testcase_01 AC 90 ms
4,376 KB
testcase_02 AC 86 ms
4,376 KB
testcase_03 AC 87 ms
4,376 KB
testcase_04 AC 88 ms
4,376 KB
testcase_05 AC 86 ms
4,380 KB
testcase_06 AC 90 ms
4,376 KB
testcase_07 AC 88 ms
4,376 KB
testcase_08 AC 86 ms
4,380 KB
testcase_09 AC 90 ms
4,376 KB
testcase_10 AC 89 ms
4,380 KB
testcase_11 AC 256 ms
4,376 KB
testcase_12 AC 252 ms
4,376 KB
testcase_13 AC 257 ms
4,376 KB
testcase_14 AC 259 ms
4,376 KB
testcase_15 AC 253 ms
4,376 KB
testcase_16 AC 194 ms
4,384 KB
testcase_17 AC 193 ms
4,376 KB
testcase_18 AC 190 ms
4,376 KB
testcase_19 AC 540 ms
6,820 KB
testcase_20 AC 501 ms
6,572 KB
testcase_21 AC 547 ms
6,988 KB
testcase_22 AC 503 ms
6,612 KB
testcase_23 AC 414 ms
5,720 KB
testcase_24 AC 663 ms
9,064 KB
testcase_25 AC 681 ms
9,428 KB
testcase_26 AC 661 ms
9,216 KB
testcase_27 AC 663 ms
9,060 KB
testcase_28 AC 662 ms
9,072 KB
testcase_29 AC 686 ms
9,236 KB
testcase_30 AC 671 ms
9,288 KB
testcase_31 AC 688 ms
9,284 KB
testcase_32 AC 675 ms
9,124 KB
testcase_33 AC 690 ms
9,360 KB
testcase_34 AC 666 ms
9,200 KB
testcase_35 AC 645 ms
13,248 KB
testcase_36 AC 645 ms
12,760 KB
testcase_37 AC 442 ms
4,376 KB
testcase_38 AC 443 ms
4,380 KB
testcase_39 AC 529 ms
6,316 KB
testcase_40 AC 532 ms
6,528 KB
testcase_41 AC 540 ms
6,336 KB
testcase_42 AC 543 ms
6,312 KB
testcase_43 AC 529 ms
6,264 KB
testcase_44 AC 673 ms
9,124 KB
testcase_45 AC 687 ms
9,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
ll Eulers_phi(ll n) {
    ll res = 0;
    ll N = n;
    vll P;
    for (ll p = 2; p * p <= N; p++) {
        if (N % p == 0) {
            P.push_back(p);
            while (N % p == 0)N /= p;
        }
    }
    if (N != 1)P.push_back(N);

    N = P.size();
    for(ll bit=0;bit< (1 << N);bit++) {
        ll k = 1, t = 0;
        for(ll i=0;i<N;i++) {
            if (bit & (1 << i)) {
                k *= P[i];
                t++;
            }
        }
        res += (t % 2 == 0 ? 1 : -1) * n / k;
    }
    return res;
}

ll gcd(ll a, ll b) {
    if (b == 0)return a;
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}

ll num(ll p,ll N){//p/N以下の既約分数
    ll S=0;
    vll F(N+1,0);
    for (ll i = 1; i <= N; i++) {
        F[i] += (p* i) / N;//分母がiで mid/N以下の(既約とは限らない)分数の個数
        for (ll j = i * 2; j <= N; j += i)F[j] -= F[i]; //既約のみを取り出すため包除
        S += F[i];
    }
    return S;
}

int main() {
    ll T;
    cin>>T;
    while(T--){
        ll N, K;
        cin >> N >> K;
        ll L = 0;
        bool INV = 0;
        for (ll i = 2; i <= N; i++)L += Eulers_phi(i);
        if (K == L + 1) {
            cout << "1/1" << endl;
            continue;
        }
        if(K>2*L+1){
            cout<<-1<<endl;
            continue;
        }
        if (K > L + 1) {
            INV = 1;
            K=2*L-K+2;
        }
        ll l = 0, r = N;
        while (r - l > 1)(num((r + l)/2,N)<=K?l:r)=(r + l) / 2;
        vll F(N + 1, 0);
        K-=num(l,N);
        vector<pair<ll,ll>> P;
        for(ll i=1;i<=N;i++){
            ll a=(l*i+N-1)/N;
            while(a*N<i*(l+1)){
                if(gcd(a,i)==1)P.push_back({a,i});
                a++;
            }
        }
        sort(P.begin(),P.end(),[](auto const& lhs, auto const& rhs) {
            return lhs.first*rhs.second < rhs.first*lhs.second;
        });

        if(INV)swap(P[K].first,P[K].second);
        cout<<P[K].first<<"/"<<P[K].second<<endl;
    }
}
0