結果

問題 No.1621 Sequence Inversions
ユーザー hitonanode
提出日時 2021-07-22 22:04:17
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 21 ms / 3,000 ms
コード長 1,487 bytes
コンパイル時間 2,352 ms
コンパイル使用メモリ 138,068 KB
最終ジャッジ日時 2025-01-23 06:44:34
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)

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

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N, K;
    cin >> N >> K;
    vector<int> A(N);
    for (auto &a : A) cin >> a;
    map<int, int> mp;
    for (auto a : A) mp[a]++;
    int n = 0;
    auto compare = [&](const vector<mint> &l, const vector<mint> &r) { return l.size() > r.size(); };
    priority_queue<vector<mint>, vector<vector<mint>>, decltype(compare)> pq{compare};
    pq.emplace(vector<mint>{1});

    for (auto [k, cnt] : mp) {
        vector dp(cnt + 1, vector<mint>(K + 1));
        dp[0][0] = 1;
        REP(t, n + 1) {
            REP(iuse, dp.size() - 1) REP(k, int(dp[iuse].size()) - t) if (dp[iuse][k].val()) {
                dp[iuse + 1][k + t] += dp[iuse][k];
            }
        }
        auto v = dp.back();
        while (v.size() and v.back() == 0) v.pop_back();
        pq.emplace(v);
        n += cnt;
    }

    while (pq.size() > 1) {
        auto f1 = pq.top();
        pq.pop();
        auto f2 = pq.top();
        pq.pop();
        auto f = atcoder::convolution(f1, f2);
        if (f.size() > K) f.resize(K + 1);
        pq.emplace(f);
    }
    auto f = pq.top();
    cout << (int(f.size()) > K ? f[K] : 0).val() << '\n';
}
0