AtCoder Beginner Contest 156 E.Roaming

AtCoder Beginner Contest 156 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 i goes to another room j(i≠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 (1e9+7).

Constraints

All values in input are integers.
3≤n≤2×1e5
2≤k≤1e9

Input

Input is given from Standard Input in the following format:

n k

Output

Print the number of possible combinations of numbers of people in the n rooms now, modulo (1e9+7).

Sample Input 1

3 2

Sample Output 1

10

Sample Input 2

200000 1000000000

Sample Output 2

607923868

Sample Input 3

15 6

Sample Output 3

22583772

通过打表找到答案 a n s = ∑ i = 0 m i n ( n − 1 , k ) C n i C n − 1 i ans=\sum_{i=0}^{min(n-1,k)}C_n^iC_{n-1}^{i} ans=i=0min(n1,k)CniCn1i,AC代码如下:

#include
using namespace std;
typedef long long ll;
const int N=2e5+5;
const ll mod=1e9+7;
ll n,k;
ll power(ll a,ll b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;}
ll F[N],I[N];
void init(){
    F[0]=1;
    for(ll i=1;i<=N;i++){
        F[i]=F[i-1]*i%mod;
    }
    I[N]=power(F[N],mod-2);
    for(ll i=N;i--;){
        I[i]=I[i+1]*(i+1)%mod;
    }
}

ll C(ll n, ll k){
    return F[n]*I[n-k]%mod*I[k]%mod;
}

int main(){
    init();
    cin>>n>>k;
    ll sum=0;
    for(ll i=0;i<n&&i<=k;i++){
       sum=(sum+C(n,i)*C(n-1,i))%mod;
    }
    cout<<sum;
    return 0;
}

你可能感兴趣的:(思维,AtCoder,数论)