Chat Group 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

思路:
首先看数据太大了,如果直接排列组合暴力,肯定过不了;然后想了想,反过来不就行了
在这里插入图片描述
但是这样仍然会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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值