結果

問題 No.3397 Max Weighted Floor of Linear
コンテスト
ユーザー t98slider
提出日時 2025-12-04 06:46:57
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 1,122 bytes
記録
コンパイル時間 4,412 ms
コンパイル使用メモリ 253,704 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-12-04 06:47:09
合計ジャッジ時間 9,945 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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 {
            assert(c >= 0 && d >= 0);
            ll dc = c / m, dd = d / m, ofs = 0;
            // Ax + B[(Cx + D) / M] -> (A + [BC / M]) x + [BD / M]
            a += b * dc;
            c -= dc * m;
            ofs += b * dd;
            d -= dd * m;
			
			// floor((C(N-1) + D) / M) の最大値
            ll num = (c * (n - 1) + d) / m;
            ll res = ofs + max(0ll, a * (n - 1) + b * num);
            if((!(a >= 0) ^ (b >= 0)) || 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