結果

問題 No.2066 Simple Math !
ユーザー tamatotamato
提出日時 2022-09-02 22:28:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 720 ms / 2,000 ms
コード長 1,228 bytes
コンパイル時間 159 ms
コンパイル使用メモリ 82,452 KB
実行使用メモリ 77,608 KB
最終ジャッジ日時 2024-04-27 22:05:03
合計ジャッジ時間 15,620 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
53,668 KB
testcase_01 AC 62 ms
76,128 KB
testcase_02 AC 109 ms
76,604 KB
testcase_03 AC 56 ms
73,768 KB
testcase_04 AC 690 ms
76,984 KB
testcase_05 AC 688 ms
76,820 KB
testcase_06 AC 699 ms
77,076 KB
testcase_07 AC 680 ms
77,396 KB
testcase_08 AC 694 ms
77,000 KB
testcase_09 AC 720 ms
77,072 KB
testcase_10 AC 693 ms
76,840 KB
testcase_11 AC 703 ms
76,800 KB
testcase_12 AC 698 ms
77,232 KB
testcase_13 AC 713 ms
76,820 KB
testcase_14 AC 666 ms
77,004 KB
testcase_15 AC 696 ms
77,152 KB
testcase_16 AC 691 ms
77,060 KB
testcase_17 AC 686 ms
77,000 KB
testcase_18 AC 680 ms
77,304 KB
testcase_19 AC 284 ms
77,548 KB
testcase_20 AC 279 ms
77,408 KB
testcase_21 AC 265 ms
77,292 KB
testcase_22 AC 290 ms
77,608 KB
testcase_23 AC 291 ms
77,044 KB
testcase_24 AC 388 ms
77,164 KB
testcase_25 AC 373 ms
76,812 KB
testcase_26 AC 362 ms
77,316 KB
testcase_27 AC 373 ms
77,176 KB
testcase_28 AC 368 ms
76,960 KB
testcase_29 AC 202 ms
77,052 KB
testcase_30 AC 57 ms
73,532 KB
testcase_31 AC 101 ms
77,092 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