题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2982

明明是lucas定理裸题……

非常需要注意C( )里  if ( n<m ) return 0; !!!!!

可以预处理阶乘和其逆元,也可以现求。现求阶乘逆元的话,可以把 jc[m] 和 jc[n-m] 乘起来再放到pw里。

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const int mod=;
int T,n,m,jc[mod+],ans;
int pw(int x,int k)
{
int ret=;while(k){if(k&)(ret*=x)%=mod;x=(ll)x*x%mod;k>>=;}return ret;
}
void init()
{
jc[]=;
for(int i=;i<mod;i++)jc[i]=jc[i-]*i%mod;
}
int C(int n,int m)
{
if(n<m)return ;//
return (ll)jc[n]*pw(jc[m]*jc[n-m],mod-)%mod;//jc[m]*jc[n-m]一起求逆元
}
int lucas(int n,int m)
{
if(!m)return ;
if(n<mod&&m<mod)return C(n,m);
return lucas(n/mod,m/mod)*C(n%mod,m%mod)%mod;
}
int main()
{
init();
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
printf("%d\n",lucas(n,m));
}
return ;
}

现求阶乘逆元

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const int mod=;
ll n,m;
int T,jc[mod+],jcn[mod+],ans;
int pw(int x,int k)
{
int ret=;while(k){if(k&)(ret*=x)%=mod;(x*=x)%=mod;k>>=;}return ret;
}
void init()
{
jc[]=;
for(int i=;i<mod;i++)jc[i]=jc[i-]*i%mod;
jcn[mod-]=pw(jc[mod-],mod-);
for(int i=mod-;i>=;i--)jcn[i]=jcn[i+]*(i+)%mod;
}
int C(int n,int m)
{
if(n<m)return ;////
return jc[n]*jcn[m]%mod*jcn[n-m]%mod;
}
int lucas(ll n,ll m)
{
if(!m)return ;
if(n<mod&&m<mod)return C(n,m);
return lucas(n/mod,m/mod)*C(n%mod,m%mod)%mod;
}
int main()
{
init();
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&n,&m);
printf("%d\n",lucas(n,m));
}
return ;
}

bzoj 2982 combination——lucas模板的更多相关文章

  1. BZOJ 2982: combination Lucas模板题

    Code: #include<bits/stdc++.h> #define ll long long #define maxn 1000003 using namespace std; c ...

  2. 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 ...

  3. BZOJ 2982: combination( lucas )

    lucas裸题. C(m,n) = C(m/p,n/p)*C(m%p,n%p). ----------------------------------------------------------- ...

  4. 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 ...

  5. BZOJ2982: combination Lucas模板

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

  6. bzoj——2982: combination

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

  7. BZOJ 2982 combination

    lucas定理裸题. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...

  8. BZOJ 2982 combination 脑子+组合数学

    可以发现,整个数列构成一个树形结构,并且是个完全二叉堆(小根堆). 并且这个堆的形态在给定$n$后是固定的,第1个位置上显然只能放1. 对子树的根来说,他自己是所分得的数集中最小的那个,所以从剩下$s ...

  9. 【BZOJ 2982】 2982: combination (卢卡斯定理)

    2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 510  Solved: 316 Description LMZ有n个 ...

随机推荐

  1. UnsatisfiedLinkError X.so is 64-bit instead of 32-bit之Android 64 bit SO加载机制

    http://blog.csdn.net/canney_chen/article/details/50633982 今天用户反馈应用闪退崩溃了.然后找呀找… 过程原来是这样的: 还是说下项目背景 应用 ...

  2. linux下安装casperjs 开发运行环境

    casperjs是一个基于phantomjs的测试框架,使用python进行操作,所以一个完整的casperjs环境需要安装phantomjs和python. 1 phantomjs安装 到官网下载最 ...

  3. SQLite3时间函数小结

    import sqlite3 conn = sqlite3.connect('/tmp/sqlite.db') cur = conn.cursor() 接下来干嘛呢?建一张表吧.这里需要注意的是,SQ ...

  4. python安装包

    1. python常用包下载地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy numpy包下载地址:https://pypi.python.org ...

  5. maven install 找不到自定义jar包

    解决方案是在Prefernces-->Maven-->user Settings 里面点击Update settings 然后就好了.因为默认你配置settings.xml指向你的类库的时 ...

  6. RHEL7 LAMP

    准备篇: RHEL 7.0系统安装配置图解教程:http://www.osyunwei.com/archives/7702.html 一.使用系统镜像文件配置本地yum源 1.使用WinSCP.exe ...

  7. awk之腾迅面试题1

    1.题目如下: 3 5 6 72 3 1 04 5 6 92 3 4 42 2 1 04 5 0 9假如把2列和3列的值作为新的第5列,第5列的平均值为avg5,求第5列中大于avg5的行数.  aw ...

  8. Eclipse4.2安装样式插件

    1.插件地址 http://eclipse-color-theme.github.com/update 点击Eclipse菜单 Help>>Install New Software... ...

  9. different between web api and web service

     https://stackoverflow.com/questions/19336347/what-is-the-difference-between-a-web-api-and-a-web-ser ...

  10. jQuery颜色面板插件

    /** * jQuery颜色面板插件 * * 使用方法:input框的id默认为inputObj,button框的id默认为btnObj,也可以自定义aaa,bbb * 1.初始化颜色面板:$.col ...