CF1444B Divide and Sum 题解 数数题 思维 数学

Divide and Sum

传送门

You are given an array a a a of length 2 n 2n 2n . Consider a partition of array a a a into two subsequences p p p and q q q of length n n n each (each element of array a a a should be in exactly one subsequence: either in p p p or in q q q ).

Let’s sort p p p in non-decreasing order, and q q q in non-increasing order, we can denote the sorted versions by x x x and y y y , respectively. Then the cost of a partition is defined as f ( p , q ) = ∑ i = 1 n ∣ x i − y i ∣ f(p, q) = \sum_{i = 1}^n |x_i - y_i| f(p,q)=i=1nxiyi .

Find the sum of f ( p , q ) f(p, q) f(p,q) over all correct partitions of array a a a . Since the answer might be too big, print its remainder modulo 998244353 998244353 998244353 .

Input

The first line contains a single integer n n n ( 1 ≤ n ≤ 150   000 1 \leq n \leq 150\,000 1n150000 ).

The second line contains 2 n 2n 2n integers a 1 , a 2 , … , a 2 n a_1, a_2, \ldots, a_{2n} a1,a2,,a2n ( 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1ai109 ) — elements of array a a a .

Output

Print one integer — the answer to the problem, modulo 998244353 998244353 998244353 .

Examples

input #1

1
1 4

output #1

6

input #2

2
2 1 2 1

output #2

12

input #3

3
2 2 2 2 2 2

output #3

0

input #4

5
13 8 35 94 9284 34 54 69 123 846

output #4

2588544

Note

Two partitions of an array are considered different if the sets of indices of elements included in the subsequence p p p are different.

In the first example, there are two correct partitions of the array a a a :

  1. p = [ 1 ] p = [1] p=[1] , q = [ 4 ] q = [4] q=[4] , then x = [ 1 ] x = [1] x=[1] , y = [ 4 ] y = [4] y=[4] , f ( p , q ) = ∣ 1 − 4 ∣ = 3 f(p, q) = |1 - 4| = 3 f(p,q)=∣14∣=3 ;
  2. p = [ 4 ] p = [4] p=[4] , q = [ 1 ] q = [1] q=[1] , then x = [ 4 ] x = [4] x=[4] , y = [ 1 ] y = [1] y=[1] , f ( p , q ) = ∣ 4 − 1 ∣ = 3 f(p, q) = |4 - 1| = 3 f(p,q)=∣41∣=3 .

In the second example, there are six valid partitions of the array $ a $ :

  1. p = [ 2 , 1 ] p = [2, 1] p=[2,1] , q = [ 2 , 1 ] q = [2, 1] q=[2,1] (elements with indices 1 1 1 and 2 2 2 in the original array are selected in the subsequence p p p );
  2. p = [ 2 , 2 ] p = [2, 2] p=[2,2] , q = [ 1 , 1 ] q = [1, 1] q=[1,1] ;
  3. p = [ 2 , 1 ] p = [2, 1] p=[2,1] , q = [ 1 , 2 ] q = [1, 2] q=[1,2] (elements with indices 1 1 1 and 4 4 4 are selected in the subsequence p p p );
  4. p = [ 1 , 2 ] p = [1, 2] p=[1,2] , q = [ 2 , 1 ] q = [2, 1] q=[2,1] ;
  5. p = [ 1 , 1 ] p = [1, 1] p=[1,1] , q = [ 2 , 2 ] q = [2, 2] q=[2,2] ;
  6. p = [ 2 , 1 ] p = [2, 1] p=[2,1] , q = [ 2 , 1 ] q = [2, 1] q=[2,1] (elements with indices 3 3 3 and 4 4 4 are selected in the subsequence p p p ).

题面翻译

给一个长度为 2 n 2n 2n 的数列 a a a,将 a a a 中的数分为两串长度为 n n n 的数列 p p p q q q

P S : p , q PS:p,q PS:p,q不一定要是数列 a a a 中连续的数列。
例: a = { 1 , 2 , 3 , 4 } a=\{1,2,3,4\} a={1,2,3,4},那么可以为 p = { 1 , 3 } , q = { 4 , 2 } p=\{1,3\},q=\{4,2\} p={1,3},q={4,2},此时 f ( p , q ) = ∣ 1 − 4 ∣ + ∣ 3 − 2 ∣ = 3 + 1 = 4 f(p,q)=|1-4|+|3-2|=3+1=4 f(p,q)=∣14∣+∣32∣=3+1=4

我们按非递减的顺序对数列 p p p 排序,而数列 q q q 按非递增的顺序排序。然后我们定义 f ( p , q ) = ∑ i = 1 n ∣ p i − q i ∣ f(p,q)=\sum_{i=1}^{n}|p_i-q_i| f(p,q)=i=1npiqi

求对于所有的 p , q p,q p,q f ( p , q ) f(p,q) f(p,q) 的值之和。答案对 998244353 998244353 998244353 取模。

