#x1011. cf1630E Expected Components
cf1630E Expected Components
Expected Components
题面翻译
一个长度为 的环状序列 ,其中的数值满足 ,序列中可能有相等的数。
序列 的一个排列和另外一个排列本质相同,当且仅当可以通过旋转使它们变得每一项都对应相等。
对于 的任何一种本质不同排列,可以构造一张 个点的无向图,节点 对应序列中的 。若序列中相邻两项相等(首项和末项也相邻),则在它们的对应的节点之间连一条边。这个排列的价值为无向图的连通块数量。
给定序列 ,从它的所有本质不同排列中等概率选择一个,求价值的期望。
题目描述
Given a cyclic array of size , where is the value of in the -th position, there may be repeated values. Let us define that a permutation of is equal to another permutation of if and only if their values are the same for each position or we can transform them to each other by performing some cyclic rotation. Let us define for a cyclic array its number of components as the number of connected components in a graph, where the vertices are the positions of and we add an edge between each pair of adjacent positions of 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 if we select it equiprobably over the set of all the different permutations of .
输入格式
The input consists of multiple test cases. The first line contains a single integer ( ) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer ( ) — the size of the cyclic array .
The second line of each test case contains integers, the -th of them is the value ( ).
It is guaranteed that the sum of over all test cases does not exceed .
It is guaranteed that the total number of different permutations of is not divisible by
输出格式
For each test case print a single integer — the expected value of components of a permutation of if we select it equiprobably over the set of all the different permutations of modulo .
Formally, let . It can be shown that the answer can be expressed as an irreducible fraction , where and are integers and . Output the integer equal to . In other words, output such an integer that and .
样例 #1
样例输入 #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 different permutation of :
- has component.
- Therefore the expected value of components is
In the second test case there are ways to permute the cyclic array , but there is only different permutation of :
- , , and are the same permutation and have components.
- Therefore the expected value of components is
In the third test case there are ways to permute the cyclic array , but there are only different permutations of :
- , , and are the same permutation and have components.
- and are the same permutation and have components.
- Therefore the expected value of components is
In the fourth test case there are ways to permute the cyclic array , but there are only different permutations of :
- Any permutation of has components.
- Therefore the expected value of components is