Gym 101775A - Chat Group - [简单数学题][2017 EC-Final Problem A]

时间:2022-06-22 16:41:46

题目链接:http://codeforces.com/gym/101775/problem/A

It is said that a dormitory with 6 persons has 7 chat groups ^_^. But the number can be even larger: since every 3 or more persons could make a chat group, there can be 42 different chat groups.

Given N persons in a dormitory, and every K or more persons could make a chat group, how many different chat groups could there be?

Input
The input starts with one line containing exactly one integer T which is the number of test cases.

Each test case contains one line with two integers N and K indicating the number of persons in a dormitory and the minimum number of persons that could make a chat group.

1 ≤ T ≤ 100.
1 ≤ N ≤ 10^9.
3 ≤ K ≤ 10^5.

Output
For each test case, output one line containing "Case #x: y" where x is the test case number (starting from 1) and y is the number of different chat groups modulo 1000000007.

Example
Input
1
6 3
Output
Case #1: 42

题意:

听说一个寝室六个人有七个群?但实际上如果六人寝里三个人及以上组成不同的群的话,可以组成 $42$ 个群……

现在给出一个 $n$ 人寝室,要求计算 $k$ 人及以上的不同的群可以建几个?

题解:

$C_{n}^{k}+ \cdots + C_{n}^{n} = (C_{n}^{0}+ C_{n}^{1} + \cdots + C_{n}^{n}) - (C_{n}^{0}+ C_{n}^{1} + \cdots + C_{n}^{k-1})$

又根据二项式展开可知 $2^n = (1+1)^{n} = C_{n}^{0} \times 1^{0} \times 1^{n} + C_{n}^{1} \times 1^{1} \times 1^{n-1} + \cdots + C_{n}^{n} \times 1^{n} \times 1^{0} = C_{n}^{0} + C_{n}^{1} + \cdots + C_{n}^{n}$

因此答案即为 $2^{n} - (C_{n}^{0}+ C_{n}^{1} + \cdots + C_{n}^{k-1})$。

运用累乘的方式计算 $C_{n}^{0}, C_{n}^{1}, \cdots, C_{n}^{k-1}$,注意除法要使用逆元。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD=;
ll n,k; ll fpow(ll a,ll b)
{
ll r=,base=a%MOD;
while(b)
{
if(b&) r*=base,r%=MOD;
base*=base;
base%=MOD;
b>>=;
}
return r;
}
ll inv(ll a){return fpow(a,MOD-);} int main()
{
int T;
cin>>T;
for(int kase=;kase<=T;kase++)
{
scanf("%lld%lld",&n,&k);
if(n<k)
{
printf("Case #%d: 0\n",kase);
continue;
}
ll sum=+n,tmp=n;
for(ll i=;i<=k-;i++)
{
tmp=(((tmp*(n-i))%MOD)*inv(i+))%MOD;
sum=(sum+tmp)%MOD;
}
ll ans=(fpow(,n)-sum+MOD)%MOD;
printf("Case #%d: %d\n",kase,ans);
}
}