中石油备战省赛2 Master of Random( 思维加模拟 用到逆)

emmmmmmmmmmmmmmmmmm题意不好懂。。。。。。。。。。。。
感觉坑点 主要是理解 和取模。。。(一直取模)。
注意是用每个点的值乘以这个点的出现的次数 然后把每个点都加起来 再除以总情况(n-1 的阶乘)。记得一直取模就可以了。
题目链接
题目描述

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 identifi es 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?

输入

The first line contains an integer T (1≤T≤10) representing the number of test cases.
For each test case, the fi rst 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.

输出

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.

样例输入
复制样例数据 2
2
1 1
3
1 2 3

样例输出
499122178
166374063

提示

The shape of the tree in the fi rst 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 32-1 mod 998244353 = 400122178.
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.

大概意思。
特效药

hakase给nano带来了一个问题。有一个根目录树,节点上有值。对于每个查询,都要求您计算子树中的值之和。然而,纳诺是一个新手,所以她决定猜测答案。她知道数据生成器是如何工作的:它用标签0到N-1标识节点,然后逐个访问它们。对于每个i(1≤i≤n),生成器选择标签小于i的节点作为其父节点。伪代码如下:
对于i=1至n-1:
父[i]=随机(0,i-1);
其中,random(a,b)随机生成范围[a,b]内均匀分布的随机整数。
Nano知道n和i-th节点a i的值,决定随机选择一个子树,并将子树中的所有值相加作为答案。现在,哈卡斯想知道对答案的期望是什么。你能帮她吗?

内幕

第一行包含一个整数t(1≤t≤10),表示测试用例的数量。
对于每个测试用例,第一行包含一个整数n(1≤n≤100000),即根树中的节点数。
第二行包含n个整数a0,a1,…,an-1(1≤ai≤100000)表示节点的值。

特技

可以证明,答案等于不可约分数p/q。对于每个测试用例,在一行中打印p*q-1 mod 998244353。q-1是模块号998244353下q的倒数。

附带条件
复制样品数据2

1 1

1 2 2

附带条件
四亿九千九百一十二万二千一百七十八
一亿六千六百三十七万四千零六十三

特技

第一个测试用例中的树的形状是唯一的。节点1的父节点为0。可以选择具有相同可能性的节点0或1。以0为根的子树之和为2,以1为根的子树之和为1。所以期望值是(2+1)/2=3/2。输出为32-1 mod 998244353=400122178。
在第二个测试用例中有两个可能的形状,节点1的父目标是0,但节点2的父目标可能是节点0或节点1。这两种情况都是可能的。
如果节点2的父节点是节点0,我们将随机选择一个节点。以节点0为根的子树的和为6。以节点1为根的子树的和为2。以节点2为根的子树的和为3。
如果节点2的父节点是节点1,我们随机选择一个节点。以节点0为根的子树的和为6。以节点1为根的子树的和为5。以节点2为根的子树的和为3。
所以期望值是(6+2+3+6+5+3)/6=25/6。输出为25
6-1 mod 998244353=166374063。

#include<stdio.h>
typedef long long int ll;
const ll mod=998244353;
ll quickpow(ll a,ll x)
{
    ll s=1;
    while(x!=1)
    {
        if(x%2==0)
        {
            x=x/2;
            a=a*a;
            a=a%mod;
        }
        else
        {
            x=x-1;
            s=s*a;
            s=s%mod;
        }
    }
    s=s*a;
    s=s%mod;
    return s;
}
ll niyuan(ll a)
{
    ll w=quickpow(a,mod-2);
    w=w%mod;
    return w;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        ll n;
        ll tmpn=1;
        scanf("%lld",&n);
        for(int i=1;i<n;i++)
        {
            tmpn=(tmpn*i)%mod;
        }
        ll tmp=tmpn;
        ll a;
        scanf("%lld",&a);
        ll ans=1;
        ans=tmpn*a%mod;
        for(ll i=1;i<n;i++)
        {
            scanf("%lld",&a);
            tmp=tmp+tmpn*niyuan(i)%mod;
            ans=(ans+a*tmp)%mod;
        }
        tmpn=(tmpn*n)&mod;
        ans=ans*niyuan(tmpn);
        ans=ans%mod;
        printf("%lld\n",ans);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值