結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 MizarMizar
提出日時 2023-09-06 04:46:31
言語 Ruby
(3.3.0)
結果
AC  
実行時間 211 ms / 2,000 ms
コード長 687 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 11,248 KB
実行使用メモリ 15,440 KB
最終ジャッジ日時 2023-09-06 04:46:37
合計ジャッジ時間 5,325 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
15,212 KB
testcase_01 AC 199 ms
15,260 KB
testcase_02 AC 195 ms
15,200 KB
testcase_03 AC 211 ms
15,264 KB
testcase_04 AC 195 ms
15,264 KB
testcase_05 AC 195 ms
15,212 KB
testcase_06 AC 184 ms
15,440 KB
testcase_07 AC 182 ms
15,344 KB
testcase_08 AC 196 ms
15,268 KB
testcase_09 AC 183 ms
15,040 KB
testcase_10 AC 188 ms
15,216 KB
testcase_11 AC 194 ms
15,144 KB
testcase_12 AC 187 ms
15,224 KB
testcase_13 AC 193 ms
15,308 KB
testcase_14 AC 206 ms
15,404 KB
testcase_15 AC 192 ms
15,256 KB
testcase_16 AC 135 ms
15,196 KB
testcase_17 AC 137 ms
15,348 KB
testcase_18 AC 136 ms
15,164 KB
testcase_19 AC 134 ms
15,160 KB
testcase_20 AC 139 ms
15,412 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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