Gym 101775A - Chat Group - [简单数学题][2017 EC-Final Problem A]

题目链接:http://codeforces.com/gym/101775/problem/A

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

 

题意:

听说一个寝室六个人有七个群?但实际上如果六人寝里三个人及以上组成不同的群的话,可以组成 42 个群……

现在给出一个 n 人寝室,要求计算 k 人及以上的不同的群可以建几个?

 

题解:

Cnk++Cnn=(Cn0+Cn1++Cnn)(Cn0+Cn1++Cnk1)

又根据二项式展开可知 2n=(1+1)n=Cn0×10×1n+Cn1×11×1n1++Cnn×1n×10=Cn0+Cn1++Cnn

因此答案即为 2n(Cn0+Cn1++Cnk1)

运用累乘的方式计算 Cn0,Cn1,,Cnk1,注意除法要使用逆元。

 

AC代码:

复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD=1000000007;
ll n,k;

ll fpow(ll a,ll b)
{
    ll r=1,base=a%MOD;
    while(b)
    {
        if(b&1) r*=base,r%=MOD;
        base*=base;
        base%=MOD;
        b>>=1;
    }
    return r;
}
ll inv(ll a){return fpow(a,MOD-2);}

int main()
{
    int T;
    cin>>T;
    for(int kase=1;kase<=T;kase++)
    {
        scanf("%lld%lld",&n,&k);
        if(n<k)
        {
            printf("Case #%d: 0\n",kase);
            continue;
        }
        ll sum=1+n,tmp=n;
        for(ll i=1;i<=k-2;i++)
        {
            tmp=(((tmp*(n-i))%MOD)*inv(i+1))%MOD;
            sum=(sum+tmp)%MOD;
        }
        ll ans=(fpow(2,n)-sum+MOD)%MOD;
        printf("Case #%d: %d\n",kase,ans);
    }
}
复制代码

 

posted @   Dilthey  阅读(930)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示