注明

以上来自 C o d e F o r c e s ,翻译来源:洛谷。 以上来自 CodeForces,翻译来源:洛谷。 以上来自CodeForces,翻译来源:洛谷。

BestMonkey:不调侃了。

解题思路

看题一万年不会,直接推。设假如前 T T T 个数都满足 q i ≥ p i q_i\geq p_i qipi i ∈ [ 1 , k ] i\in [1,k] i[1,k],取最大的 T T T,那么就有:

∑ i = 1 n ∣ p i − q i ∣ = ∑ i = 1 T ( q i − p i ) + ∑ i = T + 1 n ( p i − q i ) = ∑ i = 1 T q i − ∑ i = 1 T p i + ∑ i = T + 1 n p i − ∑ i = T + 1 n q i = ∑ i − 1 n ( q i − p i ) + 2 × ∑ i = 1 T ( q i − p i ) \sum_{i=1}^n|p_i-q_i| \\=\sum_{i=1}^{T}(q_i-p_i)+\sum_{i=T+1}^{n}(p_i-q_i) \\=\sum_{i=1}^{T}q_i-\sum_{i=1}^{T}p_i+\sum_{i=T+1}^{n}p_i-\sum_{i=T+1}^{n}q_i \\=\sum_{i-1}^{n}(q_i-p_i)+2\times \sum_{i=1}^{T}(q_i-p_i) i=1npiqi=i=1T(qipi)+i=T+1n(piqi)=i=1Tqii=1Tpi+i=T+1npii=T+1nqi=i1n(qipi)+2×i=1T(qipi)

显然, ∑ i = 1 n ( q i − p i ) = 0 \sum_{i=1}^{n}(q_i-p_i)=0 i=1n(qipi)=0,所以原式就可以化为: 2 × ∑ i = 1 T ( q i − p i ) 2\times \sum_{i=1}^{T}(q_i-p_i) 2×i=1T(qipi)

为了满足 q i ≥ p i q_i\geq p_i qipi i ∈ [ 1 , k ] i\in [1,k] i[1,k],那么 q 1 ∼ T q_{1\sim T} q1T 就只能取前 n n n 大的数, q T + 1 ∼ n q_{T+1\sim n} qT+1n 只能取前 n n n 大的数。

现在我们需要知道在前 n n n 大的数里面选 T T T 的数的所有方案的 q i q_i qi 之和,那么只需要知道每个数出现的次数即可,显然每个数出现的次数都是 T × ( n T ) n \frac{T\times \begin{pmatrix}n\\T\end{pmatrix}}{n} nT×(nT)

同理可得 ∑ i = 1 T p i \sum_{i=1}^{T}p_i i=1Tpi

AC Code

#include<bits/stdc++.h>
using namespace std;
char buf[1048576], *p1, *p2;
template<typename T>inline void Super_Quick_Read(T &x) {
	bool f = 1;
	x = 0;
	char ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++);
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = !f;
		ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++);
	}
	while (ch >= '0' && ch <= '9')x = (x << 1) + (x << 3) + (ch ^ 48), ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++);
	x = (f ? x : -x);
	return;
}
template<typename T>inline void Quick_Write(T x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) Quick_Write(x / 10);
	putchar(x % 10 + '0');
	return;
}
int n;
long long a[300005];
long long fac[300005], inv[300005], finv[300005];
long long Answer_1, Answer_2;
inline long long C(long long n, long long m) {
	return fac[n] * finv[m] % 998244353 * finv[n - m] % 998244353;
}
signed main() {
	Super_Quick_Read(n), fac[0] = inv[0] = finv[0] = 1, fac[1] = inv[1] = finv[1] = 1;
	for (register int i = 2; i <= n; ++i) fac[i] = fac[i - 1] * i % 998244353, inv[i] = (998244353 - 998244353 / i) * inv[998244353 % i] % 998244353, finv[i] = finv[i - 1] * inv[i] % 998244353;
	for (register int i = 1; i <= n << 1; ++i) Super_Quick_Read(a[i]);
	sort(a + 1, a + (n << 1) + 1);
	long long tot_1 = 0, tot_2 = 0;
	for (register int i = 1; i <= n; ++i) (tot_1 += a[i]) %= 998244353;
	for (register int i = 1; i <= n; ++i) (tot_2 += a[i + n]) %= 998244353;
	for (register int i = 1, temp; i <= n; ++i) temp = C(n, i) % 998244353 * i % 998244353 * inv[n] % 998244353, Answer_1 = (Answer_1 + tot_1 % 998244353 * temp % 998244353 * 2 % 998244353 * C(n, n - i) % 998244353) % 998244353, Answer_2 = (Answer_2 + tot_2 % 998244353 * temp % 998244353 * 2 % 998244353 * C(n, n - i) % 998244353) % 998244353;
	Quick_Write((Answer_2 - Answer_1 + 998244353) % 998244353);
	return 0;
}
  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值