牛客挑戰賽 38 D.突擊檢查(期望 + 思維)

在這裏插入圖片描述


官方題解:
在這裏插入圖片描述


代碼:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 10;
const int mod = 998244353;
typedef long long ll;
int n,x,a[maxn];
ll fpow(ll a,ll b) {
	ll r = 1;
	while (b) {
		if (b & 1) r = r * a % mod;
		b >>= 1;
		a = a * a % mod;
	}
	return r;
}
ll fac[maxn],ifac[maxn];
ll C(int x,int y) {
	if (x < y || x < 0 || y < 0) return 0;
	return fac[x] * ifac[y] % mod * ifac[x - y] % mod;
}
int main() {
	fac[0] = 1;
	for (int i = 1; i <= maxn - 10; i++) fac[i] = fac[i - 1] * i % mod;
	ifac[maxn - 10] = fpow(fac[maxn - 10],mod - 2);
	for (int i = maxn - 10 - 1; i >= 0; i--) ifac[i] = ifac[i + 1] * (i + 1) % mod;
	scanf("%d%d",&n,&x);
	for (int i = 1; i <= n; i++)
		scanf("%d",&a[i]);
	ll t1 = C(n - 2,x - 2) * fpow(C(n,x),mod - 2) % mod;
	ll t2 = C(n - 3,x - 3) * fpow(C(n,x),mod - 2) % mod;
	ll t3 = C(n - 4,x - 4) * fpow(C(n,x),mod - 2) % mod;
	ll sum = 0;
	for (int i = 1; i <= n; i++)
		sum = (sum + 1ll * a[i] * (a[i] - 1)) % mod;
	ll ans = (1ll * x * x - 2ll * x * (n - 1) % mod * t1 % mod) % mod;
	ans = (ans + (1ll * (n - 1) * (n - 2) - sum) % mod * t3 % mod) % mod;			//兩條邊沒有交點 
	ans = (ans + (1ll * sum * t2 % mod)) % mod;										//有一個交點
	ans = (ans + (n - 1) * t1 % mod) % mod;
	if (ans < 0) ans += mod;
	printf("%lld\n",ans);
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章