結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 rin204rin204
提出日時 2023-09-18 20:56:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 357 ms / 2,000 ms
コード長 846 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 81,824 KB
最終ジャッジ日時 2023-09-18 20:56:41
合計ジャッジ時間 7,450 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,296 KB
testcase_01 AC 316 ms
79,952 KB
testcase_02 AC 357 ms
79,368 KB
testcase_03 AC 311 ms
79,416 KB
testcase_04 AC 320 ms
80,372 KB
testcase_05 AC 330 ms
81,120 KB
testcase_06 AC 228 ms
78,540 KB
testcase_07 AC 228 ms
78,692 KB
testcase_08 AC 232 ms
78,660 KB
testcase_09 AC 225 ms
78,380 KB
testcase_10 AC 263 ms
78,636 KB
testcase_11 AC 327 ms
79,336 KB
testcase_12 AC 332 ms
80,052 KB
testcase_13 AC 338 ms
81,824 KB
testcase_14 AC 328 ms
80,540 KB
testcase_15 AC 327 ms
80,104 KB
testcase_16 AC 240 ms
79,284 KB
testcase_17 AC 242 ms
79,680 KB
testcase_18 AC 234 ms
79,464 KB
testcase_19 AC 226 ms
78,872 KB
testcase_20 AC 229 ms
78,744 KB
権限があれば一括ダウンロードができます

ソースコード

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