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} \times 1^{0} \times 1^{n} + C_{n}^{1} \times 1^{1} \times 1^{n-1} + \cdots + C_{n}^{n} \times 1^{n} \times 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\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);
    }
}

原文地址:https://www.cnblogs.com/dilthey/p/9863926.html

时间: 2024-11-09 06:24:38

Gym 101775A - Chat Group - [简单数学题][2017EC-Final]的相关文章

Codeforces Round #262 (Div. 2)460A. Vasya and Socks(简单数学题)

题目链接:http://codeforces.com/contest/460/problem/A A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put o

poj 3117 World Cup(简单数学题)

题目链接:http://poj.org/problem?id=3117 World Cup Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8634   Accepted: 4327 Description A World Cup of association football is being held with teams from around the world. The standing is based on

hdu 2368 Alfredo&#39;s Pizza Restaurant(简单数学题)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2368 题目很简单,但是比较恶心,用sqrt WA到死也不过,不用秒过: 忍不住吐槽一下; Problem Description Traditionally after the Local Contest, judges and contestants go to their favourite restaurant,

hdu2374 A Game with Marbles(简单数学题)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2374 Problem Description There are n bowls, numbered from 1 to n. Initially, bowl i contains mi marbles. One game step consists of removing one marble from a bowl.

UVA 12714 Two Points Revisited(简单数学题)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4452 题意: 给出两个点组成一条直线,要你任意输出两个点,要求这两点组成的直线和给出的直线垂直(注意输出的点不能有负数): 代码如下: #include <cstdio> int main(

JZOJ5773 简单数学题

escription 话说, 小X是个数学大佬,他喜欢做数学题.有一天,小X想考一考小Y.他问了小Y一道数学题.题目如下: 对于一个正整数N,存在一个正整数T(0<T<N),使得的值是正整数. 小X给出N,让小Y给出所有可能的T.如果小Y不回答这个神奇的大佬的简单数学题,他学神的形象就会支离破碎.所以小Y求你帮他回答小X的问题. Input  一个整数N. Output 第一个数M,表示对于正整数N,存在M个不同的正整数T,使得是整数. 后面是M个数,每一个数代表可能的正整数T(按从小到大的顺

HDU 6467 简单数学题 【递推公式 &amp;&amp; O(1)优化乘法】(广东工业大学第十四届程序设计竞赛)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6467 简单数学题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 308    Accepted Submission(s): 150 Problem Description 已知 F(n)=∑i=1n(i×∑j=inCij) 求 F(n) m

geometry(简单数学题)

geometry Accepts: 324 Submissions: 622 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description There is a point PP at coordinate (x,y)(x,y). A line goes through the point, and intersects with the postive p

访问修饰限定符的简单总结、final/abstruct/interface对类的限制、自动加载机制、序列化与反序列化【数据持久化和对象的序列化问题】、对象的拷贝(按引用是因为对象标识)和克隆(__clone方法中的this指向)

1.针对访问修饰限定符的理解只需要两点:(1)针对的是类的概念和访问代码的位置来确定是否能够访问(2)对访问修饰限定符的使用时只需要对该成员的使用场景注意即可[也就是内部,继承类,外部进行访问的权限] 不需要对内部进行太多理解[需要对php底层理解时进行理解] [重点][用途]通过访问修饰限定符将内部成员的权限合理的限制,然后再使用公共接口来调用这个基本服务,保证外部不能访问其内部的构件[这样既能够通过类内的设置,将内部的功能实现更好的限制,只有最外层的接口可以正常被访问到,而不了解内部的业务]