結果

問題 No.3397 Max Weighted Floor of Linear
コンテスト
ユーザー t98slider
提出日時 2025-12-04 06:39:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 1,196 bytes
記録
コンパイル時間 4,192 ms
コンパイル使用メモリ 253,768 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-12-04 06:39:26
合計ジャッジ時間 9,654 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    while(T--){
        ll n, m, a, b, c, d;
        cin >> n >> m >> a >> b >> c >> d;
        auto rec = [&](auto rec, ll n, ll m, ll a, ll b, ll c, ll d) -> ll {
            ll ofs = 0;

            assert(c >= 0 && d >= 0);
            ll dc = c / m, dd = d / m;
            // (Ax + B[(Cx + D) / M]) -> (A + [BC / M]) x + [BD / M]
            a += b * dc;
            c -= dc * m;
            ofs += b * dd;
            d -= dd * m;

            ll res = ofs + max(0ll, a * (n - 1) + b * ((c * (n - 1) + d) / m));
            if((a >= 0) ^ (b >= 0) ^ true) return res;

            // floor((C(N-1) + D) / M) の最大値
            ll num = (c * (n - 1) + d) / m;
            if(num == 0) return res;

            if(a < 0){
                res = max(res, ofs + b + rec(rec, num, c, b, a, m, m + c - 1 - d));
            }else{
                res = max(res, ofs + rec(rec, num, c, b, a, m, m - d - 1));
            }
            return res;
        };

        cout << rec(rec, n, m, a, b, c, d) << '\n';
    }
}
0