HDU多校--6595--Everything Is Generated In Equal Probability--推公式+期望

Everything Is Generated In Equal Probability

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 791    Accepted Submission(s): 583


 

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 N s 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

 

 

Source

2019 Multi-University Training Contest 2

 

 

Recommend

liuyiding

 

首先隨機生成一個1-N之間的n,然後隨機生成1-n的全排列,然後丟進cal函數中,在cal函數中隨機選取其子序列,累加輸出。

長度爲n的序列其逆序對最多有C\left ( n,2 \right )種逆序對數,每對出現概率爲1/2,所以其期望爲C\left ( n,2 \right )/2,在長度爲n的序列中選子序列,共有2^n種,選出長度爲m的序列有C\left ( n,m \right ),所以其概率C\left ( n,2 \right )/2^n

假設f[i]爲長度爲i的函數值,那麼f[i]=

然後把左邊的i項移到右邊,整理得求出fi,然後答案即爲

1/N * fi,累加。

參考

https://www.cnblogs.com/nlKOG/p/11243080.html

https://www.cnblogs.com/zxcoder/p/11253099.html

#include<iostream>
#define int unsigned long long
using namespace std;
const int N=4e3+28,p=998244353;
int Pow(int x,int y=p-2)//計算x的逆元
{
    int re=1;
    while(y)
    {
        if(y&1)
            re=re*x%p;
        x=x*x%p;
        y>>=1;
    }
    return re;
}
int mul[N],inv[N],two[N];
int inv4;
int pre()
{
    mul[0]=inv[1]=two[0]=1;
    for(int i=1; i<=4000; i++)
        mul[i]=mul[i-1]*i%p;//階乘預處理
    for(int i=0; i<=4000; i++)
        inv[i]=Pow(mul[i]);
    int inv2=Pow(2)%p;
    inv4=Pow(4)%p;
    for(int i=1; i<=4000; i++)
        two[i]=two[i-1]*inv2%p;//預處理2^i的逆元
}
int C(int n,int m)
{
    int re=mul[n];
    re=re*inv[m]%p;
    re=re*inv[n-m]%p;
    return re;
}
int g(int x)
{
    if(x==0)
        return 0;
    int re=x*(x+p-1)%p;
    re=re*inv4%p;
    return re;
}
int f[N];
signed main()
{
    pre();
    for(int i=2; i<=4000; i++)
    {
        f[i]=g(i);
        for(int j=0; j<i; j++)
        {
            int tmp=f[j];
            tmp=tmp*C(i,j)%p;
            tmp=tmp*two[i]%p;
            f[i]=(f[i]+tmp)%p;
        }
        f[i]=f[i]*Pow(1+p-two[i])%p;
    }
    int n;
    while(scanf("%lld",&n)!=EOF)
    {
        int ans=0;
        for(int i=2; i<=n; i++)
            ans=(ans+f[i])%p;
        ans=ans*Pow(n)%p;
        printf("%lld\n",ans);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章