题目网址:点击进入
problem:
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
大致题意:
T组数据,输入n,k,计算C(n,k)+C(n,k+1)…+C(n,n)的总和
思路:
C(a,b)的求法就是 a! / b!*(a-b)!,这里还有一个重要的理论,C(a,0)+C(a,1)…+C(a,a)=2^a,所以计算这题我们可以用这个总和公式减去C(n.0)+C(n,1)…+C(n,k-1)的值,将n数据范围的复杂度降到k,这样在t组情况下就是10W的复杂度纯暴力解决
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long int LLD;
LLD mod=1000000007;
LLD chengfa(LLD a,LLD b)///快速幂
{
LLD ji=1;
while (b)
{
if (b&1)
{
ji=ji*a%mod;
}
a=a*a%mod;
b=b/2;
}
return ji;
}
int main()
{
LLD t,n,k;
scanf("%lld",&t);
for (LLD i=1;i<=t;i++)
{
scanf("%lld %lld",&n,&k);
if (n<k)
{
printf("Case #%lld: 0\n",i);///如果n<k的情况下直接结束
continue;///输出0,降低复杂度
}
LLD big=chengfa(2,n)-1;///快速幂求2^n减去C(n,0)=1;
LLD sum=1;///求C(n,1)......+C(n,k-1)的总和
for (LLD j=1;j<k;j++)
{
sum=sum*(n-j+1)%mod;///C(n,1)*(n-1)/2=C(n,2)递推思想
sum=sum*chengfa(j,mod-2)%mod;///逆元的费马小定理求得除法取模
//printf("%lld\n",sum);
big=(big+mod-sum)%mod;
}
printf("Case #%lld: %lld\n",i,big);
}
return 0;
}