結果

問題 No.2440 Accuracy of Integer Division Approximate Functions
ユーザー 👑 MizarMizar
提出日時 2023-08-18 06:08:27
言語 JavaScript
(node v21.7.1)
結果
AC  
実行時間 567 ms / 2,000 ms
コード長 1,099 bytes
コンパイル時間 56 ms
コンパイル使用メモリ 5,296 KB
実行使用メモリ 51,544 KB
最終ジャッジ日時 2023-08-24 20:30:51
合計ジャッジ時間 11,822 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
42,132 KB
testcase_01 AC 567 ms
50,744 KB
testcase_02 AC 538 ms
50,672 KB
testcase_03 AC 535 ms
50,832 KB
testcase_04 AC 544 ms
50,676 KB
testcase_05 AC 540 ms
50,964 KB
testcase_06 AC 449 ms
51,304 KB
testcase_07 AC 448 ms
51,364 KB
testcase_08 AC 453 ms
51,368 KB
testcase_09 AC 459 ms
51,456 KB
testcase_10 AC 463 ms
51,304 KB
testcase_11 AC 548 ms
51,164 KB
testcase_12 AC 544 ms
51,544 KB
testcase_13 AC 547 ms
51,068 KB
testcase_14 AC 546 ms
51,532 KB
testcase_15 AC 555 ms
51,312 KB
testcase_16 AC 387 ms
51,332 KB
testcase_17 AC 389 ms
51,292 KB
testcase_18 AC 387 ms
51,344 KB
testcase_19 AC 402 ms
51,208 KB
testcase_20 AC 389 ms
51,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// calc sum_{i=0}^{n-1} floor((ai + b) / m)
function floor_sum_unsigned(n, m, a, b) {
    let ans = 0n;
    while(true) {
        if (a >= m) {
            ans += (n * (n - 1n) >> 1n) * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }
        let y_max = a * n + b;
        if (y_max < m) {
            return ans;
        }
        [n, b, m, a] = [y_max / m, y_max % m, a, m];
    }
}

function bigint_abs(x) {
    return x < 0n ? -x : x;
}
function bigint_min(...args) {
    return args.reduce((m, e) => e < m ? e : m);
}

function solve(n, d, m, s) {
    let pow2s = 1n << s, dm = d * m;
    if (pow2s != dm) {
        n = bigint_min(n, d * pow2s / bigint_abs(dm - pow2s));
        n -= bigint_abs(floor_sum_unsigned(n + 1n, pow2s, m, 0n) - floor_sum_unsigned(n + 1n, d, 1n, 0n));
    }
    return n;
}

let input = require("fs").readFileSync("/dev/stdin", "utf8").matchAll(/\b(\d+) (\d+) (\d+) (\d+)/g);
for (l of input) {
    [n, d, m, s] = l.slice(1).map(t=>BigInt(t));
    console.log(solve(n, d, m, s).toString());
}
0