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. Spring整合redis配置文件详解

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. [译]C#7 Pattern Matching

    原文 public struct Square { public double Side { get; } public Square(double side) { Side = side; } } ...

  3. 唯一约束(UNIQUE_KEY)

    唯一约束可以保证记录的唯一性 唯一约束的字段可以为空值(NULL) 每张数据表可以存在多个唯一约束(主键只有一个) mysql> CREATE TABLE tb7( -> id SMALL ...

  4. Coursera, Machine Learning, SVM

    Support Vector Machine (large margin classifiers ) 1. cost function and hypothesis 下面那个紫色线就是SVM 的cos ...

  5. oracle.sql.TIMESTAMP转为java.sql.TIMESTAMP的方法

    /** * @reference oracle.sql.Datum.timestampValue(); * @return */ private Timestamp getOracleTimestam ...

  6. 添加,删除List<Map<String, Object>>元素

    这里讲对List<Map<String, Object>>的数据结构的添加和删除实例 添加 //初始化 List<Map<String, Object>> ...

  7. burp导入证书后仍然抓不到https包

    burp导入证书后仍然抓不到https包 如果你是导入证书有问题,请参考此博客 http://blog.csdn.net/zyw_anquan/article/details/47904495 有一点 ...

  8. C/C++ 内存对齐原则及作用

    struct/class/union内存对齐原则有四个: 1).数据成员对齐规则:结构(struct)(或联合(union))的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员存储 ...

  9. python,练习乌龟吃鱼

    ''' 1.首先要有一个画布 2.随机乌龟和鱼的位置 3.移动 ''' import random as r list_x = [0,10] list_y = [0,10] class Turtle: ...

  10. 【转】fnmatch模块的使用——主要作用是文件名称的匹配,并且匹配的模式使用的unix shell风格

    [转]fnmatch模块的使用 fnmatch模块的使用 此模块的主要作用是文件名称的匹配,并且匹配的模式使用的unix shell风格.fnmatch比较简单就4个方法分别是:fnmatch,fnm ...