結果

問題 No.2144 MM
ユーザー CyanmondCyanmond
提出日時 2022-12-02 22:41:33
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 3,514 bytes
コンパイル時間 2,239 ms
コンパイル使用メモリ 199,340 KB
最終ジャッジ日時 2025-02-09 04:05:25
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30 WA * 5
権限があれば一括ダウンロードができます

ソースコード

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