Lucas定理模板【bzoj2982】【combination】
(上不了p站我要死了,侵权度娘背锅)
Description
LMZ有n个不同的基友,他每天晚上要选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
就是一道裸的组合数题,挂上自己的Lucas模板。
Lucas其实相当于将n、m按p进制分解求组合数,再乘起来。
当n < m时组合数的值为零,于是我们发现一个数按p进制分解后的C(a,b)中a有一半的几率小于b,而一旦出现这种情况答案就是0。所以在用Lucas定理时会有很大的概率答案为0。
当然这并不是Lucas定理有问题。仔细想想,如果我们把C(n,m)化为阶乘的形式,由于n和m都大于p(模数),所以阶乘的项里面很可能包含p或p的倍数。一旦出现p的倍数,答案就是0了。
模板
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#ifdef WIN32
#define RIN "%I64d"
#else
#define RIN "%lld"
#endif
#define ll long long
template <typename T>inline void read(T &res){
T k=1,x=0;char ch=0;
while(ch<'0'||ch>'9'){if(ch=='-')k=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
res=k*x;
}
const ll mod=10007;
ll jiec[10010],niy[10010];
ll n,m;
void exgcd(ll a,ll b,ll &x,ll &y){
if(b==0){
x=1,y=0;
return ;
}
ll x0,y0;
exgcd(b,a%b,x0,y0);
x=y0;
y=x0-(a/b)*y0;
}
ll inverse(ll a){
ll x,y;
exgcd(a,mod,x,y);
return (x%mod+mod)%mod;
}
void init(){
jiec[0]=niy[0]=1;
for(int i=1;i<mod;i++) jiec[i]=jiec[i-1]*i%mod;
niy[mod-1]=inverse(jiec[mod-1]);
for(int i=mod-2;i>=1;i--) niy[i]=niy[i+1]*(i+1)%mod;
}
ll comb(ll a,ll b){
return jiec[a]*niy[b]%mod*niy[a-b]%mod;
}
ll lucas(ll a,ll b){
if(a<b) return 0;
if(a==0&&b==0) return 1;
if(a<mod&&b<mod) return comb(a,b);
return lucas(a/mod,b/mod)*lucas(a%mod,b%mod)%mod;
}
void solve(){
read(n),read(m);
printf(RIN"\n",lucas(n,m));
}
int main(){
init();
int t;
read(t);
while(t--) solve();
return 0;
}
Lucas定理模板【bzoj2982】【combination】的更多相关文章
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1<p<=1e6,p必须为素数
typedef long long ll; /********************************** 大组合数取模之lucas定理模板,1<=n<=m<=1e9,1&l ...
- lucas定理 模板
lucas定理 (nm) mod p=(⌊np⌋⌊mp⌋)(n mod&VeryTh ...
- 【组合数+Lucas定理模板】HDU 3037 Saving
acm.hdu.edu.cn/showproblem.php?pid=3037 [题意] m个松果,n棵树 求把最多m个松果分配到最多n棵树的方案数 方案数有可能很大,模素数p 1 <= n, ...
- BZOJ 4403 2982 Lucas定理模板
思路: Lucas定理的模板题.. 4403 //By SiriusRen #include <cstdio> using namespace std; ; #define int lon ...
- Lucas定理模板
用于大组合数对p取模的计算. #include <cstdio> #include <iostream> #include <cmath> #include < ...
- HDU 3037 Saving Beans(Lucas定理模板题)
Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...
- xdoj-1057(Lucas定理的证明及其模板)
Lucas定理的证明: 转自百度百科(感觉写的还不错) 首先你需要这个算式: ,其中f > 0&& f < p,然后 (1 + x) nΞ(1 + x) sp+q Ξ ...
- 组合数取模&&Lucas定理题集
题集链接: https://cn.vjudge.net/contest/231988 解题之前请先了解组合数取模和Lucas定理 A : FZU-2020 输出组合数C(n, m) mod p (1 ...
随机推荐
- EasyUi DataGrid 请求Url两次问题
easyui datagrid 1.4 当total为0时,请求两次url问题 框架问题:需要在easyui文件后加修补补丁 /** * The Patch for jQuery EasyUI 1.4 ...
- ZOJ 1081 Points Within | 判断点在多边形内
题目: 给个n个点的多边形,n个点按顺序给出,给个点m,判断m在不在多边形内部 题解: 网上有两种方法,这里写一种:射线法 大体的思想是:以这个点为端点,做一条平行与x轴的射线(代码中射线指向x轴正方 ...
- TCP面试题之四次挥手过程
TCP四次挥手过程: 1.第一次挥手:Client发送一个FIN,用来关闭Client到Server的数据传送,Client进入FIN_WAIT_1状态: 2.第二次挥手:Server收到FIN后,发 ...
- 图表绘制工具--Matplotlib 3
''' [课程3.] 表格样式创建 表格视觉样式:Dataframe.style → 返回pandas.Styler对象的属性,具有格式化和显示Dataframe的有用方法 样式创建: ① Style ...
- 【Apache Nutch系列】Nutch2.0配置安装异常集锦
1.java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/HBaseConfiguration Exception in thread &qu ...
- (原创)Linux下MySQL 5.5/5.6的修改字符集编码为UTF8(彻底解决中文乱码问题)
« CloudStack+XenServer详细部署方案(10):高级网络功能应用 (总结)CentOS Linux 5.x在GPT分区不能引导的解决方法 » 2013-1 11 (原创)Linux下 ...
- AGC019-E Shuffle and Swap
给定两个长度为\(n\le 10^5\)的\(01\)串 \(A, B\), 满足 \(1\) 的数量相等 求通过下列方式将\(A\)变成\(B\)的概率 (mod意义下) 构造序列\(a,b\). ...
- A simple greedy problem(hdu 4976)
题意:有n个小兵,每个小兵有a[i]血量,第一个人每次只能对一个小兵砍一滴血,第二个人每次对所有生存的小兵砍一滴血. 最后看第一个人最多可以砍杀几个小兵. /* 首先,如果所有小兵的血量都不同的话,我 ...
- log4net配置,正在用
<?xml version="1.0" encoding="utf-8" ?> <log4net> <appender name= ...
- gdb 记录临时变量
gdb ./pgm set logging file log set logging on ... set logging off gdb ./pgm | tee -a log ... file a. ...