2017-2018 ACM-ICPC ECL-Final 2017 A. Chat Group【组合数+逆元】

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

题意:

\sum_{n}^{i=m} C_{n}^{i}。m即为k。

思路:

由于n比较大,m比较小,式子可以转化为2^n-\sum_{m}^{i=0} C_{n}^{i}。并且求\sum_{m}^{i=0} C_{n}^{i}的时候可以线性的推过去。先预处理阶乘的逆元。

复杂度为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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值