結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 MizarMizar
提出日時 2023-08-25 13:21:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 254 ms / 2,000 ms
コード長 908 bytes
コンパイル時間 438 ms
コンパイル使用メモリ 87,028 KB
実行使用メモリ 79,960 KB
最終ジャッジ日時 2023-08-25 13:21:22
合計ジャッジ時間 5,981 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,464 KB
testcase_01 AC 245 ms
79,960 KB
testcase_02 AC 241 ms
79,232 KB
testcase_03 AC 244 ms
79,564 KB
testcase_04 AC 254 ms
79,548 KB
testcase_05 AC 232 ms
79,428 KB
testcase_06 AC 155 ms
78,400 KB
testcase_07 AC 150 ms
78,544 KB
testcase_08 AC 151 ms
78,488 KB
testcase_09 AC 157 ms
78,580 KB
testcase_10 AC 152 ms
79,008 KB
testcase_11 AC 230 ms
79,356 KB
testcase_12 AC 235 ms
79,392 KB
testcase_13 AC 244 ms
79,872 KB
testcase_14 AC 240 ms
79,528 KB
testcase_15 AC 234 ms
79,464 KB
testcase_16 AC 172 ms
78,952 KB
testcase_17 AC 185 ms
78,752 KB
testcase_18 AC 179 ms
79,344 KB
testcase_19 AC 173 ms
79,096 KB
testcase_20 AC 179 ms
79,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.buffer.readline

# calc sum_{i=0}^{n-1} floor((ai + b) / m)
def floor_sum_unsigned(n: int, m: int, a: int, b: int) -> int:
    assert n >= 0 and m > 0 and a >= 0 and b >= 0
    ans = 0
    while True:
        if a >= m:
            ans += (n * (n - 1) >> 1) * (a // m)
            a %= m
        if b >= m:
            ans += n * (b // m)
            b %= m
        y_max = a * n + b
        if y_max < m: return ans
        n, b, m, a = y_max // m, y_max % m, a, m

def solve(n: int, d: int, m: int, s: int) -> int:
    assert n >= 0 and d > 0 and m >= 0 and s >= 0
    pow2s, dm = 1 << s, d * m
    if pow2s != dm:
        n = min(n, d * pow2s // abs(dm - pow2s))
        n -= abs(floor_sum_unsigned(n + 1, pow2s, m, 0) - floor_sum_unsigned(n + 1, d, 1, 0))
    return n

for _ in range(int(readline())):
	n, d, m, s = map(int, readline().split())
	print(solve(n, d, m, s))
0