結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 MizarMizar
提出日時 2023-09-06 04:45:03
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 687 bytes
コンパイル時間 369 ms
コンパイル使用メモリ 11,404 KB
実行使用メモリ 24,168 KB
最終ジャッジ日時 2023-09-06 04:45:11
合計ジャッジ時間 7,380 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

# calc sum_{i=0}^{n-1} floor((ai + b) / m)
def floor_sum(n, m, a, b)
    x = 0
    while n > 0
        if a < m && a >= 0
            q, a = a.divmod(m)
            x += n * (n - 1) / 2 * q
        end
        if b < m && b >= 0
            q, b = b.divmod(m)
            x += n * q
        end
        n, b = (a * n + b).divmod(m)
        m, a = a, m
    end
    x
end

def solve(n, d, m, s)
    pow2s, dm = 1 << s, d * m
    if pow2s != dm
        n = [n, d * pow2s / (pow2s - dm).abs].min
        n -= (floor_sum(n + 1, pow2s, m, 0) - floor_sum(n + 1, d, 1, 0)).abs
    end
    return n
end

for _ in 1..gets.to_i
    n, d, m, s = gets.split.map &:to_i
    puts solve(n, d, m, s)
end
0