洛谷 CF1999F Expected Median 题解

洛谷 CF1999F Expected Median 题解


题目传送门

做法

首先, a i ∈ { 0 , 1 } a_i \in \{0, 1\} ai{0,1},这对我们解题提供了很大的便利。
c n t 0 cnt_0 cnt0 0 0 0 出现的个数。我们发现,一个序列的中位数为
{ 0 if  c n t 0 ≥ k + 1 2 1 if  c n t 0 < k + 1 2 \begin{cases} 0 &\text{if } cnt_0 \ge \frac{k + 1}{2} \\ 1 &\text{if } cnt_0 < \frac{k + 1}{2} \end{cases} {01if cnt02k+1if cnt0<2k+1
所以题目相当于是问中位数 = 1 = 1 =1 a a a 的子序列有多少个。

假设我们知道了 c n t 0 cnt0 cnt0,那么怎么算中位数 = 1 = 1 =1 a a a 的子序列有多少个呢。
既然是方案数,那我们考虑组合计数。

首先,如果 c n t 0 > = k + 1 2 cnt_0 >= \frac{k + 1}{2} cnt0>=2k+1,那么方案数就是 0 0 0,否则就是选择 0 0 0 的方案数 × \times × 选择 1 1 1 的方案数

选择 1 1 1 的数量 c n t 1 = k − c n t 0 cnt_1 = k - cnt_0 cnt1=kcnt0
0 0 0 的数量为 m a x 0 max_0 max0 1 1 1 的数量为 m a x 1 max_1 max1

0 0 0 的选择方案数量为 ( m a x 0 c n t 0 ) \dbinom{max_0}{cnt_0} (cnt0max0) 1 1 1 的选择方案为 ( m a x 1 c n t 1 ) \dbinom{max_1}{cnt_1} (cnt1max1)

则总方案数为 ( m a x 0 c n t 0 ) × ( m a x 1 c n t 1 ) \dbinom{max_0}{cnt_0} \times \dbinom{max_1}{cnt_1} (cnt0max0)×(cnt1max1)

c n t 0 cnt_0 cnt0 怎么知道?枚举呗。
最终答案为
∑ c n t 0 m a x 0 ( m a x 0 c n t 0 ) × ( m a x 1 k − c n t 0 ) [ c n t 0 < k − c n t 0 ] \displaystyle \sum_{cnt_0}^{max0} \dbinom{max_0}{cnt_0} \times \dbinom{max_1}{k - cnt_0}[cnt_0 < k - cnt_0] cnt0max0(cnt0max0)×(kcnt0max1)[cnt0<kcnt0]

实现

组合数按公式在这里插入图片描述
预处理就行,预处理阶乘和阶乘的逆元,不知道什么是逆元的点击查看。

然后直接求答案即可

代码

#include <iostream>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
typedef long long LL;
const int N = 200010, mod = 1e9 + 7;
 
int n, k;
int a[N];
int fact[N], infact[N];
 
int qmi(int a, int k)
{
	int ans = 1;
	while (k)
	{
		if (k & 1) ans = (LL)ans * a % mod;
		a = (LL)a * a % mod;
		k >>= 1;
	}
	return ans;
}
 
void init(int n)
{
	fact[0] = infact[0] = 1;
	for (int i = 1; i <= n; i ++ )
	{
		fact[i] = (LL)fact[i - 1] * i % mod;
		infact[i] = (LL)infact[i - 1] * qmi(i, mod - 2) % mod;
	}
}
 
int C(int a, int b)
{
	if (a < b) return 0;
	return (LL)fact[a] * infact[b] % mod * infact[a - b] % mod;
}
 
int main()
{
	int T;
	scanf("%d", &T);
	init(N - 5);
	while (T -- )
	{
		scanf("%d%d", &n, &k);
		int max0 = 0, max1 = 0;
		for (int i = 1; i <= n; i ++ )
		{
			scanf("%d", &a[i]);
			if (a[i] == 0) max0 ++ ;
			else max1 ++ ;
		}
		
		int sum = 0;
		for (int cnt0 = 0; cnt0 <= max0; cnt0 ++ )
			if (cnt0 < (k + 1) / 2)
				sum = (sum + (LL)C(max0, cnt0) * C(max1, k - cnt0) % mod) % mod;
		
		printf("%d\n", sum);
	}
	
	return 0;
}

在这里插入图片描述

  • 15
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值