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. CMDB服务器管理系统【s5day88】:采集资产-文件配置(二)

    上节疑问: 1.老师我们已经写到global_settings里了,为什么还要写到__init__.py setting 这的作用是为了:整合起两个的组合global_settings和setting ...

  2. httpClient4.5 closeableHttpClient用法

    HttpClient一 简介1.尽管java.net包提供了基本通过HTTP访问资源的功能,但它没有提供全面的灵活性和其它很多应用程序需要的功能.HttpClient就是寻求弥补这项空白的组件,通过提 ...

  3. C++实现递归版二分搜索算法

    无聊撸了一个,没啥技术含量,别吐槽.. #include <iostream> using namespace std; int BinarySearch(int* nums,int ke ...

  4. sessionStorage数组、对象的存储和读取

    一个对象的demo如下: var obj = { name:"name", age:18, love:"美女" } sessionStorage.setItem ...

  5. svn 支持中文显示

    https://blog.csdn.net/chentengkui/article/details/77543498 https://blog.csdn.net/bugall/article/deta ...

  6. Fast R-CNN(理解)

    0 - 背景 经典的R-CNN存在以下几个问题: 训练分多步骤(先在分类数据集上预训练,再进行fine-tune训练,然后再针对每个类别都训练一个线性SVM分类器,最后再用regressors对bou ...

  7. redux知识点

    1.关于传参 点击跳转 两种 动态路由 url 但是取值需要自己处理 关于动态跳转(js执行跳转) 关于异步加载组件 创建loadable app.js中 在 要加载的异步组件中   (这样接收参数不 ...

  8. warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

    在C++中, char* p = "abc"; // valid in C, invalid in C++ 会跳出警告:warning: ISO C++ forbids conve ...

  9. POJ 2031 Building a Space Station (计算几何+最小生成树)

    题目: Description You are a member of the space station engineering team, and are assigned a task in t ...

  10. java类型转换小技巧

    mysql 之排序显示行号 select @r := @r+1 as rownum,birth,cardNo from card,(select @r:=0)torder by birth ASC