結果

問題 No.2149 Vanitas Vanitatum
ユーザー KudeKude
提出日時 2022-12-06 07:32:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 2,500 bytes
コンパイル時間 2,236 ms
コンパイル使用メモリ 207,196 KB
実行使用メモリ 34,972 KB
最終ジャッジ日時 2023-08-03 03:06:25
合計ジャッジ時間 5,669 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
34,888 KB
testcase_01 AC 47 ms
34,972 KB
testcase_02 AC 47 ms
34,712 KB
testcase_03 AC 47 ms
34,700 KB
testcase_04 AC 47 ms
34,680 KB
testcase_05 AC 47 ms
34,736 KB
testcase_06 AC 47 ms
34,900 KB
testcase_07 AC 47 ms
34,756 KB
testcase_08 AC 47 ms
34,684 KB
testcase_09 AC 47 ms
34,740 KB
testcase_10 AC 48 ms
34,704 KB
testcase_11 AC 47 ms
34,700 KB
testcase_12 AC 47 ms
34,780 KB
testcase_13 AC 47 ms
34,860 KB
testcase_14 AC 48 ms
34,780 KB
testcase_15 AC 47 ms
34,844 KB
testcase_16 AC 47 ms
34,696 KB
testcase_17 AC 48 ms
34,720 KB
testcase_18 AC 47 ms
34,724 KB
testcase_19 AC 48 ms
34,928 KB
testcase_20 AC 49 ms
34,732 KB
testcase_21 AC 47 ms
34,736 KB
testcase_22 AC 48 ms
34,916 KB
testcase_23 AC 48 ms
34,868 KB
testcase_24 AC 48 ms
34,788 KB
testcase_25 AC 47 ms
34,732 KB
testcase_26 AC 49 ms
34,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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';
}
0