結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,788 KB
testcase_01 AC 162 ms
7,808 KB
testcase_02 AC 160 ms
7,932 KB
testcase_03 AC 165 ms
7,984 KB
testcase_04 AC 160 ms
7,880 KB
testcase_05 AC 160 ms
7,788 KB
testcase_06 AC 133 ms
7,784 KB
testcase_07 AC 145 ms
7,836 KB
testcase_08 AC 132 ms
7,856 KB
testcase_09 AC 133 ms
7,812 KB
testcase_10 AC 132 ms
7,948 KB
testcase_11 AC 164 ms
7,796 KB
testcase_12 AC 164 ms
7,864 KB
testcase_13 AC 163 ms
7,856 KB
testcase_14 AC 174 ms
7,768 KB
testcase_15 AC 164 ms
7,764 KB
testcase_16 AC 108 ms
7,752 KB
testcase_17 AC 108 ms
7,852 KB
testcase_18 AC 108 ms
7,920 KB
testcase_19 AC 107 ms
7,872 KB
testcase_20 AC 107 ms
7,880 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 = 2 ** 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