题目: 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。
注意逆元用费马小定理处理一下即可。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long llong;
const int tmax=3005;
const llong mod=998244353;
llong f[tmax],three,rev[tmax];
llong quick(llong base)
{
llong ans=1,k=mod-2;
while(k)
{
if(k&1) ans=(ans*base)%mod;
base=(base*base)%mod;
k>>=1;
}
return ans;
}
void init()
{
int i;
three=quick(3ll);
for(i=1;i<=3000;i++)
{
f[i]=(1ll*i*(i-1))%mod*three%mod;
rev[i]=quick(1ll*i);
}
return;
}
int main()
{
init();
//test();
int N;
while(scanf("%d",&N)==1)
{
llong ans=0;
for(int i=1;i<=N;i++)
ans=(ans+f[i])%mod;
ans=ans*rev[N]%mod;
cout<<ans<<endl;
}
return 0;
}