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
1
6 3
Output
Case #1: 42
思路:
首先看数据太大了,如果直接排列组合暴力,肯定过不了;然后想了想,反过来不就行了
但是这样仍然会wa,因为当k比较大的时候,k的阶乘作为分母会爆精度;所以应该用逆元求解;然后有一个坑点来了;如果你每一次分子都重新求一次,每次分母也重新求一次逆元,可能就会超时了,这是你会发现,每次求下一步算式的时候,都是在上一步结果的基础上把分子乘以(n-i),分母再乘以一个k(也就是在乘以一个k的逆元),这样就ac了;
#include <iostream>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll quick(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans=ans*a%mod;
b>>=1;
a=a*a%mod;
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin >>t;
int id=1;
while(t--)
{
ll n,k;
cin >>n>>k;
ll ans=quick(2,n)-1;
ll temp=n;
for(int i=1;i<k;i++)
{
ans=(ans-temp+mod)%mod;
temp=(temp*(n-i)%mod*quick(i+1,mod-2)%mod+mod)%mod;
}
cout <<"Case #"<<id++<<": "<<ans<<endl;
}
return 0;
}