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的方格,一个机器人从左上走到右下,只能向右或向下走.有多少种不同的走法?由于方法数量可能很 ...
随机推荐
- 12.scrapy框架
一.Scrapy 框架简介 1.简介 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个 ...
- PHP的核心配置详解
1.PHP核心配置详解 代码在不同的环境下执行的结果也会大有不同,可能就因为一个配置问题,导致一个非常高危的漏洞能够利用:也可能你已经找到的一个漏洞就因为你的配置问题,导致你鼓捣很久都无法构造成功的漏 ...
- sql leetcode -Duplicate Emails
第一种解法: select distinct p1.Email as Email from Person p1, Person p2 where p1.Email=p2.Email and p1.Id ...
- ArrayList的底层实现
package zy809; public class myArrayList { /** 存放元素 */ private Object[] data;// 创建一个数组引用. /** 元素的个数 * ...
- nnet3中的数据类型
目标与背景 之前的nnet1和nnet2基于Component对象,是一个组件的堆栈.每个组件对应一个神经网络层,为简便起见,将一个仿射变换后接一个非线性表示为一层网络,因此每层网络有两个组件.这些旧 ...
- Spark思维导图之Spark Streaming
- 【移动端】解决fixed定位闪动问题
经常我们会把导航按钮固定在页面的最底部位置,比如饿了么的首页 但是导航栏在页面滚动的时候会不断的闪动,这样的用户体验非常不好,那么可以使用下面的CSS样式处理一下 transform: transla ...
- Core Mvc传值ViewData、ViewBag和return view(model)
先定义一个Model类Student namespace Lession.Models { public class Student { public string Name { get; set; ...
- android gradle tools 3.X中dependencies, implementation和compile区别
在3.0版本中,compile 指令被标注为过时方法,而新增了两个依赖指令,一个是implement 和api,这两个都可以进行依赖添加,但是有什么区别呢? api 指令 完全等同于compile指令 ...
- Ubuntu16.04安装最新版nodejs
原文链接:https://www.jianshu.com/p/2b24cd430a7d