結果

問題 No.2262 Fractions
ユーザー milanis48663220milanis48663220
提出日時 2023-04-07 23:28:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,904 ms / 2,000 ms
コード長 5,354 bytes
コンパイル時間 1,258 ms
コンパイル使用メモリ 132,200 KB
実行使用メモリ 33,132 KB
最終ジャッジ日時 2024-10-02 20:38:29
合計ジャッジ時間 60,847 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,185 ms
29,440 KB
testcase_01 AC 427 ms
28,928 KB
testcase_02 AC 426 ms
28,928 KB
testcase_03 AC 428 ms
29,056 KB
testcase_04 AC 447 ms
28,928 KB
testcase_05 AC 431 ms
29,056 KB
testcase_06 AC 458 ms
29,056 KB
testcase_07 AC 442 ms
28,928 KB
testcase_08 AC 430 ms
28,928 KB
testcase_09 AC 452 ms
28,928 KB
testcase_10 AC 450 ms
28,928 KB
testcase_11 AC 1,138 ms
29,184 KB
testcase_12 AC 1,124 ms
29,184 KB
testcase_13 AC 1,147 ms
29,312 KB
testcase_14 AC 1,147 ms
29,056 KB
testcase_15 AC 1,154 ms
29,184 KB
testcase_16 AC 665 ms
28,928 KB
testcase_17 AC 654 ms
28,928 KB
testcase_18 AC 656 ms
29,056 KB
testcase_19 AC 1,723 ms
30,960 KB
testcase_20 AC 1,616 ms
31,212 KB
testcase_21 AC 1,730 ms
31,084 KB
testcase_22 AC 1,589 ms
31,084 KB
testcase_23 AC 1,416 ms
30,056 KB
testcase_24 AC 1,828 ms
32,976 KB
testcase_25 AC 1,776 ms
31,084 KB
testcase_26 AC 1,793 ms
31,084 KB
testcase_27 AC 1,756 ms
31,084 KB
testcase_28 AC 1,754 ms
31,212 KB
testcase_29 AC 1,850 ms
33,128 KB
testcase_30 AC 1,803 ms
33,132 KB
testcase_31 AC 1,815 ms
31,084 KB
testcase_32 AC 1,754 ms
29,440 KB
testcase_33 AC 1,815 ms
31,084 KB
testcase_34 AC 1,846 ms
33,004 KB
testcase_35 AC 1,755 ms
29,312 KB
testcase_36 AC 1,809 ms
28,928 KB
testcase_37 AC 177 ms
28,928 KB
testcase_38 AC 179 ms
29,056 KB
testcase_39 AC 1,720 ms
32,364 KB
testcase_40 AC 1,696 ms
32,484 KB
testcase_41 AC 1,696 ms
32,316 KB
testcase_42 AC 1,710 ms
32,364 KB
testcase_43 AC 1,721 ms
32,360 KB
testcase_44 AC 1,863 ms
33,132 KB
testcase_45 AC 1,904 ms
31,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

/**
 * 有理数の計算用。dが分母でnが分子。
 * atcoderだとたいていmodintで足りるが、Project Eulerだとたまに使える。
 */ 
template <typename T>
class frac{
    public:
    T n, d;
    frac(T _n, T _d){
        T g = gcd(_n, _d);
        n = _n/g;
        d = _d/g;
        if(d < 0) {
            d *= -1;
            n *= -1;
        }
    }
    frac operator+(frac f){
        return frac(n*f.d+d*f.n, f.d*d);
    }
    frac operator-(frac f){
        return frac(n*f.d-d*f.n, f.d*d);
    }
    frac operator*(frac f){
        return frac(f.n*n, f.d*d);
    }
    frac inv(){
        return frac(d, n);
    }
    bool operator<(frac f){
        if(d*f.d < 0) return n*f.d-d*f.n > 0;
        else return n*f.d-d*f.n < 0;
    }
    bool operator>(frac f){
        if(d*f.d < 0) return n*f.d-d*f.n < 0;
        else return n*f.d-d*f.n > 0;
    }
    bool operator==(frac f){
        return n*f.d-d*f.n == 0;
    }
    void operator+=(frac f){
        n = n*f.d+d*f.n, d = f.d*d;
        reduction();
    }
    void operator-=(frac f){
        n = n*f.d-d*f.n, d = f.d*d;
        reduction();
    }
    void reduction(){
        T g = gcd(n, d);
        n /= g, d /= g;
    }
};

