結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 MizarMizar
提出日時 2023-09-02 17:05:34
言語 Ruby
(3.3.0)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 582 bytes
コンパイル時間 316 ms
コンパイル使用メモリ 11,296 KB
実行使用メモリ 15,460 KB
最終ジャッジ日時 2023-09-02 17:05:40
合計ジャッジ時間 6,064 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,192 KB
testcase_01 AC 191 ms
15,216 KB
testcase_02 AC 192 ms
15,376 KB
testcase_03 AC 191 ms
15,228 KB
testcase_04 AC 195 ms
15,380 KB
testcase_05 AC 191 ms
15,288 KB
testcase_06 AC 193 ms
15,460 KB
testcase_07 AC 192 ms
15,224 KB
testcase_08 AC 192 ms
15,288 KB
testcase_09 AC 193 ms
15,300 KB
testcase_10 AC 197 ms
15,208 KB
testcase_11 AC 194 ms
15,412 KB
testcase_12 AC 193 ms
15,176 KB
testcase_13 AC 193 ms
15,216 KB
testcase_14 AC 192 ms
15,408 KB
testcase_15 AC 194 ms
15,224 KB
testcase_16 AC 143 ms
15,392 KB
testcase_17 AC 143 ms
15,240 KB
testcase_18 AC 142 ms
15,408 KB
testcase_19 AC 141 ms
15,372 KB
testcase_20 AC 142 ms
15,116 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

# calc sum_{i=0}^{n-1} floor((ai + b) / m)
def floor_sum_unsigned(n, m, a, b)
    x = 0
    while n > 0
        x += a / m * n * (n - 1) / 2 + b / m * n
        a %= m
        n, b = (a * n + b % m).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 / (dm - pow2s).abs].min
        n -= (floor_sum_unsigned(n + 1, pow2s, m, 0) - floor_sum_unsigned(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