Gym - 101775A

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

分析
这道题的坑点是大数相除时转化为乘逆元,逆元用费马小定理求取
(自闭的一天)

代码

#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
ll quick(ll a,ll b)
{
	ll res=1;
	while(b>0)
	{
		if(b&1)
			res=(res*a+mod)%mod;
		a=(a*a+mod)%mod;
		b>>=1;
	}
	return (res+mod)%mod;
}
int main()
{
	int n;
	int time=0;
	scanf("%d",&n);
	while(n--)
	{
		time++;
		ll x,y;
		ll ans;
		ll ant;
		ll aux=0;
		ll anx=1;
		scanf("%lld%lld",&x,&y);
		printf("Case #%d: ",time);
		if(x==y)
		{
			printf("1\n");
			continue;
		}
		if(y>x)
		{
			printf("0\n");
			continue;
		}
		ans=quick(2,x);
//		printf("ans : %lld\n",ans);
		ant=1;
		for(int i=1;i<y;i++)
		{
			ant=(ant*(x-i+1)+mod)%mod;
			anx=(anx*i+mod)%mod;
			aux=(aux+(ant*quick(anx,mod-2)%mod+mod)%mod+mod)%mod;
		}
		ans=(ans-aux+mod-1)%mod;
		printf("%lld\n",ans);
	}
	return 0;
}
发布了40 篇原创文章 · 获赞 2 · 访问量 877

猜你喜欢

转载自blog.csdn.net/qq_43851311/article/details/102096336
今日推荐