結果

問題 No.2066 Simple Math !
ユーザー t98slidert98slider
提出日時 2022-09-04 20:45:02
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 515 ms / 2,000 ms
コード長 1,265 bytes
コンパイル時間 1,462 ms
コンパイル使用メモリ 165,400 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-12 05:55:02
合計ジャッジ時間 13,924 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 117 ms
4,380 KB
testcase_02 AC 153 ms
4,380 KB
testcase_03 AC 25 ms
4,376 KB
testcase_04 AC 477 ms
4,380 KB
testcase_05 AC 476 ms
4,376 KB
testcase_06 AC 481 ms
4,380 KB
testcase_07 AC 489 ms
4,376 KB
testcase_08 AC 491 ms
4,376 KB
testcase_09 AC 515 ms
4,380 KB
testcase_10 AC 502 ms
4,376 KB
testcase_11 AC 500 ms
4,380 KB
testcase_12 AC 503 ms
4,376 KB
testcase_13 AC 494 ms
4,380 KB
testcase_14 AC 425 ms
4,376 KB
testcase_15 AC 431 ms
4,380 KB
testcase_16 AC 436 ms
4,380 KB
testcase_17 AC 438 ms
4,376 KB
testcase_18 AC 431 ms
4,380 KB
testcase_19 AC 270 ms
4,380 KB
testcase_20 AC 271 ms
4,376 KB
testcase_21 AC 270 ms
4,376 KB
testcase_22 AC 265 ms
4,376 KB
testcase_23 AC 270 ms
4,380 KB
testcase_24 AC 413 ms
4,380 KB
testcase_25 AC 418 ms
4,376 KB
testcase_26 AC 406 ms
4,376 KB
testcase_27 AC 409 ms
4,380 KB
testcase_28 AC 420 ms
4,380 KB
testcase_29 AC 99 ms
4,376 KB
testcase_30 AC 26 ms
4,380 KB
testcase_31 AC 23 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

long long floor_sum(long long n, long long m, long long a, long long b) {
    long long ans = 0;
    if (a >= m) {
        ans += (n - 1) * n * (a / m) / 2;
        ans = min(ans, 1ll << 60);
        a %= m;
    }
    if (b >= m) {
        ans += n * (b / m);
        ans = min(ans, 1ll << 60);
        b %= m;
    }
    long long y_max = (a * n + b) / m, x_max = (y_max * m - b);
    if (y_max == 0) return ans;
    ans += (n - (x_max + a - 1) / a) * y_max;
    ans = min(ans, 1ll << 60);
    ans += floor_sum(y_max, a, m, (a - x_max % a) % a);
    ans = min(ans, 1ll << 60);
    return ans;
}

int main(){
    int t;
    cin >> t;
    while(t--){
        ll p, q, k, g;
        cin >> p >> q >> k;
        g = __gcd(p, q);
        p /= g, q /= g;
        if(p == 1 || q == 1){
            cout << g * k << '\n';
            continue;
        }
        auto f = [&](ll v){
            ll r = min(q, v / p + 1);
            return floor_sum(r, q, p, v + q - p * (r - 1));
        };
        ll ng = 0, ok = 1ll << 60, mid;
        while(ng + 1 < ok){
            mid = (ng + ok) / 2;
            if(f(mid) <= k)ng = mid;
            else ok = mid;
        }
        cout << g * ok << '\n';
    }
}
0