杭电第5场 A-Tetrahedron(数学期望,线性逆元)

题目传送门

题意: 给你一个直角四面体,这个直角四面体的三条棱的长度是[1,n]中的随机整数,我们设这个直角四面体的底面上的高为h,求 1/h² 的数学期望是多少。

思路: 经过一系列推算,我们得到 1/h²=1/a² + 1/b² + 1/c² 。因为a,b,c都是[1,n]中等概率生成的随机数,所以我们只要算1/a²的期望,最后乘3就行。1/a² 的期望=(1/1² * 1/n + 1/2² * 1/n + 1/3² * 1/n + …+ 1/n² * 1/n) ,可以把1/n提出来,所以就是前n个数的平方的倒数之和。预处理平当倒数前缀和。这里有个优化,就是在使用逆元的时候,不用每次都logn的时间去求,可以直接线性求出逆元,直接用就行。

代码:

#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
//#define int long long
#define pii pair<int,int>
#define pdd pair<double,double>
#define ull unsigned long long
#define unmap unordered_map
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read(){
    
    int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){
    
    if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){
    
    x=x*10+ch-'0';ch=gc();}
return x*f;}
using namespace std;
const int N=6e6+5;
const int inf=0x3f3f3f3f;
const int mod=998244353;
const double eps=1e-6;
const double PI=acos(-1);
ll j[N],inv[N];
ll qpow(ll a,ll b)
{
    
    
    ll res=1;
    while(b)
    {
    
    
        if(b&1)
            res=res*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return res;
}
void init()
{
    
    
    j[1]=1;
    inv[1]=1;
    for(int i=2;i<N;++i)
        inv[i]=(ll)(mod-mod/i)*inv[mod%i]%mod;
    for(int i=2;i<N;++i)
        j[i]=(j[i-1]+inv[i]*inv[i]%mod)%mod;
}
signed main()
{
    
    
    int T;scanf("%d",&T);
    init();
    while(T--)
    {
    
    
        int n;scanf("%d",&n);
        ll res=(j[n]*inv[n]*3%mod);
        printf("%lld\n",res);
    }
}

猜你喜欢

转载自blog.csdn.net/Joker_He/article/details/107824961
今日推荐