题目链接: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=;
ll n,k; ll fpow(ll a,ll b)
{
ll r=,base=a%MOD;
while(b)
{
if(b&) r*=base,r%=MOD;
base*=base;
base%=MOD;
b>>=;
}
return r;
}
ll inv(ll a){return fpow(a,MOD-);} int main()
{
int T;
cin>>T;
for(int kase=;kase<=T;kase++)
{
scanf("%lld%lld",&n,&k);
if(n<k)
{
printf("Case #%d: 0\n",kase);
continue;
}
ll sum=+n,tmp=n;
for(ll i=;i<=k-;i++)
{
tmp=(((tmp*(n-i))%MOD)*inv(i+))%MOD;
sum=(sum+tmp)%MOD;
}
ll ans=(fpow(,n)-sum+MOD)%MOD;
printf("Case #%d: %d\n",kase,ans);
}
}

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

  1. Gym - 101775A Chat Group 组合数+逆元+快速幂

    It is said that a dormitory with 6 persons has 7 chat groups ^_^. But the number can be even larger: ...

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

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6467 简单数学题 Time Limit: 4000/2000 MS (Java/Others)    M ...

  3. HDU 6467.简单数学题-数学题 (“字节跳动-文远知行杯”广东工业大学第十四届程序设计竞赛)

    简单数学题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  4. Discrete Function(简单数学题)

    Discrete Function There is a discrete function. It is specified for integer arguments from 1 to N (2 ...

  5. JZOJ 5773. 【NOIP2008模拟】简单数学题

    5773. [NOIP2008模拟]简单数学题 (File IO): input:math.in output:math.out Time Limits: 1000 ms  Memory Limits ...

  6. Chat Group gym101775A(逆元,组合数)

    传送门:Chat Group(gym101775A) 题意:一个宿舍中又n个人,最少k(k >= 3)个人就可以建一个讨论组,问最多可以建多少个不同的讨论组. 思路:求组合数的和,因为涉及除法取 ...

  7. [JZOJ5773]【NOIP2008模拟】简单数学题

    Description       话说, 小X是个数学大佬,他喜欢做数学题.有一天,小X想考一考小Y.他问了小Y一道数学题.题目如下:      对于一个正整数N,存在一个正整数T(0<T&l ...

  8. Gym 102056I - Misunderstood … Missing - [DP][The 2018 ICPC Asia-East Continent Final Problem I]

    题目链接:https://codeforces.com/gym/102056/problem/I Warm sunshine, cool wind and a fine day, while the ...

  9. 组合数+逆元 A - Chat Group Gym - 101775A

    题目链接:https://cn.vjudge.net/contest/274151#problem/A 具体思路:我们可以先把所有的情况算出来,为2^n.然后不合法的情况减去就可以了.注意除法的时候要 ...

随机推荐

  1. InfluxDB源码阅读之snapshotter服务

    操作系统 : CentOS7.3.1611_x64 go语言版本:1.8.3 linux/amd64 InfluxDB版本:1.1.0 服务模块介绍 源码路径: github.com/influxda ...

  2. git rebase修改历史提交内容

    目录 简述 解决过程 简述 git提交历史中有一次提交的内容是有问题,因为每隔一段时间就要发一次版本,所以必须修改这次提交的内容,以便其不影响已经发布的版本. 大概是这样子的 A --- B ---- ...

  3. C#指定长度截取字符串 并进行拼接。

    需求:有一个字符串需要按照指定长度拆分出来,然后在增加一个字符串拼接上. /// <summary> /// 字符串截取并拼接 /// </summary> /// <p ...

  4. 通过JS页面唤醒app(安卓+ios)

    var browser = { versions: function () { var u = navigator.userAgent, app = navigator.appVersion; ret ...

  5. FFmpeg中subtitle demuxer实现

    [时间:2019-01] [状态:Open] [关键词:字幕,ffmpeg,subtitle,demuxer,源码] 0 引言 本文重心在于FFmpeg中subtitle demuxer的实现逻辑. ...

  6. 如何配置JVM系统属性及获取方式System.getProperty("pname")

    https://www.cnblogs.com/keyi/p/7721893.html

  7. .net core Dapper for MySql

    Dapper 语法比较简单,效率也比较快,速度接近IDataReader 甚至读取数据超过了DataTable,建议在实际项目可以结合EntityFramework Core 一起使用. 一.创建My ...

  8. Servlet 2.0 && Servlet 3.0 新特性

    概念:透传. Callback 在异步线程中是如何使用的.?? Servlet 2.0 && Servlet 3.0 新特性 Servlet 2.0 && Servle ...

  9. 利用final定义方法:这样的方法为一个不可覆盖的方法。

      常量(这里的常量指的是实例常量:即成员变量)赋值: ①在初始化的时候通过显式声明赋值.Final int x=3: ②在构造的时候赋值. 局部变量可以随时赋值.   利用final定义方法:这样的 ...

  10. 织梦channelartlist标签当前栏目高亮

    channelartlist标签完美支持currentstyle属性,实现自动加载当前栏目CSS样式,可以用来标识当前栏目位置的CSS,解决方法如下: 打开文件include\taglib\chann ...