結果
| 問題 |
No.2149 Vanitas Vanitatum
|
| コンテスト | |
| ユーザー |
Kude
|
| 提出日時 | 2022-12-06 07:32:16 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 50 ms / 2,000 ms |
| コード長 | 2,500 bytes |
| コンパイル時間 | 2,236 ms |
| コンパイル使用メモリ | 203,396 KB |
| 最終ジャッジ日時 | 2025-02-09 05:41:41 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 24 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#include <atcoder/modint>
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) {a = max(a, b);}
template<class T> void chmin(T& a, const T& b) {a = min(a, b);}
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint998244353;
constexpr int FACT_SIZE = 4010000;
mint Fact[FACT_SIZE + 1];
mint iFact[FACT_SIZE + 1];
const auto fact_init = [] {
Fact[0] = mint::raw(1);
for(int i = 1; i <= FACT_SIZE; ++i) {
Fact[i] = Fact[i-1] * i;
}
iFact[FACT_SIZE] = Fact[FACT_SIZE].inv();
for(int i = FACT_SIZE; i; --i) {
iFact[i-1] = iFact[i] * i;
}
return false;
}();
mint comb(int n, int k) {
if (k == 0) return mint::raw(1);
assert(n >= 0 && k >= 0);
if (k > n) return mint::raw(0);
return Fact[n] * iFact[n - k] * iFact[k];
}
mint icomb(int n, int k) {
return iFact[n] * Fact[n - k] * Fact[k];
}
mint fact(int n) {return Fact[n];}
mint perm(int n, int k) {
assert(0 <= n);
return Fact[n] * iFact[n - k];
}
mint hook(const VI& a) {
if (a.empty()) return 1;
int n = a.size();
int tot = accumulate(all(a), 0);
mint res = Fact[tot];
VI ends(*max_element(all(a)) + 1);
for(int x: a) ends[x]++;
mint v = 1;
rep(i, n) {
int now = n - i + a[i];
rep(j, a[i]) {
now -= ends[j] + 1;
v *= now;
}
}
return res / v;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
VI d;
int pre = 0;
rep(_, n) {
int a;
cin >> a;
rrep(_, a - pre) d.emplace_back(1);
d.emplace_back(0);
pre = a;
}
int sz = d.size();
VI d0;
VI d1;
rep(b, 2) {
for(int i = sz - 1 - b; i >= 0; i -= 2) d0.emplace_back(d[i]);
swap(d0, d1);
}
if (abs(2 * accumulate(all(d0), 0) - (1 + 2 * accumulate(all(d1), 0))) > 1) {
cout << 0 << '\n';
return 0;
}
mint ans = 1;
int cnt0 = 0, cnt1 = 0;
rep(_, 2) {
VI p;
int now = 0;
for(int x: d0) {
if (x == 0) now++;
else p.emplace_back(now);
}
reverse(all(p));
ans *= hook(p);
cnt0 = accumulate(all(p), 0);
swap(cnt0, cnt1);
swap(d0, d1);
}
ans *= comb(cnt0 + cnt1, cnt0);
cout << ans.val() << '\n';
}
Kude