Codeforces Round 1097 (Div. 1, Based on Zhili Cup 2026)
B
Zhily and Barknights
Codeforces
1600
原题
← AC →
翻译中…

Zhily developed a game called Barknights, and she is preparing to release a major update on March $25$. Specifically, she plans to add a Module to each Operator in the game, multiplying their power level by the Module's power.

翻译中…

After the update is released, the famous game streamer Jily will evaluate the Operators' power levels and rank them in a tier list. Whenever an earlier-released Operator is ranked higher than a later-released Operator, it will cause a wave of drama.

翻译中…

Unfortunately, Zhily accidentally knocked over a hot water kettle and broke her computer, which caused all the Modules to be randomly rearranged. Now Zhily wants to know the expected number of waves of drama that will be generated, but since she has to move on to the next problem, she has entrusted this task to you.

翻译中…

You are given two arrays $a$ and $b$ of $n$ positive integers. Let $b'$ be a permutation of array $b$ chosen uniformly at random among all $n!$ possible permutations. Define $ c_i = a_i \cdot b'_i$ for $1\le i \le n$.

翻译中…

Find the expected number of inversions$^{\text{∗}}$ of array $c$.

翻译中…

$^{\text{∗}}$An inversion in array $c$ is a pair of indices $(i, j)$ such that $1 \le i \lt j \le n$ and $c_i \gt c_j$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$) — the length of the arrays $a$ and $b$.

翻译中…

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the array $a$.

翻译中…

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 10^9$) — the array $b$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$.

翻译中…

Output

翻译中…

For each test case, output the expected number of inversions in $c$ modulo $998\,244\,353$.

翻译中…

Formally, let $M = 998\,244\,353$. It can be shown that the exact answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x \lt M$ and $x \cdot q \equiv p \pmod{M}$.

翻译中…

Note

翻译中…

In the first test case, since all elements of $b$ are $1$, any of the $5!$ permutations results in $b' = (1, 1, 1, 1, 1)$. Thus, $c = (1, 14, 5, 1, 4)$ is always constant. The inversions are $(2, 3), (2, 4), (2, 5), (3, 4),$ and $(3, 5)$. The expected number of inversions is $5 \equiv 5 \pmod{998\,244\,353}$.

翻译中…

In the second test case, there are $3! = 6$ equally likely permutations $b'$. The resulting arrays $c$ and their respective inversion counts are shown below:

翻译中…

$b'$$c = (3b'_1, 2b'_2, 5b'_3)$Inversion Count$(3, 2, 5)$$(9, 4, 25)$1$(3, 5, 2)$$(9, 10, 10)$0$(2, 3, 5)$$(6, 6, 25)$0$(2, 5, 3)$$(6, 10, 15)$0$(5, 3, 2)$$(15, 6, 10)$2$(5, 2, 3)$$(15, 4, 15)$1

翻译中…

The expected number of inversions is $\frac{1+0+0+0+2+1}{6} = \frac{4}{6} = \frac{2}{3}$.

翻译中…

Modulo $998\,244\,353$, the answer is $2 \cdot 3^{-1} \equiv 665\,496\,236 \pmod{998\,244\,353}$.

翻译中…

样例

Input #1
3
5
1 14 5 1 4
1 1 1 1 1
3
3 2 5
3 2 5
10
10 72 65 43 73 23 78 13 49 99
31 90 45 19 44 18 59 31 48 29
Output #1
5
665496236
820778710
InputCopy OutputCopy
题目信息
题号
2223B
时间限制
4 s
内存限制
512 MB
标签
二分
数据结构
动态规划
数学
排序
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。