結果

問題 No.2653 [Cherry 6th Tune] Re: start! (Make it Zero!)
ユーザー hitonanode
提出日時 2024-03-10 17:51:14
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 1,597 bytes
コンパイル時間 886 ms
コンパイル使用メモリ 91,796 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-09-29 21:42:03
合計ジャッジ時間 4,984 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 72
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

#include <atcoder/modint>
using mint = atcoder::modint998244353;

mint solve() {
    int N, M;
    cin >> N >> M;
    vector<int> X(N);
    for (int &x : X) cin >> x;

    long long fixed_sum = 0;
    int nempty = 0;

    for (int x : X) {
        if (x < 0) {
            ++nempty;
        } else {
            fixed_sum += x;
        }
    }

    if (nempty == 1) {
        long long rem = (M - fixed_sum % M) % M;
        for (auto &x : X) {
            if (x < 0) {
                assert(x == -1);
                x = rem;
                --nempty;
                fixed_sum += rem;
            }
        }
    }

    if (!nempty and fixed_sum % M) return 0;

    if (!nempty) {
        mint ret = 0;
        long long current_sum = 0;
        for (int x : X) {
            current_sum += x;
            if (current_sum % M) ret += 1;
        }
        return ret;
    }

    mint ret = 0;

    long long current_sum = 0;
    int current_empty = 0;

    const mint c = mint(M).pow(nempty - 2);

    for (int x : X) {
        if (x < 0) {
            ++current_empty;
        } else {
            current_sum += x;
        }

        if (current_empty == 0) {
            if (current_sum % M) ret += c * M;
        } else if (current_empty == nempty) {
            if ((fixed_sum - current_sum) % M) ret += c * M;
        } else {
            ret += c * (M - 1);
        }
    }

    return ret;
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int T;
    cin >> T;
    while (T--) cout << solve().val() << '\n';
}
0