F. Cards
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Consider the following experiment. You have a deck of mm cards, and exactly one card is a joker. nn times, you do the following: shuffle the deck, take the top card of the deck, look at it and return it into the deck.
Let xx be the number of times you have taken the joker out of the deck during this experiment. Assuming that every time you shuffle the deck, all m!m! possible permutations of cards are equiprobable, what is the expected value of xkxk? Print the answer modulo 998244353998244353.
Input
The only line contains three integers nn, mm and kk (1≤n,m<9982443531≤n,m<998244353, 1≤k≤50001≤k≤5000).
Output
Print one integer — the expected value of xkxk, taken modulo 998244353998244353 (the answer can always be represented as an irreducible fraction abab, where bmod998244353≠0bmod998244353≠0; you have to print a⋅b−1mod998244353a⋅b−1mod998244353).
Examples
input
Copy
1 1 1
output
Copy
1
input
Copy
1 1 5000
output
Copy
1
input
Copy
2 2 2
output
Copy
499122178
input
Copy
998244352 1337 5000
output
Copy
326459680
题意:求二项分布k次方的期望。
分析:
然后O(k^2)处理出S(i,j),O(k)处理出,累加一下答案就可以了。
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
long long s[5004][5004];
long long p[5004];
long long qk(long long a,long long n){
long long res = 1;
while(n){
if(n&1)res = res * a % mod;
n>>=1;
a=a*a%mod;
}
return res;
}
int main(){
int n,m,k;
cin>>n>>m>>k;
s[0][0]=1;
for (int i = 1; i <= k; ++i) {
for (int j = 1; j <= i; ++j) {
(s[i][j] = s[i-1][j-1] + 1LL*j*s[i-1][j]%mod % mod)%=mod;
}
}
p[0]=1;
for (int i = 1; i <= k; ++i) {
p[i] = p[i-1]*(n-i+1)%mod;
}
long long res = 0;
for (int i = 1; i <= k; ++i) {
(res += s[k][i]*p[i]%mod*qk(qk(m,mod-2),i))%=mod;
}
cout<<res<<endl;
}