結果

問題 No.2144 MM
ユーザー CyanmondCyanmond
提出日時 2022-12-02 22:41:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,514 bytes
コンパイル時間 2,244 ms
コンパイル使用メモリ 201,944 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-18 07:32:33
合計ジャッジ時間 4,530 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 59 ms
6,940 KB
testcase_08 AC 70 ms
6,944 KB
testcase_09 AC 60 ms
6,944 KB
testcase_10 WA -
testcase_11 AC 70 ms
6,940 KB
testcase_12 AC 73 ms
6,940 KB
testcase_13 WA -
testcase_14 AC 72 ms
6,944 KB
testcase_15 AC 58 ms
6,940 KB
testcase_16 AC 18 ms
6,940 KB
testcase_17 WA -
testcase_18 AC 19 ms
6,944 KB
testcase_19 WA -
testcase_20 AC 48 ms
6,940 KB
testcase_21 AC 37 ms
6,944 KB
testcase_22 AC 10 ms
6,944 KB
testcase_23 AC 50 ms
6,940 KB
testcase_24 AC 15 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 1 ms
6,944 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 1 ms
6,940 KB
testcase_35 WA -
testcase_36 AC 1 ms
6,944 KB
testcase_37 AC 1 ms
6,944 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 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 (l == 0 or l > r) {
                    res += 1;
                }
            } else {
                res = r1[len] * w;
                if (l == M - 1 or l > r) {
                    res -= 1;
                }
            }
        } else {
            if (len % 2 == 0) {
                res = (r1[len] - 1) * w;
                if (l == 0 or l > r) {
                    res += 1;
                }
            } else {
                res = r1[len] * w;
                if ((l <= 1 and r >= 1) or (l > r and r >= 1) or (l == 1 and r == 0)) {
                    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