2982: combination

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 664  Solved: 397
[Submit][Status][Discuss]

Description

LMZn个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样。那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ的一年有10007天,所以他想知道答案mod 10007的值。(1<=m<=n<=200,000,000)

Input

  第一行一个整数t,表示有t组数据。(t<=200)
  接下来t行每行两个整数n, m,如题意。

Output

T行,每行一个数,为C(n, m) mod 10007的答案。

Sample Input

4
5 1
5 2
7 3
4 2

Sample Output

5
10
35
6

HINT

Source

组合数裸题,一般我们选用lucas定理实现

人话定义环节——

组合数:从n个数中取m个数,一共的方法数,记作C(n,m)

lucas定理:C(n,m)=C(n%mod,m%mod)*C(n/mod,m/mod)%mod

这里直接给出实现代码

#pragma GCC optimize("O2")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<limits.h>
#include<ctime>
#define N 100001
typedef long long ll;
const int inf=0x3fffffff;
const int maxn=2017;
using namespace std;
inline ll read()
{
ll f=1,x=0;char ch=getchar();
while(ch>'9'|ch<'0')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch<='9'&&ch>='0')
{
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar();
}
return f*x;
}
const int mod=10007;
ll fac[mod<<1],inv[mod<<1];//fac函数存储n的阶乘,inv数组存放逆元的阶乘
void init()
{
fac[0]=fac[1]=1,inv[0]=inv[1]=1;
for(int i=2;i<mod;i++)fac[i]=fac[i-1]*i%mod;//预处理阶乘
for(int i=2;i<mod;i++)inv[i]=(mod-mod/i)*inv[mod%i]%mod;//递推求逆元
for(int i=2;i<mod;i++)inv[i]=inv[i-1]*inv[i]%mod;//预处理逆元的阶乘
}
ll lucas(ll n,ll m)
{
if(m>n)return 0;
if(n<=mod&&m<=mod)return fac[n]*inv[m]*inv[n-m]%mod;
return lucas(n%mod,m%mod)*lucas(n/mod,m/mod)%mod;
}
int main()
{
int t=read();
init();
while(t--)
{
ll n=read(),m=read();
printf("%d\n",lucas(n,m));
}
}

  

 

bzoj2982: combination(lucas定理板子)的更多相关文章

  1. [BZOJ2982]combination Lucas定理

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2982 $C(N,M)\% P = C(N\% P,M\% P) * C(N/P,M/P)\ ...

  2. 【BZOJ2982】combination Lucas定理

    [BZOJ2982]combination Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然, ...

  3. ZOJ 3557 & BZOJ 2982 combination[Lucas定理]

    How Many Sets II Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a set S = {1, 2, ..., n}, n ...

  4. BZOJ2982: combination Lucas模板

    2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 734  Solved: 437[Submit][Status][Di ...

  5. BZOJ2982: combination Lucas

    Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ的一年有10007天,所以他想知道答案 ...

  6. bzoj2982 combination——卢卡斯定理

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2982 卢卡斯定理裸题: 原准备1A来着,结果输出忘了加回车! 预处理阶乘或者现求都可以,感觉 ...

  7. BZOJ 2982 combination Lucas定理

    题目大意:发上来就过不了审核了--总之大意就是求C(n,m) mod 10007 m,n∈[1,2*10^8] 卢卡斯定理:C(n,m)=C(n%p,m%p)*C(n/p,m/p) mod p 要求p ...

  8. 2018.09.14 bzoj2982: combination(Lucas定理)

    传送门 貌似就是lucas的板子题啊. 练一练手感觉挺舒服的^_^ 代码: #include<bits/stdc++.h> #define mod 10007 #define ll lon ...

  9. Lucas定理模板【bzoj2982】【combination】

    (上不了p站我要死了,侵权度娘背锅) Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ ...

随机推荐

  1. 时间函数(1):time,ctime,gmtime,localtime

    asctime(将时间和日期以字符串格式表示) #include<time.h> 定义函数 char * asctime(const struct tm * timeptr); 函数说明 ...

  2. 5.CentOS7安装mariadb

    MariaDB 和 MySQL 使用是一样的,二者只要安装一个就行了 MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公司 ...

  3. [转载]关于在Linux下上传代码至Github

    刚开始使用Github没多长时间,所以很多地方不太熟练,看到阿里云上有一篇文章写得不错,故转载过来. 转载自:https://www.aliyun.com/jiaocheng/122729.html ...

  4. PHP 【二】

    EOF EOF(heredoc)是一种在命令行shell(如sh.csh.ksh.bash.PowerShell和zsh)和程序语言(像Perl.PHP.Python和Ruby)里定义一个字符串的方法 ...

  5. 批量检测服务器是否能telnet连通

    # coding: UTF-8import osimport sysimport jsonimport platformfrom _utils.patrol2 import data_format,r ...

  6. JavaScript入门学习笔记(表单验证)

    表单验证: 在数据被送到服务器之前对HTML表单中的输入数据进行验证,避免服务器频繁验证信息造成用户体验差. (1)表单数据是否为空 (2)输入的信息格式是否正确 (3)输入数据的类型是否正确 必填( ...

  7. 浅谈z-index

    z-index使用条件 CSS3之前,z-index属性只有和定位元素在(postion不为static的元素)一起的时候才会有作用,但CSS3中flex盒子的子元素也可以设置z-index.理论上来 ...

  8. NOIP2018旅行

    这道题考场上的时候暴力写RE了,我果然很菜. 看了一篇大佬的的题解才明白 dalao的题解 但是解释很少哇,为了造福人类,在下发一篇详细一点的题解. 预处理:用vector把与每个点相连的点存起来,排 ...

  9. vs调试的时候,指定的参数已超出有效值的范围。参数名:sit ,先仔细看看错误和我的一样不一样

    https://www.cnblogs.com/pei123/p/7694947.html 指定的参数已超出有效值的范围.参数名:sit ,先仔细看看错误和我的一样不一样 更新了1709就这样了,的确 ...

  10. spring boot 2.0 neo4j 使用

    参考文档 官方文档 http://spring.io/projects/spring-data-neo4j#learn https://docs.spring.io/spring-data/neo4j ...