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

Posted dilthey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gym 101775A - Chat Group - [简单数学题][2017EC-Final]相关的知识,希望对你有一定的参考价值。

题目链接: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$ 人及以上的不同的群可以建几个?

 

题解:

$C_{n}^{k}+ cdots + C_{n}^{n} = (C_{n}^{0}+ C_{n}^{1} + cdots + C_{n}^{n}) - (C_{n}^{0}+ C_{n}^{1} + cdots + C_{n}^{k-1})$

又根据二项式展开可知 $2^n = (1+1)^{n} = C_{n}^{0} imes 1^{0} imes 1^{n} + C_{n}^{1} imes 1^{1} imes 1^{n-1} + cdots + C_{n}^{n} imes 1^{n} imes 1^{0} = C_{n}^{0} + C_{n}^{1} + cdots + C_{n}^{n}$

因此答案即为 $2^{n} - (C_{n}^{0}+ C_{n}^{1} + cdots + C_{n}^{k-1})$。

运用累乘的方式计算 $C_{n}^{0}, C_{n}^{1}, cdots, C_{n}^{k-1}$,注意除法要使用逆元。

 

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
",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
",kase,ans);
    }
}

 










以上是关于Gym 101775A - Chat Group - [简单数学题][2017EC-Final]的主要内容,如果未能解决你的问题,请参考以下文章

Gym - 101915D Largest Group 最大独立集 Or 状态压缩DP

第二十二 webchat

symfony框架中使用service

axios响应后scrollTop不起作用

需要将查询转换为 Eloquent Model laravel

uni-app 100群公告推送功能