2019HDU多校 Everything Is Generated In Equal Probability (期望+逆元)
2019HDU多校 Everything Is Generated In Equal Probability (期望+逆元)
非原创,转自:https://blog.csdn.net/jerry99s/article/details/97244050
题目: http://acm.hdu.edu.cn/showproblem.php?pid=6595
分析:
设f(n)=Calculate(Array)×Probability[Calculate(Array)],其中|Array|=n;
即f(n)=E[Calculate(Array)],其中|Array|=n;
则ans=[ ∑f(n) ] / N,n<=N;
考虑f(n)如何求:
考虑一对逆序对对答案产生的贡献!
从n个数中选则两个数:C(n,2)
较小的数排在前面的概率:1/2
在第i层子序列中对答案产生贡献的概率:(1/4)^i ,即这两个数出现在第i层子序列中的概率
举个栗子,比如这两个数出现在原数列中的概率为(1/4)^0=1,
出现在第一层子序列中的概率为(1/2×1/2)^1=1/4,其中1/2为每个数出现在子序列中的概率。
综上,f(n)=C(n,2)×1/2×∑[(1/4)^i],其中i∈[0,∞];
=n*(n-1)/3
ans=[ ∑f(n) ] / N,n<=N。
注意逆元用费马小定理处理一下即可。
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long llong; 4 const int tmax=3005; 5 const llong mod=998244353; 6 llong f[tmax],three,rev[tmax]; 7 llong quick(llong base) 8 { 9 llong ans=1,k=mod-2; 10 while(k) 11 { 12 if(k&1) ans=(ans*base)%mod; 13 base=(base*base)%mod; 14 k>>=1; 15 } 16 return ans; 17 } 18 void init() 19 { 20 int i; 21 three=quick(3ll); 22 for(i=1;i<=3000;i++) 23 { 24 f[i]=(1ll*i*(i-1))%mod*three%mod; 25 rev[i]=quick(1ll*i); 26 } 27 return; 28 } 29 int main() 30 { 31 init(); 32 //test(); 33 int N; 34 while(scanf("%d",&N)==1) 35 { 36 llong ans=0; 37 for(int i=1;i<=N;i++) 38 ans=(ans+f[i])%mod; 39 ans=ans*rev[N]%mod; 40 cout<<ans<<endl; 41 } 42 return 0; 43 }

浙公网安备 33010602011771号