Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)-D. Divide and Sum

2024-01-11 05:32

本文主要是介绍Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)-D. Divide and Sum,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接

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)=∑ni=1|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).

题意:给定一个长度为2*n的数组a,将a分成两份放在p数组和q数组里,p和q的长度相同,且里面的元素都来自a数组并且在a数组里一一对应,p非降序排列成数组x,q非升序排列为y,
我们设 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,求所有不重复排列的 f ( p , q ) f(p,q) f(p,q)的和,结果模998244353取余

思路:这题完全就是一个排序求和然后求排列组合就行了,我们可以先将a数组排序,然后去分成两个数组,假设 n = 2 n=2 n=2,同时 a 1 , a 2 , a 3 , a 3 a_1,a_2,a_3,a_3 a1,a2,a3,a3升序排列,则所有 f ( p , q ) f(p,q) f(p,q)的和可以计算出结果为 C 2 ∗ n n ∗ ( a 4 + a 3 − a 2 − a 1 ) C_{2*n}^n*(a_4+a_3-a_2-a_1) C2nn(a4+a3a2a1),算到这里就已经差不多了,排序求前半段和后半段的和然后做差,再求排列组合,相乘即可,我再做差这个地方错了两次,因为前面取模可能导致总和小于数组一半的和,所以在这个地方最好是先加上MOD,再做差,然后再取模

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll MOD=998244353;
const ll N=2e5+5;
const ll INF=0x3f3f3f;
ll V[N];
void init()
{V[1]=1;for(int i=2;i<N;i++)V[i]=V[i-1]*2%MOD;
}
ll mod_pow(ll x, ll n, ll p){ll res = 1;while(n){if(n & 1) res =res * x % p;x = x * x % p;n >>= 1;}return res;
}ll comb(ll n, ll m, ll p){if(m > n) return 0;ll ret = 1;m = min(n - m, m);for(int i = 1; i <= m; i ++){ll a = (n + i - m) % p;ll b = i % p;ret = ret * (a * mod_pow(b, p - 2, p) % p) % p;}return ret;
}ll Lucas(ll n, ll m, ll p){//从n中选m个取模;if(m == 0) return 1;return comb(n % p, m % p, p) * Lucas(n / p, m / p, p) % p;
}ll Mpow(ll a,ll b){ll r=1,base=a;while(b){if(b&1) r*=base;base*=base;b>>=1;}return r;
}void slove()
{ll n;cin>>n;n*=2;ll a[n+5];ll sum=0;for(ll i=0;i<n;i++){cin>>a[i];sum+=a[i];sum%=MOD;}sort(a,a+n);ll sum1=0;for(ll i=0;i<n/2;i++){sum1+=a[i];sum1%=MOD;}sum=sum+MOD-sum1;sum%=MOD;sum=sum+MOD-sum1;sum%=MOD;ll ans=((Lucas(n,n/2,MOD)%MOD)*sum)%MOD;cout<<ans%MOD<<endl;
}
int main()
{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);//init();//int t;cin>>t;//while(t--)slove();return 0;
}

这篇关于Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)-D. Divide and Sum的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/593314

相关文章

Go语言中的go.mod与go.sum

问题1:什么是go.mod以及它是用来解决什么问题的? go mod 是 Go 语言引入的包管理工具,用于解决 Go 语言项目在依赖管理方面的问题。 传统上,若不使用go mod,则需要要通过GOPATH来管理依赖,而这种方式存在一些问题: 如1. 包管理依赖不明确 2. 依赖库的版本管理 3. 需要手动管理同步依赖的复杂性等 而go mod可以帮助开发者在项目中明确管理依赖的版

Codeforces 158B

很久没有刷题了,刚刚有小学弟问了这道题,AC之后贴上来吧。水~~~ #include <cstdio>#include <cmath>int main() {int n;while(scanf("%d", &n) != EOF) {int a = 0, b = 0, c = 0, d = 0;int arr[100001];for (int i = 0; i < n; ++

Codeforces April Fools Day Contest 2014(附官方题解)

Codeforces2014年愚人节的坑题。。。但还是感觉挺好玩的。。。 A. The Great Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Two teams mee

Codeforces April Fools Day Contest 2013

2013年愚人节的坑题。。。 A. Mysterious strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Input The input contains a sin

▶《强化学习的数学原理》(2024春)_西湖大学赵世钰 Ch5 蒙特卡洛方法【model-based ——> model-free】

PPT 截取必要信息。 课程网站做习题。总体 MOOC 过一遍 1、视频 + 学堂在线 习题 2、 过 电子书 是否遗漏 【下载:本章 PDF GitHub 页面链接 】 【第二轮 才整理的,忘光了。。。又看了一遍视频】 3、 过 MOOC 习题 看 PDF 迷迷糊糊, 恍恍惚惚。 学堂在线 课程页面链接 中国大学MOOC 课程页面链接 B 站 视频链接 PPT和书籍下载网址: 【Gi

越复杂的CoT越有效吗?Complexity-Based Prompting for Multi-step Reasoning

Complexity-Based Prompting for Multi-step Reasoning 论文:https://openreview.net/pdf?id=yf1icZHC-l9 Github:https://github.com/FranxYao/chain-of-thought-hub 发表位置:ICLR 2023 Complexity-Based Prompting for

poj 1564 Sum It Up -- DFS 递归

题意:给一个数 t ,以及 n 个数,求 n 个数中的几个数加起来的和为 t 的情况有多少种。 注意:题目要求相同的组合方式不能出现2次,即 “3 4 1 1 1 1 ” 的结果为:“1+1+1”。 思路:一个  for  循环遍历一遍,每个 i 表示以当前数为起点开始一次DFS递归,当所遍历的和为 t 时,输出该组合方式,如果大于 t 则返回,小于则往下递归。以二维数组保存已经输出过的数据,

▶《强化学习的数学原理》(2024春)_西湖大学赵世钰 Ch4 值迭代 与 策略迭代 【动态规划 model-based】

PPT 截取必要信息。 课程网站做习题。总体 MOOC 过一遍 1、视频 + 学堂在线 习题 2、过 电子书 补充 【下载: 本章 PDF 电子书 GitHub】 [又看了一遍视频。原来第一次跳过了好多内容。。。] 3、总体 MOOC 过一遍 习题 学堂在线 课程页面链接 中国大学MOOC 课程页面链接 B 站 视频链接 PPT和书籍下载网址: 【GitHub 链接】 总述:

力扣SQL50 每月交易 I 求和 SUM(条件表达式) DATE_FORMAT(日期,指定日期格式)

Problem: 1193. 每月交易 I 👨‍🏫 参考题解 Code select DATE_FORMAT(trans_date, '%Y-%m') AS month,country,count(*) as trans_count,count(if(state = 'approved', 1, NULL)) as approved_count,sum(amount) as

力扣SQL50 项目员工 I ROUND AVG

Problem: 1075. 项目员工 I 👨‍🏫 参考题解 Code select project_id,ROUND(AVG(e.experience_years),2) as average_yearsFROMproject as pLEFT JOINemployee as eONp.employee_id = e.employee_idGROUP BYp.proje