Divide and Sum

链接 : http://codeforces.com/problemset/problem/1444/B

标签 : math *1900

思路 :

首先排序, 观察到前n个数无论是放在p序列还是q序列, 在计算时一定是别的数减去它, 即为负号, 而对于后n个数, 计算时一定是它减去别的数, 即为正号.

一共有\(C_n^{\frac{n}{2}}\)种序列.

代码 :
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int mod = 998244353;
const int N = 3e5 + 7;
int fact[N], infact[N];
int a[N];

int qmi(int a, int b, int p) {
    int res = 1 % p;
    while (b) {
        if (b & 1) res = (ll)res * a % p;
        a = (ll)a * a % p;
        b >>= 1;
    }   
    return res;
}


void init() {
    fact[0] = infact[0] = 1;   
    for (int i = 1; i < N; i++) {
        fact[i] = (ll)fact[i - 1] * i % mod;
        infact[i] = (ll)infact[i - 1] * qmi(i, mod - 2, mod) % mod;
    }   
}

ll C(int a, int b) {
    return (ll)fact[a] * infact[b] % mod * infact[a - b] % mod;
}

int main() {	
	IO;
	init();
	int n, m;
	cin >> n;
	m = n;
	n <<= 1;
	ll ans = 0;
	for (int i = 1; i <= n; ++i) cin >> a[i];
	sort (a + 1, a + 1 + n); 
	for (int i = 1; i <= m; ++i) ans = (ans - (C(n, m) * a[i]) % mod) % mod;
	for (int i = m + 1; i <= n; ++i) ans = (ans + (C(n, m) * a[i]) % mod) % mod;
	ans = (ans + mod) % mod;
	cout << ans << endl;
	return 0;
}

posted @ 2020-11-07 19:39  phr2000  阅读(89)  评论(0编辑  收藏  举报