Submission #44331312


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

#define de(x) cout<<#x<<"="<<x<<endl

using ll = long long;

ll fs(ll n,ll m,ll a,ll b){
	ll res=0;
	if (a>=m){
		res+=n*(n+1)/2*(a/m),a%=m;
	}
	if (b>=m){
		res+=(n+1)*(b/m),b%=m;
	}
	ll c=(a*n+b)/m;
	if (!c){
		return res;
	}
	res+=n*c-fs(c-1,a,m,m-b-1);
	return res;
}

const int N = 2e5+5;
const ll mod = 998244353;

ll n,a[N];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin>>n;
	for (int i=0; i<n; i++){
		cin>>a[i];
	}
	sort(a,a+n);
	ll s=a[0]*n,ans=(a[0]+1)%mod;
	for (int i=1; i<n; i++){
		ll l=a[i-1],r=a[i],A=n-i,B=s-l*(n-i),m=n;
		ll ad=fs(r,m,A,B)-fs(l,m,A,B)+r-l;
		(ans+=ad)%=mod,s+=(r-l)*(n-i);
	}
	cout<<ans<<endl;
	return 0;
}

// don't waste time!!!

Submission Info

Submission Time
Task G - Redistribution of Piles
User SFlyer
Language C++ (GCC 9.2.1)
Score 625
Code Size 780 Byte
Status AC
Exec Time 148 ms
Memory 5260 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 625 / 625
Status
AC × 4
AC × 30
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 02_n_small_00.txt, 02_n_small_01.txt, 02_n_small_02.txt, 02_n_small_03.txt, 02_n_small_04.txt, 03_random_00.txt, 03_random_01.txt, 03_random_02.txt, 03_random_03.txt, 03_random_04.txt, 03_random_05.txt, 03_random_06.txt, 03_random_07.txt, 03_random_08.txt, 03_random_09.txt, 04_corner_00.txt, 04_corner_01.txt, 04_corner_02.txt, 04_corner_03.txt, 04_corner_04.txt, 04_corner_05.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 6 ms 3540 KiB
00_sample_01.txt AC 2 ms 3532 KiB
00_sample_02.txt AC 2 ms 3644 KiB
00_sample_03.txt AC 2 ms 3528 KiB
01_small_00.txt AC 3 ms 3608 KiB
01_small_01.txt AC 2 ms 3608 KiB
01_small_02.txt AC 2 ms 3648 KiB
01_small_03.txt AC 2 ms 3620 KiB
01_small_04.txt AC 2 ms 3592 KiB
02_n_small_00.txt AC 2 ms 3536 KiB
02_n_small_01.txt AC 7 ms 3624 KiB
02_n_small_02.txt AC 2 ms 3648 KiB
02_n_small_03.txt AC 3 ms 3552 KiB
02_n_small_04.txt AC 2 ms 3536 KiB
03_random_00.txt AC 125 ms 4832 KiB
03_random_01.txt AC 145 ms 5132 KiB
03_random_02.txt AC 143 ms 5048 KiB
03_random_03.txt AC 146 ms 5108 KiB
03_random_04.txt AC 115 ms 4732 KiB
03_random_05.txt AC 148 ms 5152 KiB
03_random_06.txt AC 105 ms 4648 KiB
03_random_07.txt AC 145 ms 5080 KiB
03_random_08.txt AC 136 ms 4984 KiB
03_random_09.txt AC 145 ms 5152 KiB
04_corner_00.txt AC 26 ms 5260 KiB
04_corner_01.txt AC 137 ms 5208 KiB
04_corner_02.txt AC 3 ms 3544 KiB
04_corner_03.txt AC 139 ms 5156 KiB
04_corner_04.txt AC 86 ms 4552 KiB
04_corner_05.txt AC 134 ms 5104 KiB