結果

問題 No.2635 MST on Line++ 2
ユーザー 👑 potato167potato167
提出日時 2024-02-13 05:55:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 1,388 bytes
コンパイル時間 5,003 ms
コンパイル使用メモリ 281,312 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-02-15 23:13:31
合計ジャッジ時間 7,952 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 3 ms
6,676 KB
testcase_05 AC 81 ms
10,752 KB
testcase_06 AC 3 ms
6,676 KB
testcase_07 AC 77 ms
10,240 KB
testcase_08 AC 3 ms
6,676 KB
testcase_09 AC 51 ms
8,192 KB
testcase_10 AC 3 ms
6,676 KB
testcase_11 AC 66 ms
9,472 KB
testcase_12 AC 3 ms
6,676 KB
testcase_13 AC 81 ms
11,008 KB
testcase_14 AC 3 ms
6,676 KB
testcase_15 AC 49 ms
8,064 KB
testcase_16 AC 3 ms
6,676 KB
testcase_17 AC 55 ms
8,576 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 73 ms
10,112 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 76 ms
10,368 KB
testcase_22 AC 3 ms
6,676 KB
testcase_23 AC 50 ms
8,064 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 64 ms
6,912 KB
testcase_27 AC 38 ms
6,676 KB
testcase_28 AC 66 ms
7,168 KB
testcase_29 AC 3 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 68 ms
7,168 KB
testcase_32 AC 59 ms
6,676 KB
testcase_33 AC 55 ms
6,676 KB
testcase_34 AC 81 ms
10,112 KB
testcase_35 AC 74 ms
7,552 KB
testcase_36 AC 83 ms
9,984 KB
testcase_37 AC 78 ms
8,192 KB
testcase_38 AC 83 ms
9,984 KB
testcase_39 AC 2 ms
6,676 KB
testcase_40 AC 2 ms
6,676 KB
testcase_41 AC 2 ms
6,676 KB
testcase_42 AC 2 ms
6,676 KB
testcase_43 AC 2 ms
6,676 KB
testcase_44 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
using namespace std;
const int mod=998244353;
#define rep(i,a,b) for (int i=int(a);i<int(b);i++)
#include<atcoder/all>
using namespace atcoder;
using mint=modint998244353;


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N,K;
	cin>>N>>K;
	vector<int> A(N);
	rep(i,0,N) cin>>A[i];
	sort(A.begin(),A.end());
	vector<mint> D(N);
	rep(i,0,N) D[i]=A[i];
	for(int i=N-1;i>0;i--) D[i]-=D[i-1];
	vector<mint> fact(N+2,1),fact_inv(N+2,1);
	rep(i,0,N+1) fact[i+1]=fact[i]*(i+1);
	fact_inv[N+1]=fact[N+1].inv();
	for(int i=N+1;i>0;i--) fact_inv[i-1]=fact_inv[i]*i;
	auto P=[&](int a,int b) -> mint {
		if(a<0||a<b) return 0;
		return fact[a]*fact_inv[a-b];
	};
	auto C=[&](int a,int b) -> mint {
		if(a<0||a<b) return 0;
		return fact[a]*fact_inv[b]*fact_inv[a-b];
	};
	auto f=[&](int a) -> mint {
		mint res=0;
		rep(j,0,N){
			res+=P(a,j)*D[j]*fact[N-j];
		}
		return res;
	};
	// f(l)+f(l+1)+...+f(r)
	auto g=[&](int l,int r) -> mint {
		mint res=0;
		rep(k,0,N){
			res+=D[k]*fact[N-k]*fact[k]*(C(r+1,k+1)-C(l,k+1));
		}
		return res;
	};
	mint ans=0;
	map<int,int> m;
	int L=N+1,R=-1;
	rep(i,1,N){
		m[N-min(i,K)-min(N-i,K)]++;
	}
	for(auto x:m){
		if(x.second!=2) ans+=f(x.first)*x.second;
		else{
			L=min(L,x.first);
			R=max(R,x.first);
		}
	}
	if(L<=R) ans+=g(L,R)*2;
	cout<<ans.val()<<"\n";
}
0