結果

問題 No.2066 Simple Math !
ユーザー 👑 tamatotamato
提出日時 2022-09-02 22:28:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 843 ms / 2,000 ms
コード長 1,228 bytes
コンパイル時間 365 ms
コンパイル使用メモリ 87,108 KB
実行使用メモリ 80,272 KB
最終ジャッジ日時 2023-08-10 04:44:35
合計ジャッジ時間 19,577 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,580 KB
testcase_01 AC 102 ms
77,724 KB
testcase_02 AC 146 ms
78,256 KB
testcase_03 AC 91 ms
77,344 KB
testcase_04 AC 810 ms
78,176 KB
testcase_05 AC 810 ms
78,560 KB
testcase_06 AC 819 ms
78,668 KB
testcase_07 AC 808 ms
78,524 KB
testcase_08 AC 812 ms
78,524 KB
testcase_09 AC 839 ms
78,664 KB
testcase_10 AC 837 ms
78,544 KB
testcase_11 AC 834 ms
78,632 KB
testcase_12 AC 838 ms
78,540 KB
testcase_13 AC 843 ms
78,576 KB
testcase_14 AC 811 ms
78,408 KB
testcase_15 AC 806 ms
78,476 KB
testcase_16 AC 813 ms
78,144 KB
testcase_17 AC 812 ms
78,508 KB
testcase_18 AC 804 ms
78,456 KB
testcase_19 AC 357 ms
78,688 KB
testcase_20 AC 373 ms
79,784 KB
testcase_21 AC 337 ms
79,052 KB
testcase_22 AC 367 ms
79,032 KB
testcase_23 AC 364 ms
80,272 KB
testcase_24 AC 454 ms
78,788 KB
testcase_25 AC 456 ms
79,144 KB
testcase_26 AC 456 ms
79,164 KB
testcase_27 AC 456 ms
79,060 KB
testcase_28 AC 453 ms
78,772 KB
testcase_29 AC 269 ms
78,268 KB
testcase_30 AC 94 ms
77,680 KB
testcase_31 AC 135 ms
78,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353


def main():
    import sys
    from math import gcd
    input = sys.stdin.readline

    # sum([(a * i + b) // m for i in range(n)])
    def floor_sum(n, m, a, b):
        ret = 0
        while True:
            ret += (a // m) * n * (n - 1) // 2 + (b // m) * n
            a %= m
            b %= m
            y = (a * n + b) // m
            x = b - y * m
            if y == 0:
                return ret
            ret += (x // a + n) * y
            n, m, a, b = y, a, m, x % a

    for _ in range(int(input())):
        p, q, k = map(int, input().split())
        g = gcd(p, q)
        p //= g
        q //= g
        if p > q:
            p, q = q, p

        k0 = floor_sum(q, q, p, p) + q - 1
        if k >= k0:
            ans = p * q + (k - k0)
            print(ans * g)
        else:
            ok = p * q
            ng = 0
            mid = (ok + ng) // 2
            while ok - ng > 1:
                x0 = mid // p
                tmp = floor_sum(x0+1, q, p, mid - p * x0) + x0
                if tmp >= k:
                    ok = mid
                else:
                    ng = mid
                mid = (ok + ng) // 2
            print(ok * g)


if __name__ == '__main__':
    main()
0