結果

問題 No.2635 MST on Line++ 2
ユーザー noya2noya2
提出日時 2024-02-15 21:09:11
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 1,760 bytes
コンパイル時間 5,197 ms
コンパイル使用メモリ 312,972 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-15 23:13:56
合計ジャッジ時間 7,707 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,676 KB
testcase_01 AC 5 ms
6,676 KB
testcase_02 AC 5 ms
6,676 KB
testcase_03 AC 5 ms
6,676 KB
testcase_04 AC 5 ms
6,676 KB
testcase_05 AC 29 ms
6,676 KB
testcase_06 AC 5 ms
6,676 KB
testcase_07 AC 29 ms
6,676 KB
testcase_08 AC 5 ms
6,676 KB
testcase_09 AC 21 ms
6,676 KB
testcase_10 AC 5 ms
6,676 KB
testcase_11 AC 25 ms
6,676 KB
testcase_12 AC 5 ms
6,676 KB
testcase_13 AC 31 ms
6,676 KB
testcase_14 AC 5 ms
6,676 KB
testcase_15 AC 21 ms
6,676 KB
testcase_16 AC 5 ms
6,676 KB
testcase_17 AC 22 ms
6,676 KB
testcase_18 AC 5 ms
6,676 KB
testcase_19 AC 29 ms
6,676 KB
testcase_20 AC 5 ms
6,676 KB
testcase_21 AC 29 ms
6,676 KB
testcase_22 AC 5 ms
6,676 KB
testcase_23 AC 21 ms
6,676 KB
testcase_24 AC 5 ms
6,676 KB
testcase_25 AC 5 ms
6,676 KB
testcase_26 AC 28 ms
6,676 KB
testcase_27 AC 20 ms
6,676 KB
testcase_28 AC 28 ms
6,676 KB
testcase_29 AC 5 ms
6,676 KB
testcase_30 AC 5 ms
6,676 KB
testcase_31 AC 45 ms
6,676 KB
testcase_32 AC 48 ms
6,676 KB
testcase_33 AC 40 ms
6,676 KB
testcase_34 AC 35 ms
6,676 KB
testcase_35 AC 44 ms
6,676 KB
testcase_36 AC 36 ms
6,676 KB
testcase_37 AC 42 ms
6,676 KB
testcase_38 AC 31 ms
6,676 KB
testcase_39 AC 4 ms
6,676 KB
testcase_40 AC 5 ms
6,676 KB
testcase_41 AC 5 ms
6,676 KB
testcase_42 AC 5 ms
6,676 KB
testcase_43 AC 5 ms
6,676 KB
testcase_44 AC 5 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using ll = long long;
using mint = atcoder::modint998244353;

const int mx = 200100;
mint fact[mx], ifact[mx];

void table_build(){
    fact[0] = 1;
    for (int i = 1; i < mx; i++){
        fact[i] = fact[i-1] * i;
    }
    ifact[mx-1] = fact[mx-1].inv();
    for (int i = mx-2; i >= 0; i--){
        ifact[i] = ifact[i+1] * (i+1);
    }
}

mint binom(int n, int r){
    if (n < 0 || n < r || r < 0) return 0;
    return fact[n] * ifact[r] * ifact[n-r];
}
mint inv(int n){
    if (n <= 0) return 0;
    return ifact[n] * fact[n-1];
}

auto get0(int m, int r){
    if (m < 0 || r < 0) return mint(0);
    return binom(m+r+1,r+1);
};
auto get1(int m, int r){
    if (m < 0 || r < 0) return mint(0);
    return binom(r+m+1,r) * m * (m+1) * inv(r+2);
};
auto get2(int m, int r){
    if (m < 0 || r < 0) return mint(0);
    return binom(r+m+1,r) * m * (m+1) * (ll(r+2)*m+1) * inv(r+2) * inv(r+3);
};

void input(int &x){
    static char c;
    x = 0;
    while ((c = getchar_unlocked() ^ 48) < 10){
        x *= 10;
        x += c;
    }
}

int main(){
    table_build();
    int n, k; input(n), input(k);
    vector<int> a(n);
    for (auto &x : a) input(x);
    sort(a.begin(),a.end());
    for (int i = n-2; i >= 0; i--){
        a[i+1] -= a[i];
    }
    mint ans = fact[n] * (n-1) * a[0];
    for (int i = 1; i < n; i++){
        mint md = get0(n-2*k-i,i-2) * (i-1) * (n-2*k-i+1) 
                + get1(n-2*k-i,i-2) * (-(i-1) + n-2*k-i+1)
                + get2(n-2*k-i,i-2) * (-1);
        mint lr = get0(n-k-i-1,i-1) * (n-k-i)
                + get1(n-k-i-1,i-1) * (-1);
        mint sum = md + lr*2;
        ans += sum * fact[i] * fact[n-i] * a[i];
    }
    cout << ans.val() << '\n';
}
0