hdu6267 Master of Random(期望)

版权声明:本文为博主原创文章,转载请说明出处。 https://blog.csdn.net/xianpingping/article/details/83210616

比赛的时候是队友推的。但是赛后想了想这种题找到方向了,就不该推那么长时间。

思路:

一看,所有情况都枚举一遍显然是不合理的 。那么我就去转化思维,想每一个点对总答案的贡献。(很多题都是这个想法。)

推导过程:

还要乘以权值。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod=998244353;
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;///计算(n-1)!
        }
        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;
}

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/83210616