結果

問題 No.1621 Sequence Inversions
ユーザー momoyuumomoyuu
提出日時 2023-04-23 00:43:49
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 404 ms / 3,000 ms
コード長 2,867 bytes
コンパイル時間 3,344 ms
コンパイル使用メモリ 287,328 KB
実行使用メモリ 60,160 KB
最終ジャッジ日時 2024-04-25 01:49:45
合計ジャッジ時間 5,922 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 4 ms
6,528 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 200 ms
37,012 KB
testcase_09 AC 398 ms
60,160 KB
testcase_10 AC 404 ms
60,032 KB
testcase_11 AC 207 ms
37,012 KB
testcase_12 AC 109 ms
14,404 KB
testcase_13 AC 87 ms
9,416 KB
testcase_14 AC 21 ms
7,704 KB
testcase_15 AC 15 ms
7,680 KB
testcase_16 AC 24 ms
6,912 KB
testcase_17 AC 43 ms
7,680 KB
testcase_18 AC 24 ms
6,656 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 11 ms
5,376 KB
testcase_21 AC 48 ms
7,808 KB
testcase_22 AC 18 ms
7,808 KB
testcase_23 AC 54 ms
7,808 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;

const ll mod = 998'244'353;
//const ll mod = 1'000'000'007;
//const ll mod = 67'280'421'310'721;
struct mint{
    long long x;
    mint(long long x=0):x((x%mod+mod)%mod){}
    mint operator-() const{
        return mint(-x);
    }
    mint& operator+=(const mint& a){
        if((x+=a.x)>=mod)x-=mod;
        return *this;
    }
    mint& operator-=(const mint& a){
        if((x+=mod-a.x)>=mod)x-=mod;
        return *this;
    }
    mint& operator*=(const  mint& a){
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint& a) const{
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint& a) const{
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint& a) const{
        mint res(*this);
        return res*=a;
    }
    mint pow(long long n) const {
        assert(0 <= n);
        mint a = *this, r = 1;
        while (n) {
            if (n & 1) r *= a;
            a *= a;
            n >>= 1;
        }
        return r;
    }
    mint inv() const{
        return pow(mod-2);
    }
    mint& operator/=(const mint& a){
        return (*this)*=a.inv();
    }
    mint operator/(const mint& a) const {
        mint res(*this);
        return res/=a;
    }
    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
    bool operator==(const mint& a) const {
        return x == a.x;
    }
    bool operator<(const mint& a) const{
        return x < a.x;
    }
};

int main(){
    int n,k;
    cin>>n>>k;
    vector<int> a(n);
    for(int i = 0;i<n;i++) cin>>a[i];
    sort(a.begin(),a.end());
    vector<vector<mint>> dp(n+1,vector<mint>(n*(n-1)/2+1,0));
    dp[0][0] = 1;
    int cur = 0;
    for(int i = 0;i<n;){
        int ni = lower_bound(a.begin(),a.end(),a[i]) - a.begin();
        int nj = upper_bound(a.begin(),a.end(),a[i]) - a.begin();
        int now = nj - ni;
        int nn = i;
        int mx = nn * now;
        if(i==0){
            i = nj;
            continue;
        }
        i = nj;
        vector<vector<vector<mint>>>cnt(nn+2,vector<vector<mint>>(now+1,vector<mint>(mx+1,0)));
        cnt[0][0][0] = 1;
        for(int j = 0;j<=nn;j++){
            for(int l = now;l>=0;l--){
                //for(int m = 0;m<=mx;m++) cnt[j+1][l][m] += cnt[j][l][m];
                for(int m = now;m>=0;m--){
                    if(m+l>now) continue;
                    for(int kk = mx;kk>=0;kk--)if(kk+m*j<=mx) cnt[j+1][m+l][kk+m*j] += cnt[j][l][kk];
                }
            }
        }
        for(int kk = 0;kk<=k;kk++){
            for(int j = 0;j<=mx;j++){
                if(kk+j>k) continue;
                dp[cur+1][kk+j] += dp[cur][kk]*cnt[nn+1][now][j];
            }
        }
        cur++;
    }
    cout<<dp[cur][k]<<endl;
}

0