結果

問題 No.3397 Max Weighted Floor of Linear
コンテスト
ユーザー 👑 Mizar
提出日時 2025-11-27 21:50:13
言語 C++23
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 1,916 ms / 2,000 ms
コード長 3,729 bytes
記録
コンパイル時間 7,519 ms
コンパイル使用メモリ 383,764 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-12-03 23:35:18
合計ジャッジ時間 26,369 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <cassert> // assert
#include <iostream> // cin, cout, ios
#include <utility> // swap, pair
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
using bigint = boost::multiprecision::cpp_int;

template <typename T> bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T> constexpr std::pair<T, T> divmod(T a, T b) {
    T q = a / b;
    T r = a % b;
    return {q, r};
}

// 商を無限大方向に切り下げる除算(剰余が0でない時の符号は除数の符号に一致)
template <typename T> constexpr std::pair<T, T> divmod_floor(T a, T b) {
    // 標準の truncating 除算を使う
    T q = a / b;
    T r = a % b;
    // もし符号が食い違っていたら 1 調整
    if ((r != 0) && ((r > 0) != (b > 0))) {
        q -= 1;
        r += b;
    }
    return {q, r};
}

// 剰余が非負になる除算(ユークリッド除算)
template <typename T> constexpr std::pair<T, T> divmod_euclid(T a, T b) {
    // 標準の truncating 除算を使う
    T q = a / b;
    T r = a % b;
    // 剰余が負なら 1 調整
    if (r < 0) {
        if (b > 0) {
            q -= 1;
            r += b;
        } else {
            q += 1;
            r -= b;
        }
    }
    return {q, r};
}

/**
    Max Weighted Floor (mwf) の非再帰実装。
      mwf(l,r,m,a,b,c,d) = max_{l <= x < r} a*x + b*floor((c*x + d)/m)

    前提:
      - l < r, 0 < m

    計算量/メモリ:
      - 時間: O(log m)(ユークリッド互除法的再帰による構造縮約)
      - 追加メモリ: O(1)
*/
template <typename T> T mwf(T l, T r, T m, T a, T b, T c, T d) {
    assert(l < r && 0 < m);
    // 初回の正規化: c,d を m で割った余りに変換し、a,b を更新、 sum_acc,max_acc を初期化
    // c,d が負の場合にも剰余を非負にするため、ユークリッド除算を使う
    T n = r - l;
    const auto [qc0, rc0] = divmod_euclid<T>(c, m);
    c = rc0;
    a += b * qc0;
    const auto [qd0, rd0] = divmod_euclid<T>(c * l + d, m);
    d = rd0;
    T sum_acc = a * l + b * qd0, max_acc = sum_acc;
    while(true) {
        assert(0 < n && 0 < m && 0 <= c && c < m && 0 <= d && d < m);
        // 0 ≤ x < n における y = floor((c*x+d)/m) の最大値 y_max を計算
        const T y_max = (c * (n - 1) + d) / m; // y_max >= 0
        // 現在の小問題における x = 0, n-1 のときの値を max_acc に反映
        chmax<T>(max_acc, sum_acc);
        chmax<T>(max_acc, sum_acc + a * (n - 1) + b * y_max);
        // x = 0, n-1 のいずれかで最大値を取るのが確定したら終了
        if(y_max == 0 || (a >= 0 && b >= 0) || (a <= 0 && b <= 0)) {
            return max_acc;
        }
        // 小問題へのパラメータ変換
        if(a < 0) {
            sum_acc += a + b;
        }
        n = y_max;
        d = m - d - 1;
        std::swap(a, b);
        std::swap(c, m);
        assert(0 < n && 0 < m && 0 < c && 0 <= d);
        // 2回目以降の正規化: c,d を m で割った余りに変換し、a,b,sum_acc を更新
        // c,d,m は非負なので通常の剰余で良い
        const auto [qc1, rc1] = divmod<T>(c, m);
        c = rc1;
        a += b * qc1;
        const auto [qd1, rd1] = divmod<T>(d, m);
        d = rd1;
        sum_acc += b * qd1;
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int t;
    bigint n, m, a, b, c, d, ans;
    std::cin >> t;
    for(int i = 0; i < t; i++) {
        std::cin >> n >> m >> a >> b >> c >> d;
        assert(0 < n && 0 < m);
        ans = mwf<bigint>(0, n, m, a, b, c, d);
        std::cout << ans << '\n';
    }
}
0