ABC326E 题解

· · 题解

题面传送门

解决思路

套路地,我们讨论每个数字对于最终答案的贡献。

以四个数 1 2 3 4 为例:

首先是他们第一次就被选到的概率,显然都为 \frac14。

1 2 3 4
第 1 次 \frac14 \frac14 \frac14 \frac14

然后考虑第二次的贡献:

1 2 3 4
第 1 次 \frac14 \frac14 \frac14 \frac14
第 2 次 0 \frac{1}{4^2} \frac{2}{4^2} \frac{3}{4^2}

同理考虑第三次的贡献:

第四次概率也同理。

所以我们得到最终表格:

1 2 3 4
第 1 次 \frac14 \frac14 \frac14 \frac14
第 2 次 0 \frac{1}{4^2} \frac{2}{4^2} \frac{3}{4^2}
第 3 次 0 0 \frac{1}{4^3} \frac{3}{4^3}
第 4 次 0 0 0 \frac{1}{4^4}

发现了什么?第 j 次贡献的分母是 n^j,而对于每个数 i 来说,其分子依次为杨辉三角的第 i 横排。所以最终所求答案即为:

\sum _ {i = 1} ^ n \sum _ {j = 1} ^ i \frac{C_{i-1}^{j-1}}{n^j}\times a_i

我们设第 i 个数的贡献为 f_i,然后根据杨辉三角的优秀性质,就可以得到递推式:

f_i=f_{i-1}+f_{i-1}\times\frac1n

然后问题就解决了。

AC Code

//If, one day, I finally manage to make my dreams a reality...
//I wonder, will you still be there by my side?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
#define TIE cin.tie(0),cout.tie(0)
#define int long long
#define y1 cyy
#define fi first
#define se second
#define cnt1(x) __builtin_popcount(x)
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define lbt(x) (x&(-x))
#define mod 998244353
using namespace std;
int n,a[300005],p,ans,f[300005];
int ksm(int x,int b){
    int ans=1;
    while(b){
        if(b&1) ans=ans*x%mod;
        x=x*x%mod,b>>=1;
    }
    return ans;
}
signed main(){
    IOS;TIE;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    int INV=ksm(n,mod-2);
    f[1]=INV;
    ans+=f[1]*a[1]%mod;
    for(int i=2;i<=n;i++){
        f[i]=f[i-1]+f[i-1]*INV%mod,f[i]%=mod;
        ans+=a[i]*f[i]%mod;
        ans%=mod;
    }
    cout<<ans<<endl;
    return 0;
}