hdu 6267 Master of Random 公式

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.

这题就是个公式推导 平时看题解看多了 这道题的复杂度超过了平时的训练的思考程度导致现场赛没想出来 谨记勤思多练

#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <bits/stdc++.h>
typedef long long LL;
const int N =1e6+100;
using namespace std;
typedef long long LL;
const LL mod =  998244353;
LL a[N];
LL quick1(LL x,LL n)
{
    LL r=1;
    while(n)
    {
        if(n&1)r=r*x%mod;
        n>>=1;
        x=x*x%mod;
    }
    return r%mod;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        LL n;
        scanf("%lld", &n);
        for(int i=0; i<n; i++) scanf("%lld", &a[i]);
        LL sum=a[0], cnt=1, num=1, h=1;
        for(LL i=1; i<n; i++)
        {
            sum=(sum*(i)%mod)%mod;
            num=num*i%mod;
            sum=(sum+((cnt+h)%mod)*a[i])%mod;
            cnt=(cnt*i%mod+cnt+h)%mod;
            h=(h*(i+1))%mod;
        }
        //cout<<quick1(num,mod-2)%mod<<endl;
        printf("%lld\n",sum*quick1(num*n%mod,mod-2)%mod);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值