結果

問題 No.2144 MM
ユーザー CyanmondCyanmond
提出日時 2022-12-02 23:02:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 3,659 bytes
コンパイル時間 2,298 ms
コンパイル使用メモリ 201,452 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 08:19:56
合計ジャッジ時間 4,418 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 58 ms
5,376 KB
testcase_08 AC 70 ms
5,376 KB
testcase_09 AC 57 ms
5,376 KB
testcase_10 AC 67 ms
5,376 KB
testcase_11 AC 68 ms
5,376 KB
testcase_12 AC 68 ms
5,376 KB
testcase_13 AC 64 ms
5,376 KB
testcase_14 AC 69 ms
5,376 KB
testcase_15 AC 58 ms
5,376 KB
testcase_16 AC 17 ms
5,376 KB
testcase_17 AC 51 ms
5,376 KB
testcase_18 AC 20 ms
5,376 KB
testcase_19 AC 54 ms
5,376 KB
testcase_20 AC 51 ms
5,376 KB
testcase_21 AC 38 ms
5,376 KB
testcase_22 AC 10 ms
5,376 KB
testcase_23 AC 49 ms
5,376 KB
testcase_24 AC 15 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i64 = long long;

#include "atcoder/modint"
using Fp = atcoder::modint998244353;

int main() {
    int N, M;
    std::cin >> N >> M;
    std::vector<int> A(N);
    for (auto &e : A) {
        std::cin >> e;
    }

    // judge
    i64 sum = 0;
    for (int i = N - 1; i >= 0; --i) {
        sum += A[i] * (i % 2 == (N - 1) % 2 ? -1 : 1);
    }
    if (sum % M != 0) {
        std::cout << -1 << std::endl;
        return 0;
    }

    std::vector<Fp> r1(N + 1);
    r1[0] = 1;
    for (int i = 0; i < N; ++i) {
        r1[i + 1] = r1[i] * (M - 1);
        if (i % 2 == 0) {
            r1[i + 1] -= M - 2;
        }
    }

    auto calc = [&](int l, Fp g, bool x) {
        if (x) {
            if (l % 2 == 0) {
                if (g == 0) {
                    return r1[l];
                } else {
                    return r1[l] - 1;
                }
            } else {
                if (g == M - 1) {
                    return r1[l] - 1;
                } else {
                    return r1[l];
                }
            }
        } else {
            if (l % 2 == 0) {
                if (g == 0) {
                    return r1[l];
                } else {
                    return r1[l] - 1;
                }
            } else {
                if (g == 1) {
                    return r1[l] - 1;
                } else {
                    return r1[l];
                }
            }
        }
    };

    auto contain = [&](int l, int r, int g) {
        if (l > r) {
            r += M;
        }
        if (g < l) {
            g += M;
        }
        return l <= g and g <= r;
    };

    auto calc2 = [&](int len, int l, int r, bool x) {
        Fp res = 0;
        int w = 0;
        if (l <= r) {
            w = r - l + 1;
        } else {
            w = M - l + r + 1;
        }
        if (x) {
            if (len % 2 == 0) {
                res = (r1[len] - 1) * w;
                if (contain(l, r, 0)) {
                    res += 1;
                }
            } else {
                res = r1[len] * w;
                if (contain(l, r, M - 1)) {
                    res -= 1;
                }
            }
        } else {
            if (len % 2 == 0) {
                res = (r1[len] - 1) * w;
                if (contain(l, r, 0)) {
                    res += 1;
                }
            } else {
                res = r1[len] * w;
                if (contain(l, r, 1)) {
                    res -= 1;
                }
            }
        }
        return res;
    };

    int s = 0;
    Fp answer = 0;
    for (int i = 0; i < N; ++i) {
        bool x = i % 2 == (N - 1) % 2 ? true : false;

        if (A[i] != 0) {
            int l = M - s, r = M - s;
            if (x) {
                l -= A[i] - 1;
            } else {
                r += A[i] - 1;
            }
            l = atcoder::internal::safe_mod(l, M);
            r = atcoder::internal::safe_mod(r, M);

            answer += calc2(N - i - 1, l, r, not x);
            /*
            for (int j = 0; j < M; ++j) {
                if (l <= r) {
                    if (l <= j and j <= r) {
                        answer += calc(N - i - 1, j, not x);
                    }
                } else {
                    if (l <= j or j <= r) {
                        answer += calc(N - i - 1, j, not x);
                    }
                }
            }
            */
        }

        if (x) {
            s += A[i];
        } else {
            s -= A[i];
        }
        s = atcoder::internal::safe_mod(s, M);
    }
    answer += 1;

    std::cout << answer.val() << std::endl;
}
0