Gym - 101775A Chat Group 组合数+逆元+快速幂
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?
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
1
6 3
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 组合数+逆元+快速幂的更多相关文章
- 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 ...
- 【2021 ICPC Asia Jinan 区域赛】 C Optimal Strategy推公式-组合数-逆元快速幂
题目链接 题目详情 (pintia.cn) 题目 题意 有n个物品在他们面前,编号从1自n.两人轮流移走物品.在移动中,玩家选择未被拿走的物品并将其拿走.当所有物品被拿走时,游戏就结束了.任何一个玩家 ...
- 牛客网 牛客小白月赛1 I.あなたの蛙が帰っています-卡特兰数,组合数阶乘逆元快速幂
I.あなたの蛙が帰っています 链接:https://www.nowcoder.com/acm/contest/85/I来源:牛客网 这个题有点意思,是卡特兰数,自行百度就可以.卡特兰数用处 ...
- 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...
- 刷题总结——分糖(ssoj 容斥原理+逆元+快速幂+组合数求插板)
题目: 题目描述 有 N 个(相同的)糖果,M 个(不同的)小朋友.M 和 N 满足:1≤M≤N≤100000(105).要求:1.每个小朋友都至少有一个糖果.2.不存在正整数 X(X>=2), ...
- 牛客网 Wannafly挑战赛11 B.白兔的式子-组合数阶乘逆元快速幂
链接:https://www.nowcoder.com/acm/contest/73/B来源:牛客网 B.白兔的式子 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K, ...
- Chat Group gym101775A(逆元,组合数)
传送门:Chat Group(gym101775A) 题意:一个宿舍中又n个人,最少k(k >= 3)个人就可以建一个讨论组,问最多可以建多少个不同的讨论组. 思路:求组合数的和,因为涉及除法取 ...
- 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 ...
- 51nod-1119 1119 机器人走方格 V2(组合数学+乘法逆元+快速幂)
题目链接: 1119 机器人走方格 V2 基准时间限制:1 秒 空间限制:131072 KB M * N的方格,一个机器人从左上走到右下,只能向右或向下走.有多少种不同的走法?由于方法数量可能很 ...
随机推荐
- arcgis中的geodatabase模型
简介Geodatabase是ESRI公司定义的一个为ArcGIS所用的数据框架,该框架定义了ArcGIS中用到的所有的数据类型.不管ArcGIS的数据存储到何处.以什么格式存储,都脱离不了该框架.也可 ...
- asp.net mvc 多文件上传
@{ ViewBag.Title = "多文件上传测试"; } <h2>多文件上传测试</h2> <form action="/Demo/I ...
- CTF 湖湘杯 2018 WriteUp (部分)
湖湘杯 2018 WriteUp (部分),欢迎转载,转载请注明出处! 1. CodeCheck(WEB) 测试admin ‘ or ‘1’=’1’# ,php报错.点击登录框下面的滚动通知,URL ...
- flash 概要分析器
这个东东 调试用.会每毫秒一次记录正在运行的函数 及相关数据 只在调试版flash player才能用. startSampling 开始记录 getSamples 获取记录 Sample ...
- web 安全知识点
XSS 新手指南:DVWA-1.9全级别教程(完结篇,附实例)之XSS https://www.jianshu.com/p/303206ae2471 https://www.netsparker.co ...
- luogu P3338 [ZJOI2014]力
传送门 首先化简原式\[F_j=\sum_{i<j}\frac{q_iq_j}{(i-j)^2}-\sum_{i>j}\frac{q_iq_j}{(i-j)^2},E_j=F_j/q_j\ ...
- 第27月第25天 clang -rewrite-objc main.m
1.clang -rewrite-objc main.m #import <objc/runtime.h> #import<objc/message.h> #import &l ...
- rem+js响应式布局的设置
直接调用代码即可,不过不同屏幕宽度要求会不同,相应修改一下就ok了 // rem响应式布局 (function(){ var html=document.querySelector('html') h ...
- webwork框架
以前都没有用过WebWork这个框架,只是听说过.没想到现在要用,所以就自学了一下.做了个小例子给大家分享下中间遇到的苦难和经验. 准备工作:首先要去下载WebWork框架的开发包.我用的2.2.6版 ...
- Maven SSH三大框架整合的加载流程
<Maven精品教程视频\day02视频\03ssh配置文件加载过程.avi;> 此课程中讲 SSH三大框架整合的加载流程,还可以,初步接触的朋友可以听一听. < \day02视频\ ...