hdu 6267 Master of Random

好菜啊…ccpc的签到题都做的这么吃力..T^T
大概是一个..数学题把..
递推计算每个节点在最终情况可能为最终结果做贡献的可能种类,等于前面的节点每个做贡献的可能之和(自己作为子节点贴上去,就可以蹭到人家的了)+自己被选为子树根节点(阶乘,容易想到),然后此前的每一个节点做贡献可能,都要乘以当前的i(因为总数量会乘以i),这个就之后过一遍阶乘就好..
最后的分母一定是
n!=((n-1)!*n) 这个是容易的

#include <iostream>
using namespace std;
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
typedef long long LL;
const int MAXN = 1e5+17;
const int MOD = 998244353;
LL qm(LL a,LL b)
{
    LL res = 1;
    while(b)
    {
        if(b&1) res = res*a%MOD;
        b>>=1;
        a = a*a%MOD;
    }
    return res;
}
LL inv(LL a)
{
    return qm(a,MOD-2);
}
LL a[MAXN],val[MAXN];
int main(int argc ,char const *argv[])
{
    #ifdef noob
    freopen("Input.txt","r",stdin);freopen("Output.txt","w",stdout);
    #endif      
    int t;
    cin>>t;
    while(t--)
    {
        a[0] = 1;
        a[1] = 2;
        LL jc = 2,sum=3,n;
        cin>>n;
        for (int i = 0; i < n; ++i)
            scanf("%lld",val+i);
        for (int i = 2; i < n; ++i)
        {
            a[i] = (sum+jc)%MOD;
            sum = ((sum*i)%MOD+a[i])%MOD;
            jc = (jc*(i+1))%MOD;
        }
        jc = 1;
        for (int i = n-1; i > 1; --i)
        {
            a[i] = (a[i]*jc)%MOD;
            jc=(i*jc)%MOD;
        }
        a[0] = (jc*a[0])%MOD;
        a[1] = (jc*a[1])%MOD;
        LL up = 0,down=(jc*n)%MOD;
        for (int i = 0; i < n; ++i)
            up = (up+val[i]*a[i]%MOD)%MOD;
        cout<<up*inv(down)%MOD<<endl;
    }
    return 0;   
}

猜你喜欢

转载自blog.csdn.net/m0_37802215/article/details/80015460
今日推荐