結果

問題 No.1621 Sequence Inversions
ユーザー rianoriano
提出日時 2021-07-22 22:24:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 3,000 ms
コード長 2,431 bytes
コンパイル時間 1,723 ms
コンパイル使用メモリ 181,816 KB
実行使用メモリ 11,208 KB
最終ジャッジ日時 2023-09-24 17:34:37
合計ジャッジ時間 3,833 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 42 ms
6,180 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 34 ms
6,296 KB
testcase_09 AC 53 ms
7,288 KB
testcase_10 AC 125 ms
11,208 KB
testcase_11 AC 39 ms
6,532 KB
testcase_12 AC 105 ms
10,948 KB
testcase_13 AC 104 ms
11,052 KB
testcase_14 AC 11 ms
4,488 KB
testcase_15 AC 7 ms
4,376 KB
testcase_16 AC 24 ms
5,864 KB
testcase_17 AC 51 ms
7,656 KB
testcase_18 AC 22 ms
5,712 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 12 ms
4,704 KB
testcase_21 AC 57 ms
8,308 KB
testcase_22 AC 12 ms
4,528 KB
testcase_23 AC 73 ms
9,420 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<ll,ll,ll>
using Graph = vector<vector<int>>;

const ll mod = 998244353;


//累乗 aのb乗、正しmを法として求める
long long pw(long long a,long long b,long long m){
    if(b==0) return 1;
    else if(b%2==0){
        long long x = pw(a,b/2,m);
        return (x*x)%m;
    }
    else{
        long long x = pw(a,b-1,m);
        return (a*x)%m;
    }
}



//mod逆元
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}


//Combination2
//10^6くらいまで
//modはグローバルに定義しておく

vector<ll> fact;
vector<ll> invf;
ll comb(ll n,ll k){
    if(n<0||k<0||k>n) return 0LL;
    else{
        ll a = fact[n]*invf[k]%mod;
        a = a*invf[n-k]%mod;
        return a;
    }
}
    

int main() {
    ll N,K; cin >> N >> K;
    //main関数内に以下ペースト
    //N:max
    fact.assign(N+1,1LL);
    invf.assign(N+1,1LL);
    rep(i,N) fact[i+1] = fact[i]*(i+1)%mod;
    rep(i,N+1) invf[i] = modinv(fact[i],mod);
    map<ll,ll> cnt; set<ll> app;
    rep(i,N){
        ll b; cin >> b;
        app.insert(b);
        if(cnt.count(b)) cnt[b]++;
        else cnt[b] = 1;
    }
    ll dp[N+1][K+1];
    rep(i,N+1){
        rep(j,K+1) dp[i][j] = 0;
    }
    dp[0][0] = 1;
    rep(i,N){
        rep(j,K+1){
            dp[i][j] %= mod;
            rep(k,N-i){
                if(j+k>K) break;
                dp[i+1][j+k] += dp[i][j];
                dp[i+1][j+k] %= mod;
            }
        }
    }
    ll db[N+1][K+1];
    rep(i,N+1){
        rep(j,K+1) db[i][j] = 0;
    }
    db[0][0] = 1; int j = 0;
    for(ll x:app){
        int t = cnt[x];
        rep(i,t){
            rep(k,K+1){
                rep(m,i+1){
                    if(k+m>K) break;
                    db[j+1][k+m] += db[j][k];
                    db[j+1][k+m] %= mod;
                }
            }
            j++;
        }
    }
    rep(i,K+1){
        rep(j,K+1){
            if(j==0) continue;
            if(i+j>K) break;
            dp[N][i+j] += (mod-(dp[N][i]*db[N][j])%mod);
            dp[N][i+j] %= mod;
        }
    }
    ll ans = dp[N][K];
    cout << ans << endl;
}
0