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 3≤n≤2×105
- 2 ≤ k ≤ 1 0 9 2 \leq k \leq 10^9 2≤k≤109
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=0∑min(n−1,k)(Cni)∗(Cn−1i)
前面的
C
n
i
C_n^i
Cni为从n个房间中挑选i个房间使其不存在人,后边的
C
n
−
1
i
C_{n-1}^i
Cn−1i为在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;
}
道阻且长,且行且珍惜。