template <typename T>
bool operator<(const frac<T> f1, const frac<T> f2){
    if(f1.d*f2.d < 0) return f1.n*f2.d-f1.d*f2.n > 0;
    else return f1.n*f2.d-f1.d*f2.n < 0;
}

template <typename T>
bool operator>(const frac<T> f1, const frac<T> f2){
    if(f1.d*f2.d < 0) return f1.n*f2.d-f1.d*f2.n > 0;
    else return f1.n*f2.d-f1.d*f2.n > 0;
}

template <typename T>
bool operator==(const frac<T> f1, const frac<T> f2){
    return f1.n*f2.d-f1.d*f2.n == 0;
}

template <typename T>
ostream& operator<<(ostream& os, const frac<T>& f){
    os << f.n << '/' << f.d;
    return os;
}

class Eratosthenes{
    public:
    int m;
    vector<bool> is_prime;
    vector<int> primes;
    Eratosthenes(int m_){
        m = m_;
        init();
    }
    private:
    void init(){
        is_prime.assign(m+1, true);
        is_prime[0] = false, is_prime[1] = false;
        for(int i = 2; i <= m; i++){
            if(is_prime[i]){
                primes.push_back(i);
                for(int j = 2; i*j <= m; j++) is_prime[i*j] = false;
            }
        }
    }
};

const int N = 300000;

Eratosthenes et(N);
vector<int> facs[N+1];
int meb[N+1];

void init(){
    for(int x = 1; x <= N; x++) meb[x] = 1;
    for(int p: et.primes){
        for(int i = 1; i*p <= N; i++){
            if(i%p == 0){
                meb[i*p] = 0;
            }else{
                meb[i*p] *= -1;
            }
        }
    }
    for(int x = 1; x <= N; x++){
        if(meb[x] == 0) continue;
        for(int i = 1; i*x <= N; i++){
            facs[i*x].push_back(x);
        }
    }
}

using F = frac<ll>;
const int M = 1000000000;

void solve(){
    ll n; ll k; cin >> n >> k;
    ll sum = 0;
    // x/m以下になるやつの個数
    const ll m = 100000000;
    auto count = [&](ll x){ 
        ll ans = 0;
        for(int i = 1; i <= n; i++){
            ll tmp = 0;
            for(int j: facs[i]){
                ll r = n;
                if(x <= (n*m)/i){
                    r = min((x*i)/m, n);
                }
                tmp += (ll)meb[j]*(r/j);
            }
            ans += tmp;
        }
        return ans;
    };
    if(count(n*m) < k){
        cout << -1 << endl;
        return;
    }
    ll l = 0, r = n*m;
    while(r-l > 1){
        ll x = (l+r)/2;
        if(count(x) < k) l = x;
        else r = x;
    }
    ll cnt_r = count(r);
    vector<F> fs;
    for(int x = 1; x <= n; x++){
        // (l/m) < y/x <= (r/m)
        ll yl = (l*x)/m;
        if(yl > n) continue;
        ll yr = (r*x)/m;
        chmin(yr, n);
        for(ll y = yl; y <= yr; y++){
            if(gcd(x, y) != 1) continue;
            fs.push_back(F(y, x));
        }
    }
    sort(fs.begin(), fs.end());
    ll cur = cnt_r;
    while(cur > k){
        fs.pop_back();
        cur--;
    }
    cout << fs.back() << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    init();
    int t; cin >> t;
    while(t--) solve();
}
0