結果
| 問題 |
No.2144 MM
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-12-02 23:02:35 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 83 ms / 2,000 ms |
| コード長 | 3,659 bytes |
| コンパイル時間 | 2,151 ms |
| コンパイル使用メモリ | 199,452 KB |
| 最終ジャッジ日時 | 2025-02-09 04:18:27 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 |
ソースコード
#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;
}