2017 China Collegiate Programming Contest
Hangzhou Onsite @ ZSTU, November 5, 2017
Problem D. Master of Random
Hakase provides Nano with a problem. There is a rooted tree with values on nodes. For each query,
you are asked to calculate the sum of the values in the subtree. However, Nano is a rookie so she
decides to guess the answer. She has known how the data generator works: it identifies the nodes
with labels from 0 to n − 1 and then visits them one by one. For each i (1 ≤ i ≤ n), the generator
selects a node whose label is smaller than i to be its father. The pseudocode is like this:
for i = 1 to n - 1:
father[i] = random(0, i - 1);
where random(a, b) randomly generates a uniformly distributed random integer in range [a, b].
Knowing n and the value of the i-th node ai
, Nano decides to randomly choose a subtree and sum
up all of the values in the subtree as the answer. Now Hakase wants to know what the expectation
of the answer is. Can you help her?
Input
The first line contains an integer T (1 ≤ T ≤ 10) representing the number of test cases.
For each test case, the first line contains an integer n (1 ≤ n ≤ 100000), the number of the nodes in
the rooted tree.
The second line contains n integers a0, a1, …, an−1 (1 ≤ ai ≤ 100000) represent the values of nodes.
Output
It can be proven that the answer equals to an irreducible fraction p/q. For each test case, print
p ∗ q
−1 mod 998244353 in one line. q
−1
is the inverse of q under module number 998244353.
Example
standard input standard output
2
2
1 1
3
1 2 3
499122178
166374063
Explanation
The shape of the tree in the first test case is unique. The father of node 1 is 0. It is possible to
choose node 0 or 1 with equal possibility. The sum of the subtree with 0 as the root is 2 while the
sum of the subtree with 1 as the root is 1. So the expectation is (2 + 1)/2 = 3/2. The output is
3 ∗ 2
−1 mod 998244353 = 400122178.
Page 4 of 17
2017 China Collegiate Programming Contest
Hangzhou Onsite @ ZSTU, November 5, 2017
There are two possible shapes in the second test case, node 1’s father destines to be 0, but node 2’s
father might be node 0 or node 1. Both conditions are equally possible.
If node 2’s father is node 0, we randomly choose a node. The sum of the subtree with node 0 as the
root is 6. The sum of the subtree with node 1 as the root is 2. The sum of the subtree with node 2
as the root is 3.
If node 2’s father is node 1, we randomly choose a node. The sum of the subtree with node 0 as the
root is 6. The sum of the subtree with node 1 as the root is 5. The sum of the subtree with node 2
as the root is 3.
So the expectation is (6 + 2 + 3 + 6 + 5 + 3)/6 = 25/6. The output is 25 ∗ 6
−1 mod 998244353 =
166374063.
题目意思倒是看了蛮久才看懂,大致是:比如给你4个点分别是 1 2 3 4 ,那么除了第一个点,其他所有的点都可以让他前面的点做自己的父节点,例如 4 的父节点可以是3 也可以是2 也可以是1 ,最后问以每一个节点为根节点的子树的值的和/不同子树的个数总和(应该就相当于求每一个点的平均期望值吧。。。不管他也没事,反正能做就行。)
自己可以画一下4个点的时候,每一个点出现的次数为多少,可以发现:
1 出现了6 次,2 出现了12 次,3出现了15 次,4出现了17次。
于是你又会发现,6 = 3!,12=3!+3!/1,15=3!+3!/1+3!/2,17=3!+3!/1+3!/2+3!/3
所以啊 可以推出,n 个节点的时候,第0 号节点出现的次数为(n-1)!,第1号节点出现的次数为(n-1)!/1,。。。以此类推。
又会发现,4个节点的子树共有24种即4! 也就是n!
那递推不就出来了嘛?
数比较大,快速幂解决,乘法逆元当模数为质数的时候,直接用 a^(mod-2) 就好
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<ctime>
#define Mod 998244353
typedef long long LL;
using namespace std;
LL quick(LL a,LL b)
{
LL ans=1;
while(b)
{
if(b&1) ans = (ans*a)%Mod;
b>>=1;
a=(a*a)%Mod;
}
return ans;
}
LL inv(LL x)
{
return quick(x,Mod-2);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
LL tp=1;
for(int i=1; i<n; i++)
{
tp=(tp*i)%Mod;
}
LL a,tmp=tp;
scanf("%lld",&a);
LL ans=tp*a%Mod;
for(int i=1; i<n; i++)
{
scanf("%lld",&a);
tmp=tmp+tp*inv(i)%Mod;
ans=(ans+tmp*a)%Mod;
}
tp=tp*n%Mod;
printf("%lld\n",ans*inv(tp)%Mod);
}
return 0;
}