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
1
6 3
Output
Case #1: 42

题解:

看起来很简单的题   我还是做了很久 基础知识点有很多漏洞吧

本题就是一个 C(n,k)+C(n,k+1)+...+C(n,n) 的过程

如果暴力算的话会超时  哭叽叽

要知道组合数的总和是2^n

所以转化成 2^n - C(n,0)+C(n,1)+...+C(n,k-1)

加减乘可直接用同余定理直接拆开算  除法不行

所以利用费马小定理(假如p是质数,且gcd(a,p)=1,那么 a(p-1)≡1(mod p),即:假如a是整数,p是质数,且a,p互质(即两者只有一个公约数1),那么a的(p-1)次方除以p的余数恒等于1。)

得到推论  a*a(p-2)≡1(mod p)对于整数a,p,a关于p的逆元就是a^(p-2),直接快速幂解之即可,但注意这个定理要求a,p互质

利用上述方法求得逆元

计算乘方时用到快速幂

如此这一问题便解决了

(我的第一篇博客,欢迎大家批评指正^-^)

#include <iostream>
#include <stdio.h> using namespace std;
typedef long long ll;
const int N=1e5+;
const ll mod=1e9+; ll quick(ll a,ll b) //快速幂
{
ll ans=;
a%=mod;
while(b){
if(b&) ans=ans*a%mod;
a=a*a%mod;
b>>=;
}
return ans;
} int main()
{
ll n,k,i;
int l=,t;
scanf("%d",&t);
while(t--){
scanf("%I64d%I64d",&n,&k);
ll ans=quick(,n)-; //2^n-C(n,0)
ll cur=n;
for(i=;i<k;i++){
ans=(ans+mod-cur)%mod; //emmm 为啥要加个mod我还不太了解,如果有大佬知道,麻烦指点一下
cur=cur*(n-i)%mod;
cur=cur*quick(i+,mod-)%mod; //乘以逆元
}
printf("Case #%d: %I64d\n",l++,ans);
}
return ;
}

Gym - 101775A Chat Group 组合数+逆元+快速幂的更多相关文章

  1. 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 cha ...

  2. 【2021 ICPC Asia Jinan 区域赛】 C Optimal Strategy推公式-组合数-逆元快速幂

    题目链接 题目详情 (pintia.cn) 题目 题意 有n个物品在他们面前,编号从1自n.两人轮流移走物品.在移动中,玩家选择未被拿走的物品并将其拿走.当所有物品被拿走时,游戏就结束了.任何一个玩家 ...

  3. 牛客网 牛客小白月赛1 I.あなたの蛙が帰っています-卡特兰数,组合数阶乘逆元快速幂

    I.あなたの蛙が帰っています   链接:https://www.nowcoder.com/acm/contest/85/I来源:牛客网     这个题有点意思,是卡特兰数,自行百度就可以.卡特兰数用处 ...

  4. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

  5. 刷题总结——分糖(ssoj 容斥原理+逆元+快速幂+组合数求插板)

    题目: 题目描述 有 N 个(相同的)糖果,M 个(不同的)小朋友.M 和 N 满足:1≤M≤N≤100000(105).要求:1.每个小朋友都至少有一个糖果.2.不存在正整数 X(X>=2), ...

  6. 牛客网 Wannafly挑战赛11 B.白兔的式子-组合数阶乘逆元快速幂

    链接:https://www.nowcoder.com/acm/contest/73/B来源:牛客网 B.白兔的式子   时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K, ...

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

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

  8. ACM学习历程—HDU5490 Simple Matrix (数学 && 逆元 && 快速幂) (2015合肥网赛07)

    Problem Description As we know, sequence in the form of an=a1+(n−1)d is called arithmetic progressio ...

  9. 51nod-1119 1119 机器人走方格 V2(组合数学+乘法逆元+快速幂)

    题目链接: 1119 机器人走方格 V2 基准时间限制:1 秒 空间限制:131072 KB    M * N的方格,一个机器人从左上走到右下,只能向右或向下走.有多少种不同的走法?由于方法数量可能很 ...

随机推荐

  1. vs2013 配置支持https的libcurl

    需求:在vs2013上配置支持https协议的libcurl. 环境:win7(64位),vs2013 一.安装openssl: 需要先安装nasm和ActivePerl. nasm需要手动配置环境变 ...

  2. 十八、Linux 进程与信号---进程介绍

    18.1 进程的概念 程序:程序(program)是存放再磁盘文件中的可执行文件 进程 程序的执行实例被称为进程(process) 一个程序的执行实例可能由多个 进程具有独立的权限和职责.如果系统中某 ...

  3. 小程序开发 从简单的 crud 开始

    关键字:“小程序 API” [WXML 完成布局] <view> == <div> {{}}  == <%= %> ejs | jsp2 <block wx: ...

  4. js apply使用

    js中apply方法的使用 1.对象的继承,一般的做法是复制:Object.extend prototype.js的实现方式是:  Object.extend = function(destinati ...

  5. firefox 播放h5爱奇艺视频

    先安装 violentmonkey 扩展(https://addons.mozilla.org/zh-CN/firefox/addon/violentmonkey/), 在安装这个脚本 https:/ ...

  6. 电脑丢失api-ms-win-core-libraryloader-|1-1-1.dll怎么办

    电脑从win7升级到win10,到98%的时候提示说丢失.dll,如图,我是64位系统,怎么解决这个问题呢?在脚本之家下载了 放到system32中也没有用,在线等,谢谢! 用C:\Windows\S ...

  7. 第26月第20天 springboot

    --------------------- 1.pom.xml中添加支持web的模块: <dependency> <groupId>org.springframework.bo ...

  8. Lattice

    Lattice是一个无环WFSA,结点可以是hmm状态.hmm(音素).词,每个结点是一段音频在某个时间的对齐 用训练好的声学模型.现成的语言模型和发音字典构建解码网络(wfst),最后将提取的测试集 ...

  9. python 的基础 学习 第七天 is id 编码的补充

    1,== 两个等号比较的是数值,is比较的是内存地址.print(id())查看的内存地址. 小数据池只存在于数字与字符串中,数字   是 -5^^256,是为了节省空间. 字符串1,如果含有特殊字符 ...

  10. linux 僵屍进程

    参考链接 :  http://soft.chinabyte.com/os/5/12172005.shtml