結果

問題 No.2066 Simple Math !
ユーザー HachimoriHachimori
提出日時 2023-09-07 00:05:42
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 261 ms / 2,000 ms
コード長 1,227 bytes
コンパイル時間 753 ms
コンパイル使用メモリ 79,152 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-07 00:05:50
合計ジャッジ時間 7,544 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 25 ms
4,384 KB
testcase_02 AC 39 ms
4,376 KB
testcase_03 AC 26 ms
4,380 KB
testcase_04 AC 238 ms
4,380 KB
testcase_05 AC 235 ms
4,380 KB
testcase_06 AC 233 ms
4,380 KB
testcase_07 AC 261 ms
4,380 KB
testcase_08 AC 236 ms
4,380 KB
testcase_09 AC 240 ms
4,376 KB
testcase_10 AC 244 ms
4,380 KB
testcase_11 AC 243 ms
4,380 KB
testcase_12 AC 249 ms
4,380 KB
testcase_13 AC 244 ms
4,384 KB
testcase_14 AC 230 ms
4,376 KB
testcase_15 AC 230 ms
4,376 KB
testcase_16 AC 231 ms
4,376 KB
testcase_17 AC 234 ms
4,380 KB
testcase_18 AC 230 ms
4,380 KB
testcase_19 AC 85 ms
4,380 KB
testcase_20 AC 86 ms
4,380 KB
testcase_21 AC 84 ms
4,380 KB
testcase_22 AC 85 ms
4,380 KB
testcase_23 AC 86 ms
4,380 KB
testcase_24 AC 131 ms
4,380 KB
testcase_25 AC 133 ms
4,380 KB
testcase_26 AC 134 ms
4,376 KB
testcase_27 AC 132 ms
4,376 KB
testcase_28 AC 131 ms
4,384 KB
testcase_29 AC 81 ms
4,384 KB
testcase_30 AC 27 ms
4,380 KB
testcase_31 AC 38 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
using namespace std;


long long f(long long n, long long a, long long b) {
    return a * n + b;
}

long long floorSum(long long n, long long m, long long a, long long b) {
    if (n == 0) return 0;

    long long toAdd = (a / m) * n * (n - 1) / 2 + (b / m) * n;

    a %= m;
    b %= m;

    return toAdd + f(n - 1, a, b) / m + floorSum(f(n - 1, a, b) / m, a, m, f(n - 1, a, b) % m);
}


int P, Q, K;

void read() {
    cin >> P >> Q >> K;
}


int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}


void work() {
    int div = gcd(P, Q);
    P /= div;
    Q /= div;

    long long LT = 0, GE = 1LL * P * Q - 1;
    while (LT + 1 < GE) {
        long long mid = (LT + GE) / 2;

        if (floorSum(mid / P + 1, Q, P, mid - (mid / P) * P) + mid / P < K) {
            LT = mid;
        } else {
            GE = mid;
        }
    }

    if (GE == 1LL * P * Q - 1) {
        long long toAdd = K - (floorSum(GE / P + 1, Q, P, GE - (GE / P) * P) + GE / P);
        cout << (GE + toAdd) * div << endl;
    } else {
        cout << GE * div << endl;
    }
}


int main() {
    int T;
    cin >> T;
    for (int i = 0; i < T; ++i) {
        read();
        work();
    }
    return 0;
}
0