結果

問題 No.2066 Simple Math !
ユーザー mkawa2
提出日時 2025-03-04 22:54:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 872 ms / 2,000 ms
コード長 2,138 bytes
コンパイル時間 794 ms
コンパイル使用メモリ 82,776 KB
実行使用メモリ 79,152 KB
最終ジャッジ日時 2025-03-04 22:54:35
合計ジャッジ時間 23,223 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 62)

# md = 10**9+7
md = 998244353

from math import gcd

# px+qy<=N
# y<=(N-px)/q
# y<=(px+N-pq+p+q)/q

def solve():
    # sum((a*i+b)//m for i in range(n))が求まる
    # m>=1
    # a,bは負でも可だが、結果はmax(0,(a*i+b)//m)の和となる

    def floor_sum(n: int, m: int, a: int, b: int) -> int:
        if a < 0:
            if b < 0: return 0
            n = min(n, -b//a+1)
            if n == 0: return 0
            a, b = -a, a*n-a+b
        if b < 0:
            d = -b+a-1//a
            if d > n: return 0
            b, n = a*d+b, n-d
        ans = 0
        while 1:
            if a >= m:
                ans += (n-1)*n*(a//m)//2
                a %= m
            if b >= m:
                ans += n*(b//m)
                b %= m
            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
            n, m, a, b = y_max, a, m, (a-x_max%a)%a

    def ok(m):
        return floor_sum(q, q, -p, m+q) > k

    def binary_search(l, r, minimize):
        if minimize: l -= 1
        else: r += 1
        while l+1 < r:
            m = (l+r)//2
            if ok(m) ^ minimize: l = m
            else: r = m
        if minimize: return r
        return l

    p, q, k = LI()
    g = gcd(p, q)
    p //= g
    q //= g
    print(binary_search(0, 10**18+10**9, True)*g)

for _ in range(II()): solve()
0