A. Chat Group
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output
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 ≤ 109.
- 3 ≤ K ≤ 105.
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
Copy
1 6 3
output
Copy
Case #1: 42
题意:
求。m即为k。
思路:
由于n比较大,m比较小,式子可以转化为。并且求
的时候可以线性的推过去。先预处理阶乘的逆元。
复杂度为O(t*m).
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 100005
#define mod 1000000007
ll fac[MAXN],dis[MAXN];
ll inv[MAXN];
ll QPow(ll x,ll n)
{
ll ret=1;
ll tmp=x%mod;
while(n)
{
if(n&1) ret=(ret*tmp)%mod;
tmp=tmp*tmp%mod;
n>>=1;
}
return ret;
}
void init()
{
fac[0]=1;
for(int i=1;i<MAXN;i++)
fac[i]=fac[i-1]*i%mod;
inv[MAXN-1]=QPow(fac[MAXN-1],mod-2);
for(int i=MAXN-2;i>=0;i--)
inv[i]=inv[i+1]*(i+1)%mod;
}
ll n,m;
int main()
{
int t;
scanf("%d",&t);
init();
for(int tt=1;tt<=t;tt++)
{
scanf("%lld%lld",&n,&m);
dis[0]=1;
ll ans=(QPow(2,n)-1+mod)%mod;
for(int i=1;i<m;i++)
{
dis[i]=dis[i-1]*(n-i+1)%mod;
ans=(ans-dis[i]*inv[i]%mod+mod)%mod;
}
printf("Case #%d: %lld\n",tt,ans);
}
return 0;
}