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
3Sample 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) 的期望值:
一共有 对数,每一对数为逆序数的概率均为 1/2,那么累加起来就是:
再考虑长度为 n 的序列的 CAL(Array) 的期望值:
设该期望值对应状态为 dp[n],那么有:
即:
因此状态转移方程为:
于是,长度 n∈[1,N] 的 CAL(Array) 的期望值为:
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;
}