CF1753C.Wish I Knew How to Sort

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a binary array aa (all elements of the array are 00 or 11) of length nn. You wish to sort this array, but unfortunately, your algorithms teacher forgot to teach you sorting algorithms. You perform the following operations until aa is sorted:

  1. Choose two random indices ii and jj such that i<ji \lt j. Indices are chosen equally probable among all pairs of indices (i,j)(i, j) such that 1≤i<j≤n1 \le i \lt j \le n.
  2. If ai>aja_i \gt a_j, then swap elements aia_i and aja_j.

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 pq\frac{p}{q}, where pp and qq are integers and q≢0(mod998 244 353)q \not \equiv 0 \pmod{998\,244\,353}. Output the integer equal to p⋅q−1 mod 998 244 353p \cdot q^{-1} \bmod 998\,244\,353. In other words, output such an integer xx that 0≤x<998 244 3530 \le x \lt 998\,244\,353 and x⋅q≡p(mod998 244 353)x \cdot q \equiv p \pmod{998\,244\,353}.

给你一个长度为 nn 的二进制数组 aa(数组中所有元素均为 00 或 11)。你想将该数组排序,但不幸的是,你的算法老师忘记教你排序算法了。你将持续执行以下操作,直到数组 aa 被排好序:

  1. 随机选择两个下标 ii 和 jj,满足 i<ji \lt j。所有满足 1≤i<j≤n1 \le i \lt j \le n 的下标对 (i,j)(i, j) 被选中的概率相等。
  2. 若 ai>aja_i \gt a_j,则交换元素 aia_i 和 aja_j。

在数组变为有序之前,你所执行的此类操作的期望次数是多少?

可以证明,该答案可表示为最简分数 pq\frac{p}{q},其中 pp 和 qq 为整数,且 q≢0(mod998 244 353)q \not \equiv 0 \pmod{998\,244\,353}。请输出整数 p⋅q−1 mod 998 244 353p \cdot q^{-1} \bmod 998\,244\,353。换言之,请输出满足 0≤x<998 244 3530 \le x \lt 998\,244\,353 且 x⋅q≡p(mod998 244 353)x \cdot q \equiv p \pmod{998\,244\,353} 的整数 xx。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1051 \le t \le 10^5). Description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤200 0001 \le n \le 200\,000) — the number of elements in the binary array.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (ai∈0,1a_i \in {0, 1}) — elements of the array.

It's guaranteed that sum of nn over all test cases does not exceed 200 000200\,000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1051 \le t \le 10^5)。随后是测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤200 0001 \le n \le 200\,000)—— 表示二进制数组中元素的个数。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(其中 ai∈{0,1}a_i \in \{0, 1\})—— 表示数组的元素。

保证所有测试用例的 nn 之和不超过 200 000200\,000。

输出格式

For each test case print one integer — the value p⋅q−1 mod 998 244 353p \cdot q^{-1} \bmod 998\,244\,353.

对于每个测试用例,输出一个整数——即 p⋅q−1 mod 998 244 353p \cdot q^{-1} \bmod 998\,244\,353 的值。

输入输出样例

  • 输入#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)(2, 3) will be chosen, these elements will be swapped and array will become sorted. Otherwise, if one of pairs (1,2)(1, 2) or (1,3)(1, 3) will be selected, nothing will happen. So, the probability that the array will become sorted after one operation is 13\frac{1}{3}, the probability that the array will become sorted after two operations is 23⋅13\frac{2}{3} \cdot \frac{1}{3}, the probability that the array will become sorted after three operations is 23⋅23⋅13\frac{2}{3} \cdot \frac{2}{3} \cdot \frac{1}{3} and so on. The expected number of operations is ∑i=1∞(23)i−1⋅13⋅i=3\sum \limits_{i=1}^{\infty} \left(\frac{2}{3} \right)^{i - 1} \cdot \frac{1}{3} \cdot 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 754\frac{75}{4} so the answer is 75⋅4−1≡249 561 107(mod998 244 353)75 \cdot 4^{-1} \equiv 249\,561\,107 \pmod {998\,244\,353}.

考虑第一个测试用例。若选择下标对 (2,3)(2, 3),则这两个元素将被交换,数组将变为有序;否则,若选择下标对 (1,2)(1, 2) 或 (1,3)(1, 3) 中的任意一个,则数组不发生任何变化。因此,一次操作后数组变为有序的概率为 13\frac{1}{3},两次操作后变为有序的概率为 23⋅13\frac{2}{3} \cdot \frac{1}{3},三次操作后变为有序的概率为 23⋅23⋅13\frac{2}{3} \cdot \frac{2}{3} \cdot \frac{1}{3},依此类推。期望操作次数为 ∑i=1∞(23)i−1⋅13⋅i=3\sum \limits_{i=1}^{\infty} \left(\frac{2}{3} \right)^{i - 1} \cdot \frac{1}{3} \cdot i = 3。

在第二个测试用例中,数组初始即为有序,因此期望操作次数为 00。

在第三个测试用例中,期望操作次数等于 754\frac{75}{4},故答案为 75⋅4−1≡249 561 107(mod998 244 353)75 \cdot 4^{-1} \equiv 249\,561\,107 \pmod {998\,244\,353}。

输入解题思路,AI测评打分。不知道怎么写?

首页