CF1630E.Expected Components

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Given a cyclic array aa of size nn, where aia_i is the value of aa in the ii-th position, there may be repeated values. Let us define that a permutation of aa is equal to another permutation of aa if and only if their values are the same for each position ii or we can transform them to each other by performing some cyclic rotation. Let us define for a cyclic array bb its number of components as the number of connected components in a graph, where the vertices are the positions of bb and we add an edge between each pair of adjacent positions of bb with equal values (note that in a cyclic array the first and last position are also adjacents).

Find the expected value of components of a permutation of aa if we select it equiprobably over the set of all the different permutations of aa.

给定一个长度为 nn 的循环数组 aa,其中 aia_i 表示数组 aa 在第 ii 个位置上的值,数组中可能存在重复元素。我们定义:当且仅当两个 aa 的排列在每个位置 ii 上的值均相同,或可通过若干次循环移位相互转换时,这两个排列被视为相等。
对任意循环数组 bb,我们定义其**连通分量数(number of components)**为如下图的连通块数量:该图的顶点为 bb 的所有位置;若 bb 中两个相邻位置上的值相等,则在对应顶点间连一条边(注意:在循环数组中,首尾位置也被视为相邻)。

现从 aa 的所有互不相同的排列中等概率随机选取一个排列,求该排列作为循环数组时其连通分量数的期望值。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051 \le t \le 10^5) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1061 \le n \le 10^6) — the size of the cyclic array aa.

The second line of each test case contains nn integers, the ii-th of them is the value aia_i (1≤ai≤n1 \le a_i \le n).

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

It is guaranteed that the total number of different permutations of aa is not divisible by MM

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

每个测试用例的第一行包含一个整数 nn(1≤n≤1061 \le n \le 10^6),表示循环数组 aa 的大小。

每个测试用例的第二行包含 nn 个整数,其中第 ii 个数为 aia_i(1≤ai≤n1 \le a_i \le n)。

保证所有测试用例的 nn 之和不超过 10610^6。

保证数组 aa 的不同排列总数不能被 MM 整除。

输出格式

For each test case print a single integer — the expected value of components of a permutation of aa if we select it equiprobably over the set of all the different permutations of aa modulo 998 244 353998\,244\,353.

Formally, let M=998 244 353M = 998\,244\,353. 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(modM)q \not \equiv 0 \pmod{M}. Output the integer equal to p⋅q−1 mod Mp \cdot q^{-1} \bmod M. In other words, output such an integer xx that 0≤x<M0 \le x \lt M and x⋅q≡p(modM)x \cdot q \equiv p \pmod{M}.

对每个测试用例,输出一个整数——在所有 aa 的不同排列构成的集合上等概率随机选取一个排列时,该排列的连通块数量的期望值,结果对 998 244 353998\,244\,353 取模。

形式化地,令 M=998 244 353M = 998\,244\,353。可以证明答案可表示为既约分数 pq\frac{p}{q},其中 pp 和 qq 为整数,且 q≢0(modM)q \not \equiv 0 \pmod{M}。请输出整数 p⋅q−1 mod Mp \cdot q^{-1} \bmod M。换言之,请输出满足 0≤x<M0 \le x \lt M 且 x⋅q≡p(modM)x \cdot q \equiv p \pmod{M} 的整数 xx。

输入输出样例

  • 输入#1

    5
    4
    1 1 1 1
    4
    1 1 2 1
    4
    1 2 1 2
    5
    4 3 2 5 1
    12
    1 3 2 3 2 1 3 3 1 3 3 2

    输出#1

    1
    2
    3
    5
    358642921

说明/提示

In the first test case there is only 11 different permutation of aa:

  • [1,1,1,1][1, 1, 1, 1] has 11 component.
  • Therefore the expected value of components is 11=1\frac{1}{1} = 1

In the second test case there are 44 ways to permute the cyclic array aa, but there is only 11 different permutation of aa:

  • [1,1,1,2][1, 1, 1, 2], [1,1,2,1][1, 1, 2, 1], [1,2,1,1][1, 2, 1, 1] and [2,1,1,1][2, 1, 1, 1] are the same permutation and have 22 components.
  • Therefore the expected value of components is 21=2\frac{2}{1} = 2

In the third test case there are 66 ways to permute the cyclic array aa, but there are only 22 different permutations of aa:

  • [1,1,2,2][1, 1, 2, 2], [2,1,1,2][2, 1, 1, 2], [2,2,1,1][2, 2, 1, 1] and [1,2,2,1][1, 2, 2, 1] are the same permutation and have 22 components.
  • [1,2,1,2][1, 2, 1, 2] and [2,1,2,1][2, 1, 2, 1] are the same permutation and have 44 components.
  • Therefore the expected value of components is 2+42=62=3\frac{2+4}{2} = \frac{6}{2} = 3

In the fourth test case there are 120120 ways to permute the cyclic array aa, but there are only 2424 different permutations of aa:

  • Any permutation of aa has 55 components.
  • Therefore the expected value of components is 24⋅524=12024=5\frac{24\cdot 5}{24} = \frac{120}{24} = 5

在第一个测试用例中,数组 aa 只有 11 种不同的排列:

  • [1,1,1,1][1, 1, 1, 1] 有 11 个连通分量。
  • 因此连通分量数的期望值为 11=1\frac{1}{1} = 1。

在第二个测试用例中,循环数组 aa 有 44 种排列方式,但其中只有 11 种不同的排列:

  • [1,1,1,2][1, 1, 1, 2]、[1,1,2,1][1, 1, 2, 1]、[1,2,1,1][1, 2, 1, 1] 和 [2,1,1,1][2, 1, 1, 1] 是同一种排列,且具有 22 个连通分量。
  • 因此连通分量数的期望值为 21=2\frac{2}{1} = 2。

在第三个测试用例中,循环数组 aa 有 66 种排列方式,但其中只有 22 种不同的排列:

  • [1,1,2,2][1, 1, 2, 2]、[2,1,1,2][2, 1, 1, 2]、[2,2,1,1][2, 2, 1, 1] 和 [1,2,2,1][1, 2, 2, 1] 是同一种排列,且具有 22 个连通分量。
  • [1,2,1,2][1, 2, 1, 2] 和 [2,1,2,1][2, 1, 2, 1] 是同一种排列,且具有 44 个连通分量。
  • 因此连通分量数的期望值为 2+42=62=3\frac{2+4}{2} = \frac{6}{2} = 3。

在第四个测试用例中,循环数组 aa 有 120120 种排列方式,但其中只有 2424 种不同的排列:

  • aa 的任意一种排列均有 55 个连通分量。
  • 因此连通分量数的期望值为 24⋅524=12024=5\frac{24\cdot 5}{24} = \frac{120}{24} = 5。

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

首页