AtCoder abc156_E_Roaming(组合数学,隔板法)

AtCoder abc156_e_Roaming

Problem Statement

There is a building with n rooms, numbered 1 to n.

We can move from any room to any other room in the building.

Let us call the following event a move: a person in some room ii goes to another room j ( i i i ≠ \neq =$ j$).

Initially, there was one person in each room in the building.

After that, we know that there were exactly k moves happened up to now.

We are interested in the number of people in each of the n rooms now. How many combinations of numbers of people in the n rooms are possible?

Find the count modulo (10^9 + 7).

Constraints

  • All values in input are integers.
  • 3 ≤ n ≤ 2 × 1 0 5 3 \leq n \leq 2 \times 10^5 3n2×105
  • 2 ≤ k ≤ 1 0 9 2 \leq k \leq 10^9 2k109

Output

Print the number of possible combinations of numbers of people in the n rooms now, modulo (10^9 + 7)

题意:

一共有n间房子。一开始,每间房子中有一个人,每个人从其所在的房间移动到另一间房间,这个我们称之为一次移动,问你恰好k次移动以后,所有房间可能存在的人数有多少种可能组合?

思路:

当一个人从一个房间走到另一个房间,那么必然会造成该人原先的房间没有人存在。k步(k<=n-1)时,最多可能存在k个房间不存在人,如果k>n-1时,最多n-1个房间不存在人,因为你在k再怎么多,也不可能存在所有房间都不存在人,因此我们只需要从n个房间中挑选i个房间使得其房间不存在人,然后将所有n个人分配到剩下的n-i个房间就行了,这n-i个人怎么分配呢?这里就可以想到隔排,在n个人存在最多n-1块隔板,而n-i存在n-i-1块隔板,换而言之,就是将n个人分为n-i块,就是在n个人中插入n-i-1块隔板,就是其分配的方案数。

公式为:
s u m = ∑ i = 0 m i n ( n − 1 , k ) ( C n i ) ∗ ( C n − 1 i ) sum= \sum_{i=0}^{min(n-1,k)}(C_{n}^i)*(C_{n-1}^{i}) sum=i=0min(n1,k)(Cni)(Cn1i)
前面的 C n i C_n^i Cni为从n个房间中挑选i个房间使其不存在人,后边的 C n − 1 i C_{n-1}^i Cn1i为在n个人中插入n-i-1块隔板所得的方案数。

代码

#include<bits/stdc++.h>
#define Case int t;scanf("%d",&t);while(t--)
using namespace std;
typedef long long ll;
const int N=2e5+10;
const int M=1e6+10;
const int mod=1e9+7;
ll fac[N];
ll inv[N];
ll qpow(ll q,ll n){
	ll res=1;
	while(n){
		if(n&1)res=(res*q)%mod;
		n>>=1;
		q=(q*q)%mod;
	}
	return res;
}
void run(){
	ll n,k;
	scanf("%lld%lld",&n,&k);
	ll sum=0;
	for(ll i=0;i<=min(n-1,k);i++){
		sum=(sum+fac[n]*inv[i]%mod*inv[n-i]%mod*fac[n-1]%mod*inv[n-i-1]%mod*inv[i]%mod)%mod;
	}
	printf("%lld\n",sum);
}
int main(){
	fac[0]=1;
	for(ll i=1;i<=200000;i++){
		fac[i]=fac[i-1]*i%mod;
	}
	inv[200000]=qpow(fac[200000],mod-2);
	for(int i=199999;i>=0;i--){
		inv[i]=(inv[i+1]*(i+1))%mod;
	}
	run();
	return 0;
}

道阻且长,且行且珍惜。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值