题解:AT_abc483_g [ABC483G] Qualification
如果
为了方便计算,下面仅计算期望
于是
因为
其中
到这里,我们已经保证了
之后答案就很好算了。
:::info[code]
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 2e6 + 5,p = 998244353;
ll A[N + 5],I[N + 5];
ll t,n,k,ans;
ll qpow(ll x,ll y){
ll res = 1,w = x;
while(y){
if(y & 1){
res = res * w % p;
}
w = w * w % p;
y >>= 1;
}
return res;
}
ll C(ll x,ll y){
if(x < y || y < 0) return 0;
return A[x] * I[y] % p * I[x-y] % p;
}
ll D(ll x,ll y){
if(x < y || y < 0) return 0;
return I[x] * A[y] % p * A[x-y] % p;
}
int main(){
A[0] = 1;
for(ll i = 1;i <= N;++i) A[i] = A[i-1] * i % p;
I[N] = qpow(A[N],p-2);
for(ll i = N - 1;i >= 0;--i) I[i] = I[i+1] * (i+1) % p;
scanf("%lld",&t);
while(t--){
scanf("%lld%lld",&n,&k);
ans = 0;
for(ll i = 0;i <= k;++i){
ans += (n-2*k)*qpow(n-2*k+i,p-2)%p*i%p*C(k,i)%p*C(n-k,k-i)%p*D(n,k)%p;
ans %= p;
}
ans = ans * A[n] % p;
ans = ans * A[n] % p;
ans = (ans % p + p) % p;
printf("%lld\n",ans);
}
return 0;
}
:::