結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 MizarMizar
提出日時 2023-08-17 20:15:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 325 ms / 2,000 ms
コード長 859 bytes
コンパイル時間 749 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 80,708 KB
最終ジャッジ日時 2023-08-24 20:30:24
合計ジャッジ時間 6,952 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,300 KB
testcase_01 AC 325 ms
79,536 KB
testcase_02 AC 296 ms
80,708 KB
testcase_03 AC 276 ms
79,888 KB
testcase_04 AC 295 ms
79,468 KB
testcase_05 AC 291 ms
79,724 KB
testcase_06 AC 196 ms
77,992 KB
testcase_07 AC 196 ms
78,112 KB
testcase_08 AC 197 ms
78,148 KB
testcase_09 AC 198 ms
78,088 KB
testcase_10 AC 200 ms
78,144 KB
testcase_11 AC 291 ms
79,736 KB
testcase_12 AC 292 ms
80,144 KB
testcase_13 AC 285 ms
79,004 KB
testcase_14 AC 283 ms
80,096 KB
testcase_15 AC 287 ms
79,600 KB
testcase_16 AC 219 ms
78,256 KB
testcase_17 AC 230 ms
79,320 KB
testcase_18 AC 229 ms
79,328 KB
testcase_19 AC 219 ms
79,604 KB
testcase_20 AC 217 ms
79,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 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(input())):
    n, d, m, s = map(int, input().split())
    print(solve(n, d, m, s))
0