CF1753C.Wish I Knew How to Sort
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a binary array a (all elements of the array are 0 or 1 ) of length n . You wish to sort this array, but unfortunately, your algorithms teacher forgot to teach you sorting algorithms. You perform the following operations until a is sorted:
- Choose two random indices i and j such that i<j . Indices are chosen equally probable among all pairs of indices (i,j) such that 1≤i<j≤n .
- If ai>aj , then swap elements ai and aj .
What is the expected number of such operations you will perform before the array becomes sorted?
It can be shown that the answer can be expressed as an irreducible fraction qp , where p and q are integers and q≡0(mod998244353) . Output the integer equal to p⋅q−1mod998244353 . In other words, output such an integer x that 0≤x<998244353 and x⋅q≡p(mod998244353) .
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t ( 1≤t≤105 ). Description of the test cases follows.
The first line of each test case contains an integer n ( 1≤n≤200000 ) — the number of elements in the binary array.
The second line of each test case contains n integers a1,a2,…,an ( ai∈{0,1} ) — elements of the array.
It's guaranteed that sum of n over all test cases does not exceed 200000 .
输出格式
For each test case print one integer — the value p⋅q−1mod998244353 .
输入输出样例
输入#1
3 3 0 1 0 5 0 0 1 1 1 6 1 1 1 0 0 1
输出#1
3 0 249561107
说明/提示
Consider the first test case. If the pair of indices (2,3) will be chosen, these elements will be swapped and array will become sorted. Otherwise, if one of pairs (1,2) or (1,3) will be selected, nothing will happen. So, the probability that the array will become sorted after one operation is 31 , the probability that the array will become sorted after two operations is 32⋅31 , the probability that the array will become sorted after three operations is 32⋅32⋅31 and so on. The expected number of operations is i=1∑∞(32)i−1⋅31⋅i=3 .
In the second test case the array is already sorted so the expected number of operations is zero.
In the third test case the expected number of operations equals to 475 so the answer is 75⋅4−1≡249561107(mod998244353) .