結果

問題 No.2149 Vanitas Vanitatum
ユーザー 👑 hitonanodehitonanode
提出日時 2022-12-06 02:11:15
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,797 bytes
コンパイル時間 1,254 ms
コンパイル使用メモリ 117,396 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-20 23:14:06
合計ジャッジ時間 2,020 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

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

// Hook length formula
// Complexity: O(sum(hook))
// Verify: https://yukicoder.me/problems/no/2149
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, 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> seq;
    int lasta = 0;
    for (int i = 0; i < N; ++i) {
        int a;
        cin >> a;
        seq.resize(seq.size() + a - lasta, 0);
        seq.push_back(1);
        lasta = a;
    }

    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