結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 MizarMizar
提出日時 2023-08-16 00:00:15
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 859 bytes
コンパイル時間 99 ms
コンパイル使用メモリ 10,808 KB
実行使用メモリ 7,940 KB
最終ジャッジ日時 2023-08-24 20:30:16
合計ジャッジ時間 4,322 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,776 KB
testcase_01 AC 170 ms
7,848 KB
testcase_02 AC 166 ms
7,892 KB
testcase_03 AC 167 ms
7,856 KB
testcase_04 AC 167 ms
7,776 KB
testcase_05 AC 166 ms
7,840 KB
testcase_06 AC 137 ms
7,940 KB
testcase_07 AC 139 ms
7,780 KB
testcase_08 AC 137 ms
7,852 KB
testcase_09 AC 136 ms
7,824 KB
testcase_10 AC 138 ms
7,852 KB
testcase_11 AC 168 ms
7,856 KB
testcase_12 AC 168 ms
7,808 KB
testcase_13 AC 169 ms
7,848 KB
testcase_14 AC 168 ms
7,892 KB
testcase_15 AC 168 ms
7,816 KB
testcase_16 AC 108 ms
7,856 KB
testcase_17 AC 108 ms
7,928 KB
testcase_18 AC 107 ms
7,828 KB
testcase_19 AC 107 ms
7,808 KB
testcase_20 AC 107 ms
7,800 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