跳到正文

题解

[ABC156E] Roaming 题解

cccyyyxxx约 2 分钟
原文
文章信息与版本

文章信息

发布
2023年3月25日
收录
2026年11月4日
更新
2026年11月8日
篇幅
约 2 分钟
获赞
20

版本

  1. v1当前

题目传送门

分析:

这种容易看出是数学题。当所有人都移动完后,空房子(人数为零)的数量最少为 00 最多为 min(n−1,k)min(n-1,k) 。因此我们可以枚举最后空房子的数量。当空房子数量为 ii 时,非空房子有 n−in-i 个,那我们就需要将 nn 个人放入 n−in-i 个房子,运用插板法,在 n−1n-1 个空中,插入 n−i−1n-i-1 个板。那么对于每一个 ii ,答案为 Cni×Cn−1n−i−1C_n^i \times C_{n-1}^{n-i-1} 。最后累加答案即可。

注意:

因为取模,且数据范围不小,因此需要逆元。

code:

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll Maxn = 2e5+10;
const ll Mod = 1e9+7;
ll fac[Maxn],n,k,ans,Max;
inline ll qsm(ll a,ll b)
{
	ll cnt=1;
	while(b)
	{
		if(b&1)
			cnt=cnt*a%Mod;
		b>>=1;
		a=a*a%Mod;
	}
	return cnt;
}
inline ll Inv(ll a)
{
	return qsm(a,Mod-2)%Mod;
}
inline void init()
{
	Max=min(n-1,k);
	fac[0]=1;
	for(int i=1;i<=n;i++)
		fac[i]=i*fac[i-1]%Mod;
}
inline ll C(ll n,ll m)
{
	ll cnt;
	cnt=((fac[n]*Inv(fac[m]))%Mod*Inv(fac[n-m]))%Mod;
	return cnt;
}
int main()
{
	cin>>n>>k;
	init();
	for(int i=0;i<=Max;i++)
		ans=(ans+(C(n,i)*C(n-1,n-i-1))%Mod+Mod*2)%Mod;
	cout<<ans<<endl;
	return 0;
}