結果

問題 No.3397 Max Weighted Floor of Linear
コンテスト
ユーザー 👑 Mizar
提出日時 2025-11-28 18:17:14
言語 JavaScript
(node v23.5.0)
結果
AC  
実行時間 1,729 ms / 2,000 ms
コード長 1,443 bytes
記録
コンパイル時間 90 ms
コンパイル使用メモリ 7,844 KB
実行使用メモリ 126,224 KB
最終ジャッジ日時 2025-12-03 23:35:54
合計ジャッジ時間 34,457 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#!/usr/bin/env node

const divmod_euclid = (a, b) => {
    let [q, r] = [a / b, a % b];
    if (r < 0n) {
        if (b > 0n) {
            q -= 1n;
            r += b;
        } else {
            q += 1n;
            r -= b;
        }
    }
    return [q, r];
}

const mwf = (l, r, m, a, b, c, d) => {
    let n = r - l;
    let [qc0, rc0] = divmod_euclid(c, m);
    c = rc0;
    a += b * qc0;
    let [qd0, rd0] = divmod_euclid(d, m);
    d = rd0;
    let sum_acc = a * l + b * qd0;
    let max_acc = sum_acc;
    while (true) {
        n -= 1n;
        let y_max = (c * n + d) / m;
        if (max_acc < sum_acc) {
            max_acc = sum_acc;
        }
        let t = sum_acc + a * n + b * y_max;
        if (max_acc < t) {
            max_acc = t;
        }
        if ((a <= 0n && b <= 0n) || (a >= 0n && b >= 0n) || y_max == 0n) {
            return max_acc;
        }
        if (a < 0n) {
            sum_acc += a + b;
        }
        n = y_max;
        d = m - d - 1n;
        [m, c, a, b] = [c, m, b, a];
        let qc = c / m;
        c %= m;
        a += b * qc;
        let qd = d / m;
        d %= m;
        sum_acc += b * qd;
    }
}

let [t, ...inputs] = require("fs")
  .readFileSync("/dev/stdin", "utf8")
  .trim()
  .split(/\s+/);
t = Number(t);
for (let i = 0; i < t; i += 1) {
  let [n, m, a, b, c, d] = inputs.slice(i * 6, i * 6 + 6).map((s) => BigInt(s));
  console.log(mwf(0n, n, m, a, b, c, d).toString());
}
0