結果

問題 No.2066 Simple Math !
ユーザー 👑 rin204
提出日時 2022-09-02 22:30:30
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 799 bytes
コンパイル時間 437 ms
コンパイル使用メモリ 82,280 KB
実行使用メモリ 164,544 KB
最終ジャッジ日時 2024-11-16 04:31:35
合計ジャッジ時間 25,820 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 2 WA * 24 TLE * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd

def solve(p, q, k):
    x = p * q
    y = (p - 1) * (q - 1)
    if y <= k:
        ans = x - 1 + k - y
        return ans

    if p > q:
        p, q = q, p
    
    ma = k // q
    lst = [q]
    se = {0}
    tot = 0
    for _ in range(ma + 10):
        tot += p
        if (tot % q) not in se:
            lst.append(tot)
            se.add(tot % q)    

    def cnt(x):
        c = 0
        for a in lst:
            c += max(0, (x - a + q) // q)
        return c

    l = 0
    r = p * q
    while r - l > 1:
        mid = (l + r) // 2
        if cnt(mid) >= k:
            r = mid
        else:
            l = mid
    return r


for _ in range(int(input())):
    p, q, k = map(int, input().split())
    g = gcd(p, q)
    p //= g
    q //= g
    print(g * solve(p, q, k))
0