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. 字符设备驱动(一)---led

    一.总体架构 二.硬件电路 1.硬件原理图 2.寄存器配置 三.代码编写 写linux的设备驱动操作的是系统的虚拟地址,并不是像裸机程序一样操作的是物理地址. 物理地址要映射成虚拟地址,就要用到ior ...

  2. 【51nod 1191】消灭兔子

    Description 有N只兔子,每只有一个血量B[i],需要用箭杀死免子.有M种不同类型的箭可以选择,每种箭对兔子的伤害值分别为D[i],价格为P[i](1 <= i <= M).假设 ...

  3. python-类型转化

    s='1234' a=int(s)     字符串转换成整数 s=str(a)    整数转换成字符串

  4. Vue 中循环绑定v-module表单

    子组件有一个列表要做输入验证 <template> <div> <el-form :model="value" ref="numberVal ...

  5. Http 持久连接与 HttpClient 连接池

    一.背景 HTTP协议是无状态的协议,即每一次请求都是互相独立的.因此它的最初实现是,每一个http请求都会打开一个tcp socket连接,当交互完毕后会关闭这个连接. HTTP协议是全双工的协议, ...

  6. border-radius图解

    自己看了理解的border-radius: 设置参数: 100*100的正方形,第一个:border-top-left-radius:100px 100px,即圆的半径为100px.圆弧与上和左bor ...

  7. linux atoi

    atoi 只能针对字符串为数字, 对字符串为十六进制.八进制的不能进行转化

  8. Maven SSH三大框架整合的加载流程

    <Maven精品教程视频\day02视频\03ssh配置文件加载过程.avi;> 此课程中讲 SSH三大框架整合的加载流程,还可以,初步接触的朋友可以听一听. < \day02视频\ ...

  9. Django学习手册 - Form 插件

    """ 核心: from django import formsfrom django.forms import fieldsfrom django.forms impo ...

  10. OVS-----CentOS7上搭建基于Open vSwitch的VxLAN隧道实验

    一.关于VXLAN VXLAN 是 Virtual eXtensible LANs 的缩写,它是对 VLAN 的一个扩展,是非常新的一个 tunnel 技术,在Open vSwitch中应用也非常多. ...