CF1630E.Expected Components
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Given a cyclic array a of size n , where ai is the value of a in the i -th position, there may be repeated values. Let us define that a permutation of a is equal to another permutation of a if and only if their values are the same for each position i or we can transform them to each other by performing some cyclic rotation. Let us define for a cyclic array b its number of components as the number of connected components in a graph, where the vertices are the positions of b and we add an edge between each pair of adjacent positions of b 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 a if we select it equiprobably over the set of all the different permutations of a .
输入格式
The input consists of multiple test cases. The first line contains a single integer t ( 1≤t≤105 ) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer n ( 1≤n≤106 ) — the size of the cyclic array a .
The second line of each test case contains n integers, the i -th of them is the value ai ( 1≤ai≤n ).
It is guaranteed that the sum of n over all test cases does not exceed 106 .
It is guaranteed that the total number of different permutations of a is not divisible by M
输出格式
For each test case print a single integer — the expected value of components of a permutation of a if we select it equiprobably over the set of all the different permutations of a modulo 998244353 .
Formally, let M=998244353 . It can be shown that the answer can be expressed as an irreducible fraction qp , where p and q are integers and q≡0(modM) . Output the integer equal to p⋅q−1modM . In other words, output such an integer x that 0≤x<M and x⋅q≡p(modM) .
输入输出样例
输入#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 1 different permutation of a :
- [1,1,1,1] has 1 component.
- Therefore the expected value of components is 11=1
In the second test case there are 4 ways to permute the cyclic array a , but there is only 1 different permutation of a :
- [1,1,1,2] , [1,1,2,1] , [1,2,1,1] and [2,1,1,1] are the same permutation and have 2 components.
- Therefore the expected value of components is 12=2
In the third test case there are 6 ways to permute the cyclic array a , but there are only 2 different permutations of a :
- [1,1,2,2] , [2,1,1,2] , [2,2,1,1] and [1,2,2,1] are the same permutation and have 2 components.
- [1,2,1,2] and [2,1,2,1] are the same permutation and have 4 components.
- Therefore the expected value of components is 22+4=26=3
In the fourth test case there are 120 ways to permute the cyclic array a , but there are only 24 different permutations of a :
- Any permutation of a has 5 components.
- Therefore the expected value of components is 2424⋅5=24120=5