結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー rin204
提出日時 2023-09-18 20:56:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 304 ms / 2,000 ms
コード長 846 bytes
コンパイル時間 216 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 79,236 KB
最終ジャッジ日時 2024-07-05 10:14:40
合計ジャッジ時間 6,767 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

def floor_sum(n, m, a, b):
    """
    return \\sum_{i=0}^{n-1} ((a*i+b)//m)
    """
    ret = 0
    while True:
        if a >= m:
            ret += (n - 1) * n // 2 * (a // m)
            a %= m
        if b >= m:
            ret += n * (b // m)
            b %= m
        y_max = (a * n + b) // m
        if y_max == 0:
            return ret
        x_max = y_max * m - b
        ret += (n - (x_max + a - 1) // a) * y_max
        n, m, a, b = y_max, a, m, -x_max % a


def solve(N, D, M, S):
    S = 1 << S
    if S == M * D:
        return N

    if S < M * D:
        th = -S * D // (S - M * D)
    else:
        th = S * D // (S - M * D)
    th = min(th, N)
    l = floor_sum(th, D, 1, 1)
    r = floor_sum(th, S, M, M)
    return th - abs(l - r)


for _ in range(int(input())):
    ans = solve(*map(int, input().split()))
    print(ans)
0