結果

問題 No.2452 Incline
ユーザー hitonanode
提出日時 2023-09-01 23:27:53
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 1,599 bytes
コンパイル時間 1,051 ms
コンパイル使用メモリ 79,600 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2025-01-03 12:33:06
合計ジャッジ時間 2,684 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

using ULL = __uint128_t;
using LongLong = __int128_t;

LongLong floor_sum(LongLong n, LongLong m, LongLong a, LongLong b) {
    auto safe_mod = [](LongLong x, LongLong m) -> LongLong {
        x %= m;
        if (x < 0) x += m;
        return x;
    };
    auto floor_sum_unsigned = [](ULL n, ULL m, ULL a,
                                 ULL b) -> ULL {
        ULL ans = 0;
        while (true) {
            if (a >= m) {
                ans += n * (n - 1) / 2 * (a / m);
                a %= m;
            }
            if (b >= m) {
                ans += n * (b / m);
                b %= m;
            }

            ULL y_max = a * n + b;
            if (y_max < m) break;
            // y_max < m * (n + 1)
            // floor(y_max / m) <= n
            n = (ULL)(y_max / m);
            b = (ULL)(y_max % m);
            std::swap(m, a);
        }
        return ans;
    };

    ULL ans = 0;
    if (a < 0) {
        ULL a2 = safe_mod(a, m);
        ans -= 1ULL * n * (n - 1) / 2 * ((a2 - a) / m);
        a = a2;
    }
    if (b < 0) {
        ULL b2 = safe_mod(b, m);
        ans -= 1ULL * n * ((b2 - b) / m);
        b = b2;
    }
    return ans + floor_sum_unsigned(n, m, a, b);
}


int solve() {
    long long N, M, L, R;
    cin >> N >> M >> L >> R;

    LongLong ret = R - L + 1;
    ret += floor_sum(R - L + 1, N - 1, 1, L);
    ret += floor_sum(R - L + 1, N - 1, 1, M - R);
    return ret % 998244353;
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int T;
    cin >> T;
    while (T--) cout << solve() << '\n';
}
0