题意:

给定一个长度为2n的数组,将数组分成两个长度为n的数组p,q,将p从小到大排序,将q从大到小排序,对于每种分法,f(p,q)=∑i=1n\sum_{i=1}^{n}∑i=1n​|xi−yi|.求总和

题目:

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

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

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

Input

The first line contains a single integer n (1≤n≤150000).

The second line contains 2n integers a1,a2,…,a2n (1≤ai≤109) — elements of array a.

Output

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

Examples

Input

1
1 4

Output

6

Input

2
2 1 2 1

Output

12

Input

3
2 2 2 2 2 2

Output

0

Input

5
13 8 35 94 9284 34 54 69 123 846

Output

2588544

Note

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

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

p=[1], q=[4], then x=[1], y=[4], f(p,q)=|1−4|=3;
p=[4], q=[1], then x=[4], y=[1], f(p,q)=|4−1|=3.
In the second example, there are six valid partitions of the array a:

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

分析:

1.考虑将a先从小到大排序后,可以分成前后两块,其中因为p,q的长度是固定的n。
2.求和是每个对应值差的绝对值。可以看作任意两个值交换集合,因为得到差值的绝对值相同,这里顺序对结果没有影响。
3.前面对a排序,所以对于每种方案,一定是右块的数去减左块的数,其值和为ans;
4.而所有的排列总数是CmnC_{m}^{n}Cmn​(m=2*n),所以总的答案ans=ans ×Cmn\times C_{m}^{n}×Cmn​
5.由于是大数需要求余,CmnC_{m}^{n}Cmn​=m!n!(m−n)!\frac{m!}{n!(m-n)!}n!(m−n)!m!​,只需拆开来算时求n!×n!\timesn!×(m−n)!(m-n)!(m−n)!的逆元即可,又n==m-n,只需求 n!n!n!的逆元即可,即根据费马小定理和快速幂求n!mod−2n!^{mod-2}n!mod−2.

AC代码:

#include<bits/stdc++.h>
using namespace std;
#define mod 998244353
typedef long long ll;
const int M=3e5+5;
int n,m;
ll ans,num,a,b,dp[M];
ll dfs(ll x,int y){ll ans=1;while(y){if(y&1) ans=ans*x%mod;y>>=1;x=x*x%mod;}return ans;
}
int main(){cin>>n;m=n<<1;for(int i=0;i<m;i++)cin>>dp[i];sort(dp,dp+m);for(int i=0;i<n;i++)ans=(ans+dp[n+i]-dp[i])%mod;a=b=1;for(int i=2;i<=m;i++){b=b*i%mod;if(i==n)a=b;}num=dfs(a,mod-2);ans=ans*b%mod*num%mod*num%mod;cout<<ans<<endl;return 0;
}

Divide and Sum CodeForces - 1445D(排列组合+逆元)相关推荐

  1. [Codeforces 893E. Counting Arrays]排列组合

    [Codeforces 893E. Counting Arrays]排列组合 分类:combinatorics number theory math 1. 题目链接 [Codeforces 893E. ...

  2. Codeforces 991E. Bus Number (DFS+排列组合)

    解题思路 将每个数字出现的次数存在一个数组num[]中(与顺序无关). 将出现过的数字i从1到num[i]遍历.(i from 0 to 9) 得到要使用的数字次数数组a[]. 对于每一种a使用排列组 ...

  3. Codeforces Gym 100187D D. Holidays 排列组合

    D. Holidays Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/D ...

  4. [Codeforces 997C]Sky Full of Stars(排列组合+容斥原理)

    [Codeforces 997C]Sky Full of Stars(排列组合+容斥原理) 题面 用3种颜色对\(n×n\)的格子染色,问至少有一行或一列只有一种颜色的方案数.\((n≤10^6)\) ...

  5. 排列组合,字符串——Killer Names

    **题目:**Problem - 6143 http://acm.hdu.edu.cn/showproblem.php?pid=6143 Killer Names Time Limit: 2000/1 ...

  6. 组合数学-排列组合整理

    此文是我整理组合数学排列组合知识的博文,排列组合从零开始...加油! 1.重复组合: 从n种不同元素中取出m的元素(方法是从n个元素中每次取出一个后,放回,再取另外一个,直到取出m个元素),每一种元素 ...

  7. itertools库常用高效迭代器一览表,帮你快速实现数据的排列组合【python】

    itertools库常用高效迭代器一览表,帮你快速实现数据的排列组合 文档: https://docs.python.org/zh-cn/3/library/itertools.html iterto ...

  8. 论排列组合,持续更新

    今天刚好碰到了一个排列组合问题,因为之前对这方面的学习比较少,所以用的非常蠢的方法做了四位数中取三位的排列,写的程序太有局限性,源码如下 #define _CRT_SECURE_NO_WORNINGS ...

  9. 【COGS】2287:[HZOI 2015]疯狂的机器人 FFT+卡特兰数+排列组合

    [题意][COGS 2287][HZOI 2015]疯狂的机器人 [算法]FFT+卡特兰数+排列组合 [题解]先考虑一维的情况,支持+1和-1,前缀和不能为负数,就是卡特兰数的形式. 设C(n)表示第 ...

最新文章

  1. 用jquery验证用户名是否有效或重复
  2. 建立计算机系学生视图,实验六 视图的操作.doc
  3. ES 的分布式架构原理能说一下么?
  4. DLL内线程同步主线程研究(子线程代码放到主线程执行)
  5. 对不起,你那不叫努力,叫重复劳动
  6. Java VM –提防YoungGen空间
  7. mathematica 下载安装注册激活大本营
  8. 测试 —— 与开发双手互搏的艺术
  9. python爬虫可以做哪些好玩的地方_如何快速的找到好玩的旅游景点信息?Python爬虫帮你轻松解决...
  10. java 省市区三级联动_javaWeb数据库动态加载全国省市区三级联动
  11. 使用ARCGIS对shp数据添加投影坐标系
  12. 基于Android图书馆借阅系统app毕业设计
  13. 三角函数和复指数函数的转化_【导数压轴】当三角函数遇到导数02
  14. 指针数组和数组指针的使用
  15. MySQL复制表数据到新表的方法 亲测可用
  16. 【Week7 作业B】TT的旅行日记
  17. 程序猿的办公桌都长啥样?
  18. IP-guard苹果加密软件|苹果系统加密|Mac文档加密软件
  19. 【NOIP2007提高组】矩阵取数游戏
  20. 计算机网络思维导图(零基础--思维导图详细版本及知识点)

热门文章

  1. LeetCode之Two Sum II - Input array is sorted
  2. 汇编语言之寄存器(内存访问)
  3. Android之ndk之gdb调试
  4. Android插件化开发基础之Java反射机制研究
  5. 公司c语言面试题目,c语言面试最必考的十道试题,求职必看!!!
  6. 3500个常用汉字表_小学常用560个汉字笔画笔顺表,打印下来,小学六年慢慢练...
  7. java解析xml生成表格_JAVA读取XML文件并解析 以及 JAVA生成文本文件输出
  8. 有的人走着走着就散了!
  9. 一招搞定高等数学! | 今日最佳
  10. 全国二级计算机理论知识,2021年度全国计算机等级考试二级MSOffice常考知识点基础知识部分.doc...