結果

問題 No.2149 Vanitas Vanitatum
ユーザー 👑 hitonanodehitonanode
提出日時 2022-12-06 02:03:29
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,821 bytes
コンパイル時間 1,724 ms
コンパイル使用メモリ 116,804 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 23:09:16
合計ジャッジ時間 2,176 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 5 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#include <atcoder/modint>
using mint = atcoder::modint998244353;
template <class T> T hook_length_formula(std::vector<int> hook) {
    if (hook.empty()) return T(1);
    std::sort(hook.begin(), hook.end());
    std::vector<int> len(hook.back());
    T num(1), den(1);
    int sum = 0;
    for (int l : hook) {
        for (int j = 0; j < l; ++j) num *= ++sum, ++len.at(j), den *= len.at(j) + l - 1 - j;
    }
    return num / den;
}

pair<int, mint> solve(const vector<int> &v) {
    vector<int> hook;
    int n0 = 0;
    int total_move = 0;
    for (auto x : v) {
        if (x == 0) {
            ++n0;
        } else if (n0) {
            total_move += n0;
            hook.push_back(n0);
        }
    }
    mint ret = hook_length_formula<mint>(hook);
    return {total_move, ret};
}

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<int> A(N);
    for (auto &x : A) cin >> x;
    if (accumulate(A.cbegin(), A.cend(), 0) % 2) {
        puts("0");
        return 0;
    }
    for (int i = N - 1; i > 0; --i) A.at(i) -= A.at(i - 1);
    vector<int> seq;
    for (auto a : A) {
        seq.resize(seq.size() + a, 0);
        seq.push_back(1);
    }
    vector<int> v0, v1;
    for (int i = 0; i < int(seq.size()); ++i) {
        (i % 2 ? v1 : v0).push_back(seq.at(i));
    }
    const int n0 = count(v0.cbegin(), v0.cend(), 1);
    const int n1 = count(v1.cbegin(), v1.cend(), 1);
    if (n0 < n1 or n1 + 1 < n0) {
        puts("0");
        return 0;
    }
    auto [len0, sol0] = solve(v0);
    auto [len1, sol1] = solve(v1);
    mint ret = sol0 * sol1;
    mint den = 1;
    for (int i = 0; i < len0; ++i) ret *= len0 + len1 - i, den *= i + 1;
    cout << (ret / den).val() << endl;
}
0