1 条题解

  • 0
    @ 2026-7-3 15:57:32

    Prufer 序列:nn 个结点的无根树与长度为 max⁡{n−2,0}\max\{n - 2, 0\} 且每一项都在 {1,2,…,n}\{1, 2, \ldots, n\} 中取值的序列一一对应,且序列中 ii 的出现次数恰好比树中结点 ii 的度数少 11。

    一棵 nn 个结点的树有 n−1n - 1 条边,总度数为 2n−22n - 2,每一项的平均值为:

    2n−2n\dfrac{2n - 2}{n}

    假设 n≥3n \ge 3,对于序列中所有项的出现次数加上 11 后的平方和的平均值,可以认为一个数第 ii 次出现时贡献了 2i+1n\dfrac{2i + 1}{n} 的平均值,空序列对应的平均值为 11,第一项的贡献总是 3n\dfrac{3}{n},每出现一项就对下一项的期望贡献带来 2n2\dfrac{2}{n^2} 的增益,故这一平均值的期望为:

    (n−1)(5n−6)n2\dfrac{(n - 1)(5n - 6)}{n^2}

    一个序列的方差等于序列中每一项的平方的平均值减去每一项的平均值的平方的差,代入上面两个结果,按题目要求乘上 n2n^2,最终的答案为:

    (n−1)(n−2)(n - 1)(n - 2)
    #include <bits/stdc++.h>
    using namespace std;
    inline __int128 read()
    {
    	__int128 x = 0;
    	char c = getchar();
    	while (c < '0') c = getchar();
    	while (c >= '0')
    	{
    		x = x * 10 + c - '0';
    		c = getchar();
    	}
    	return x;
    }
    void write(__int128 x)
    {
    	if (x <= 9)
    	{
    		putchar(x + '0');
    		return;
    	}
    	write(x / 10);
    	putchar(x % 10 + '0');
    }
    inline void writeline(__int128 x)
    {
    	write(x);
    	putchar('\n');
    }
    int main()
    {	int T = read();
    	while (T--)
    	{
    		__int128 n = read();
    		writeline((n - 1) * (n - 2));
    	}
    	return 0;
    }
    

    信息

    ID
    277
    时间
    1000ms
    内存
    512MiB
    难度
    8
    标签
    递交数
    2
    已通过
    2
    上传者