Submission #46637085


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'||
ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
//mt19937 rand(time(0));
//mt19937_64 rand(time(0));
//srand(time(0));
#define N 5010
//#define M
#define mo 998244353
int pw(int a, int b) {
	int ans=1; 
	while(b) {
		if(b&1) ans*=a; 
		a*=a; b>>=1; 
		ans%=mo; a%=mo; 
	}
	return ans; 
}
int fac[N], inv[N], ifac[N]; 
void init(int n) {
	int i; 
	for(i=fac[0]=1; i<=n; ++i) fac[i]=fac[i-1]*i%mo; 
	ifac[n]=pw(fac[n], mo-2); 
	for(i=n-1; i>=0; --i) ifac[i]=ifac[i+1]*(i+1)%mo; 
    for(i=1; i<=n; ++i) inv[i]=ifac[i]*fac[i-1]%mo; 
}
int C(int n, int m) {
	if(m>n) return 0;
	return fac[n]*ifac[m]%mo*ifac[n-m]%mo; 
}
void Add(int &a, int b) {
	a+=b; if(a>=mo || a<=-mo) a%=mo; 
    if(a<0) a+=mo; 
}
void Mul(int &a, int b) {
	a*=b; if(a>=mo || a<=-mo) a%=mo; 
    if(a<0) a+=mo; 
}
void Mod(int &a) {
	if(a>=mo || a<=-mo) a%=mo; 
    if(a<0) a+=mo; 
}
const int iv2=pw(2, mo-2); 
int n, m, i, j, k, T;
int ans, len1, len2, len3, pos, a[N], b[N], s1, s2; 
int g[N]; 

signed main()
{
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
//	T=read();
//	while(T--) {
//
//	}
	n=read(); k=read(); init(n); 
	for(i=1; i<=n; ++i) a[i]=read(); 
	sort(a+1, a+n+1); 
	for(i=1; i<=n; ++i) b[i]=a[i]*fac[i-1]%mo*fac[n-i]%mo; 
	for(len1=1; len1<=k; ++len1) 
		for(i=2; i<=n; ++i) Add(ans, b[i]*C(n-len1-1, i-2)%mo*(n-len1)*2%mo); 
	for(pos=1; pos<=n; ++pos) 
			for(len3=2; len3<n; ++len3) 	
					if(len3<=k) {						
						s1=len3-pos+1; s2=len3-n+pos; 
						s1=max(1ll, s1); s2=max(1ll, s2); 
						if(s1+s2>len3) continue; 
						Add(g[len3], len3-s1-s2+1); 
//						Add(ans, -b[i]*C(n-len3-1, i-3)%mo*(len3-s1-s2+1)%mo); 
					}
		
	for(i=3; i<=n; ++i) 
		for(len3=2; len3<n; ++len3) 
			Add(ans, -b[i]*C(n-len3-1, i-3)%mo*g[len3]%mo); 
	printf("%lld", ans); 
	return 0;
}

Submission Info

Submission Time
Task C - MST on Line++
User zhangtingxi
Language C++ 20 (gcc 12.2)
Score 700
Code Size 2106 Byte
Status AC
Exec Time 323 ms
Memory 4020 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 3
AC × 34
Set Name Test Cases
Sample 01-sample-01.txt, 01-sample-02.txt, 01-sample-03.txt
All 01-sample-01.txt, 01-sample-02.txt, 01-sample-03.txt, 02-min-01.txt, 03-max-01.txt, 03-max-02.txt, 03-max-03.txt, 03-max-04.txt, 03-max-05.txt, 04-rand-01.txt, 04-rand-02.txt, 04-rand-03.txt, 04-rand-04.txt, 04-rand-05.txt, 05-large-rand-01.txt, 05-large-rand-02.txt, 05-large-rand-03.txt, 05-large-rand-04.txt, 05-large-rand-05.txt, 05-large-rand-06.txt, 05-large-rand-07.txt, 05-large-rand-08.txt, 05-large-rand-09.txt, 05-large-rand-10.txt, 06-K-small-01.txt, 06-K-small-02.txt, 06-K-small-03.txt, 06-K-small-04.txt, 06-K-small-05.txt, 07-K-large-01.txt, 07-K-large-02.txt, 07-K-large-03.txt, 07-K-large-04.txt, 07-K-large-05.txt
Case Name Status Exec Time Memory
01-sample-01.txt AC 1 ms 3772 KiB
01-sample-02.txt AC 1 ms 3652 KiB
01-sample-03.txt AC 1 ms 3640 KiB
02-min-01.txt AC 1 ms 3772 KiB
03-max-01.txt AC 323 ms 3892 KiB
03-max-02.txt AC 170 ms 3956 KiB
03-max-03.txt AC 209 ms 3792 KiB
03-max-04.txt AC 261 ms 3836 KiB
03-max-05.txt AC 238 ms 3804 KiB
04-rand-01.txt AC 9 ms 3752 KiB
04-rand-02.txt AC 40 ms 3732 KiB
04-rand-03.txt AC 160 ms 4020 KiB
04-rand-04.txt AC 78 ms 3896 KiB
04-rand-05.txt AC 10 ms 3880 KiB
05-large-rand-01.txt AC 299 ms 3988 KiB
05-large-rand-02.txt AC 255 ms 3984 KiB
05-large-rand-03.txt AC 238 ms 3872 KiB
05-large-rand-04.txt AC 309 ms 3856 KiB
05-large-rand-05.txt AC 297 ms 3860 KiB
05-large-rand-06.txt AC 284 ms 3820 KiB
05-large-rand-07.txt AC 288 ms 3980 KiB
05-large-rand-08.txt AC 298 ms 3820 KiB
05-large-rand-09.txt AC 281 ms 3980 KiB
05-large-rand-10.txt AC 294 ms 3816 KiB
06-K-small-01.txt AC 95 ms 3864 KiB
06-K-small-02.txt AC 99 ms 4016 KiB
06-K-small-03.txt AC 90 ms 3944 KiB
06-K-small-04.txt AC 101 ms 3820 KiB
06-K-small-05.txt AC 102 ms 3796 KiB
07-K-large-01.txt AC 214 ms 3828 KiB
07-K-large-02.txt AC 214 ms 3900 KiB
07-K-large-03.txt AC 259 ms 3888 KiB
07-K-large-04.txt AC 310 ms 3920 KiB
07-K-large-05.txt AC 236 ms 3840 KiB