数论——lucas定理
网上证明很多,虽然没看懂。。。。
主要解决大组合数取模的情况
费马小定理求大组合数:
a^(p-1)=1%p;
两边同除a
a^(p-2)=1/a%p;
C(n,m)= n!/(m!*(n-m)!)
所以C(n,m)=f(n)*qpow(f(m)*f(n-m),MOD-2))%MOD
预处理组合数f
百度之星2016 1003
先推公式,再lucas
p很大的情况 1e9+7
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define clc(a,b) memset(a,b,sizeof(a))
#include <bits/stdc++.h>
const int maxn = ;
const int inf=0x3f3f3f3f;
const double pi=acos(-);
typedef long long LL;
using namespace std;
const LL MOD = 1e9+; LL exp_mod(LL a, LL b, LL p)
{
LL res = ;
while(b != )
{
if(b&) res = (res * a) % p;
a = (a*a) % p;
b >>= ;
}
return res;
} LL Comb(LL a, LL b, LL p)
{
if(a < b) return ;
if(a == b) return ;
if(b > a - b) b = a - b; LL ans = , ca = , cb = ;
for(LL i = ; i < b; ++i)
{
ca = (ca * (a - i))%p;
cb = (cb * (b - i))%p;
}
ans = (ca*exp_mod(cb, p - , p)) % p;
return ans;
} LL Lucas(int n, int m, int p)
{
LL ans = ; while(n&&m&&ans)
{
ans = (ans*Comb(n%p, m%p, p)) % p;
n /= p;
m /= p;
}
return ans;
} int main()
{
int n, m;
LL p;
while(~scanf("%d%d", &n, &m))
{
p=MOD;
printf("%I64d\n", Lucas(n+m-, m-, p));
}
return ;
}
p在100000左右
HDU 3037
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define clc(a,b) memset(a,b,sizeof(a))
#include <bits/stdc++.h>
const int maxn = ;
const int inf=0x3f3f3f3f;
const double pi=acos(-);
typedef long long LL;
using namespace std;
//const LL MOD = 1e9+7; LL PowMod(LL a,LL b,LL MOD){
LL ret=;
while(b){
if(b&) ret=(ret*a)%MOD;
a=(a*a)%MOD;
b>>=;
}
return ret;
}
LL fac[];
LL Get_Fact(LL p){
fac[]=;
for(int i=;i<=p;i++)
fac[i]=(fac[i-]*i)%p;
}
LL Lucas(LL n,LL m,LL p){
LL ret=;
while(n&&m){
LL a=n%p,b=m%p;
if(a<b) return ;
ret=(ret*fac[a]*PowMod(fac[b]*fac[a-b]%p,p-,p))%p;
n/=p;
m/=p;
}
return ret;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
LL n,m,p;
scanf("%I64d%I64d%I64d",&n,&m,&p);
Get_Fact(p);
printf("%I64d\n",Lucas(n+m,m,p));
}
return ;
}
数论——lucas定理的更多相关文章
- CPC23-4-K. 喵喵的神数 (数论 Lucas定理)
喵喵的神∙数 Time Limit: 1 Sec Memory Limit: 128 MB Description 喵喵对组合数比較感兴趣,而且对计算组合数很在行. 同一时候为了追求有后宫的素养的生活 ...
- Bzoj 4591: [Shoi2015]超能粒子炮·改 数论,Lucas定理,排列组合
4591: [Shoi2015]超能粒子炮·改 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 178 Solved: 70[Submit][Stat ...
- 数论(Lucas定理) HDOJ 4349 Xiao Ming's Hope
题目传送门 题意:求C (n,0),C (n,1),C (n,2)...C (n,n)中奇数的个数 分析:Lucas 定理:A.B是非负整数,p是质数.AB写成p进制:A=a[n]a[n-1]...a ...
- 【BZOJ-4591】超能粒子炮·改 数论 + 组合数 + Lucas定理
4591: [Shoi2015]超能粒子炮·改 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 95 Solved: 33[Submit][Statu ...
- Bzoj 4403: 序列统计 Lucas定理,组合数学,数论
4403: 序列统计 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 328 Solved: 162[Submit][Status][Discuss] ...
- 古代猪文:数论大集合:欧拉定理,exgcd,china,逆元,Lucas定理应用
/* 古代猪文:Lucas定理+中国剩余定理 999911658=2*3*4679*35617 Lucas定理:(m,n)=(sp,tp)(r,q) %p 中国剩余定理:x=sum{si*Mi*ti} ...
- 【bzoj1951】: [Sdoi2010]古代猪文 数论-中国剩余定理-Lucas定理
[bzoj1951]: [Sdoi2010]古代猪文 因为999911659是个素数 欧拉定理得 然后指数上中国剩余定理 然后分别lucas定理就好了 注意G==P的时候的特判 /* http://w ...
- HDU 3037 Saving Beans (数论,Lucas定理)
题意:问用不超过 m 颗种子放到 n 棵树中,有多少种方法. 析:题意可以转化为 x1 + x2 + .. + xn = m,有多少种解,然后运用组合的知识就能得到答案就是 C(n+m, m). 然后 ...
- 『Lucas定理以及拓展Lucas』
Lucas定理 在『组合数学基础』中,我们已经提出了\(Lucas\)定理,并给出了\(Lucas\)定理的证明,本文仅将简单回顾,并给出代码. \(Lucas\)定理:当\(p\)为质数时,\(C_ ...
随机推荐
- window live writer的曲折安装过程
之前一直使用windows live writer2012写日志,由于之前重装了系统,所以需要重新安装writer,本以为是一个很简单的过程,你就是安装个软件吗.... 然而事实是... ...
- 你不需要jQuery(四)
jQuery是个好东西.它诞生于IE6在互联网称霸的那个时代.jQuery的存在让我们的代码能很好的兼容各种平台. 然而,到如今,浏览器技术已经取得了巨大的进步.我们可以自由的使用所有最新众多ES5/ ...
- PDF、WORD、PPT、TXT转换方法
- 团体程序设计天梯赛-练习集L1-017. 到底有多二
L1-017. 到底有多二 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一个整数“犯二的程度”定义为该数字中包含2的个数与其 ...
- 不定长内存池之apr_pool
内存池可有效降低动态申请内存的次数,减少与内核态的交互,提升系统性能,减少内存碎片,增加内存空间使用率,避免内存泄漏的可能性,这么多的优点,没有理由不在系统中使用该技术. 内存池分类: 1. ...
- HDU2110+母函数
/* 母函数(生成函数) 题意: 有n种资产,每种资产num份,每份有val的价值 问取出总价值的1/3有多少种方案 */ #include<stdio.h> #include<st ...
- HeadFirst设计模式之状态模式
一. 1. 2.The State Pattern allows an object to alter its behavior when its internal state changes. Th ...
- jQuery 的 $("someobjectid”).event() 的绑定
经验证,jquery 的 $("someobjectid”).event()事件绑定,如果放在某个会被重新初始化的对象里,就会被多次绑定. 如下 <div id="divID ...
- 214. Shortest Palindrome
题目: Given a string S, you are allowed to convert it to a palindrome by adding characters in front of ...
- C#创建XML文件并保存
随着XML的普及以及在动态WEB应用程序中大量应用,如何通过.NET创建,删除,修改XML文件变的也来也重要了.一个简单的概念是,XML文件跟大的文本文件并没有什么区别,同时它是先于.NET出现,很多 ...