Everything Is Generated In Equal Probability(HDU-6595)

Problem Description

One day, Y_UME got an integer N and an interesting program which is shown below:
 



Y_UME wants to play with this program. Firstly, he randomly generates an integer n∈[1,N] in equal probability. And then he randomly generates a permutation of length n in equal probability. Afterwards, he runs the interesting program(function calculate()) with this permutation as a parameter and then gets a returning value. Please output the expectation of this value modulo 998244353.

A permutation of length n is an array of length n consisting of integers only ∈[1,n] which are pairwise different.

An inversion pair in a permutation p is a pair of indices (i,j) such that i>j and pi<pj. For example, a permutation [4,1,3,2] contains 4 inversions: (2,1),(3,1),(4,1),(4,3).

In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. Note that empty subsequence is also a subsequence of original sequence.

Refer to https://en.wikipedia.org/wiki/Subsequence for better understanding.

Input

There are multiple test cases.
Each case starts with a line containing one integer N(1≤N≤3000).
It is guaranteed that the sum of Ns in all test cases is no larger than 5×104.

Output

For each test case, output one line containing an integer denoting the answer.

Sample Input

1
2
3

Sample Output

0
332748118
554580197

题意:

对于给出的程序,分为三种,依次为:

  • SUB(Array):随机返回一个 Array 的子序列
  • CNT(Array):返回 Array 的逆序对数
  • CAL(Array):若 Array 为空,返回 0,否则 CNT(Array)+CAL(SUB(Array))

现在有多组样例,每组给出一个整数 n∈[1,N] ,再随机生成一个长度为 n 序列,求这个排列的 CAL 函数的期望值

思路:

对于给定长度 n,设其随机生成序列为 Array

那么考虑长度为 n 的序列的 CNT(Array) 的期望值:

一共有 C_n^2 对数,每一对数为逆序数的概率均为 1/2,那么累加起来就是:ans=\frac{C_n^2}{2}=\frac{n!}{2(2!)(n-2)!}=\frac{n(n-1)}{4}

再考虑长度为 n 的序列的 CAL(Array) 的期望值:

设该期望值对应状态为 dp[n],那么有:dp[n]=ans+\frac{\sum_{i=0}^nC_n^idp[i]}{\sum_{i=0}^nC_n^i}

即:dp[n]=\frac{n(n-1)}{4}+\frac{\sum_{i=0}^{n-1}C_n^idp[i]}{2^n}+\frac{dp[n]}{2^n}

因此状态转移方程为:dp[n]=\frac{2^{n-2}n(n-1)+\sum_{i=0}^{n-1}C_n^idp[i]}{2^n-1}

于是,长度 n∈[1,N] 的 CAL(Array) 的期望值为:res=\frac{\sum_{n=1}^Ndp[n]}{N}

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<LL,LL>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 998244353;
const int N = 3000+5;
const int dx[] = {-1,1,0,0,1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

LL C[N][N];
LL sumBinary[N];
LL dp[N];
LL sum[N];
void init(){
    //组合数打表
    for(int i=1;i<=3000;i++)
        C[i][0]=C[i][i]=1;
    for(int i=2;i<=3000;i++)
        for(int j=1;j<i;j++)
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%MOD;

    //2^i打表
    sumBinary[0]=1;
    for(int i=1;i<=3000;i++)
        sumBinary[i]=(sumBinary[i-1]*2)%MOD;

    //dp[i]打表
    dp[0]=0,dp[1]=0;
    for(int i=2;i<=3000;i++){
        dp[i]=sumBinary[i-2]*i*(i-1)%MOD;
        for(int j=2;j<i;j++)
            dp[i]=(dp[i]+C[i][j]*dp[j]%MOD)%MOD;
        dp[i]=dp[i]*getInv(sumBinary[i]-1,MOD)%MOD;
    }
    
    //前缀和
    sum[0]=0;
    for(int i=1;i<=3000;i++)
        sum[i]=( sum[i-1]%MOD + dp[i]%MOD )%MOD;
}
int main(){
    init();
    LL n;
    while(scanf("%lld",&n)!=EOF){
        LL inv=getInv(n,MOD);
        LL res=(sum[n]%MOD * inv%MOD )%MOD;
        printf("%lld\n",res);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值