結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 MizarMizar
提出日時 2023-09-06 04:55:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 754 bytes
コンパイル時間 97 ms
コンパイル使用メモリ 10,756 KB
実行使用メモリ 7,964 KB
最終ジャッジ日時 2023-09-06 04:55:20
合計ジャッジ時間 4,459 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,964 KB
testcase_01 AC 160 ms
7,780 KB
testcase_02 AC 158 ms
7,856 KB
testcase_03 AC 157 ms
7,728 KB
testcase_04 AC 183 ms
7,772 KB
testcase_05 AC 161 ms
7,864 KB
testcase_06 AC 129 ms
7,804 KB
testcase_07 AC 129 ms
7,920 KB
testcase_08 AC 129 ms
7,860 KB
testcase_09 AC 128 ms
7,808 KB
testcase_10 AC 130 ms
7,816 KB
testcase_11 AC 186 ms
7,924 KB
testcase_12 AC 158 ms
7,728 KB
testcase_13 AC 159 ms
7,824 KB
testcase_14 AC 161 ms
7,816 KB
testcase_15 AC 160 ms
7,816 KB
testcase_16 AC 104 ms
7,816 KB
testcase_17 AC 104 ms
7,960 KB
testcase_18 AC 105 ms
7,808 KB
testcase_19 AC 108 ms
7,864 KB
testcase_20 AC 104 ms
7,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# calc sum_{i=0}^{n-1} floor((ai + b) / m)
def floor_sum(n: int, m: int, a: int, b: int) -> int:
    # assert n >= 0 and m > 0
    x = 0
    while n > 0:
        if a >= m or a < 0:
            q, a = divmod(a, m)
            x += (n * (n - 1) >> 1) * q
        if b >= m or b < 0:
            q, b = divmod(b, m)
            x += n * q
        n, b = divmod(a * n + b, m)
        m, a = a, m
    return x

def solve(n: int, d: int, m: int, s: int) -> int:
    pow2s, dm = 1 << s, d * m
    if pow2s != dm:
        n = min(n, (d * pow2s - 1) // abs(pow2s - dm))
        n -= abs(floor_sum(n + 1, pow2s, m, 0) - floor_sum(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