lucas求组合数C(n,k)%p
Saving Beans http://acm.hdu.edu.cn/showproblem.php?pid=3037
#include<cstdio>
typedef __int64 LL;
const int M=;
class LUCAS { //lucas求组合数C(n,k)%p
LL F[M];
LL inv(LL a,LL mod) {
if(a==) return ;
return inv(mod%a,mod)*(mod-mod/a)%mod;
}
void init(LL p) {
F[]=;
for(int i=; i<=p; i++) {
F[i]=F[i-]*i%p;
}
}
LL Lucas(LL n,LL k,LL p) {
LL ans=;
while(n&&k) {
LL a=n%p;
LL b=k%p;
if(a<b) return ;
ans=ans*F[a]%p*inv(F[b]*F[a-b]%p,p)%p;
n/=p;
k/=p;
}
return ans;
}
public:
LL solve(LL n,LL k,LL p){
init(p);
return Lucas(n,k,p);
}
}gx;
int main() {
int t,n,m,p;
while(~scanf("%d",&t)) {
while(t--) {
scanf("%d%d%d",&n,&m,&p);
printf("%d\n",(int)gx.solve(n+m,n,p));
}
}
return ;
}
DP? http://acm.hdu.edu.cn/showproblem.php?pid=3944
#include<cstdio>
#include<cstring>
#include<vector>
#define mt(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef __int64 LL;
const int M=;
class LUCASM { //比较适合n,k<=10^9,p<=10^4的情况
int flag[M*],prime[],pcnt;
vector<int> rev[M],fac[M];
int quickpow(int a,int b,int c) { //快速幂求(a^b)%c
int ret=%c;
for(; b; a=a*a%c,b>>=) {
if(b&) {
ret=ret*a%c;
}
}
return ret;
}
public:
void init() {//先初始化一次即可
pcnt=;
mt(flag,-);
for(int i=; i<=; i++) {
if(flag[i]) {
prime[pcnt++]=i;
rev[i].clear();
fac[i].clear();
}
for(int j=; j<pcnt&&prime[j]<=/i; j++) {
flag[i*prime[j]]=;
if(!(i%prime[j])) break;
}
}
for(int i=; i<pcnt; i++) {
int tnum=;
rev[prime[i]].push_back();
fac[prime[i]].push_back();
for (int j=; j<prime[i]; j++) {
tnum=(tnum*j)%prime[i];
int now=quickpow(tnum,prime[i]-,prime[i]);
fac[prime[i]].push_back(tnum);
rev[prime[i]].push_back(now);
}
}
}
int lucas(int n,int k,int p) {
int ret=;
while (n && k) {
int num1=n%p;
int num2=k%p;
n/=p;
k/=p;
if (num1<num2) return ;
int num=(fac[p][num1]*rev[p][num2])%p;//计算c(num1,num2)%p
num=(num*rev[p][num1-num2])%p;
ret=(ret*num)%p;
}
return ret;
}
} gx;
int main() {
int n,k,p,cas=;
gx.init();
while(~scanf("%d%d%d",&n,&k,&p)) {
printf("Case #%d: ",cas++);
if(k>n/) k=n-k;
int o=gx.lucas(n+,k,p);
printf("%d\n",(n-k+o)%p);
}
return ;
}
end
lucas求组合数C(n,k)%p的更多相关文章
- 1067 - Combinations---LightOj(Lucas求组合数)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1067 模板求C(n,m)%p, Lucas模板; #include <iostr ...
- 求组合数 C++程序
一 递归求组合数 设函数为void comb(int m,int k)为找出从自然数1.2.... .m中任取k个数的所有组合. 分析:当组合的第一个数字选定时,其后的数字是从余下的m-1个数中 ...
- HDU 5698——瞬间移动——————【逆元求组合数】
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...
- URAL 1994 The Emperor's plan 求组合数 大数用log+exp处理
URAL 1994 The Emperor's plan 求组合数 大数用log #include<functional> #include<algorithm> #inclu ...
- HDU 5852 Intersection is not allowed!(LGV定理行列式求组合数)题解
题意:有K个棋子在一个大小为N×N的棋盘.一开始,它们都在棋盘的顶端,它们起始的位置是 (1,a1),(1,a2),...,(1,ak) ,它们的目的地是 (n,b1),(n,b2),...,(n,b ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】
任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...
- 51nod1119(除法取模/费马小定理求组合数)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1119 题意:中文题诶- 思路:这题数据比较大直接暴力肯定是不 ...
- [2011山东ACM省赛] Binomial Coeffcients(求组合数)
Binomial Coeffcients nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...
随机推荐
- C#中Messagebox.Show()常用参数用法详解
声明:IWin32Window owner , HelpNavigator navigator , string keyword 上面的三个参数类型不是很了解.没有做讨论. 等以后了解多了 ...
- ASP.Net 类(CS)文件怎样获取Web应用程序的路径
Web应用程序,写了一个线程CS类别,这个类别将会放于Global.asax文件中执行,主要是监控程序下某一个文件是否有异动,而作出相应警示动作,如发送邮件等. 实现运行过程中,也许会有一个情况出现, ...
- AMQ学习笔记 - 17. 事务的测试
概述 对事务机制进行测试. 测试实例 测试实例 结果预测 发送正常 3条消息入队 发送异常 0条消息入队 接收正常 3条消息出队 接收异常 0条消息出队 demo设计 设计图 测试分工 测试类 测试方 ...
- (转)集成架构:对比 Web API 与面向服务的架构和企业应用程序集成
摘要:总体上讲,SOA 和 Web API 似乎解决的是同一个问题:以实时的.可重用的方式公开业务功能.本教程将分析这些举措有何不同,以及如何将它们融入到一个不断演变的集成架构中.文中还将讨论 API ...
- 超炫酷web前端的jQuery/HTML5应用搜罗
作为前端开发者,我们肯定都使用过非常多的jQuery插件,毋庸置疑,jQuery非常流行,尤其是结合HTML5和CSS3以后,让这些jQuery插件有了更多地动画效果,更为绚丽多彩. 1.HTML5/ ...
- Model Builder中Table2Table中字段映射的问题
ArcGIS10中使用过程中,Bug不少.尽管有了SP3,但模型耦合的深层次的应用中还是错误不少.目前只是遇到一个,利用躲避的方法解决一个.例如,从NetCDF中抽出的数据表,必须在内存和数据库中都存 ...
- CentOS 7.2 无法生成 coredump文件
CentOS版本 cat /etc/centos-release CentOS Linux release 7.2.1511 (Core) 设置ulimit -c ulimited 依旧无法生成co ...
- 《linux 网卡别名的添加和绑定》RHEL6
网卡别名的配置: 这个和ifconfig临时修改网卡ip 差不多,但是不一样.都是临时的,只要重启电脑就没了. 配永久的ip别名: cp ifcfg-eth0 ifcfg-eth0:0 vim if ...
- nginx总结
kill int 2333 进程号 停止程序 kiil quit 2322 优雅停止服务 kill -HUP 2333 优雅重启 从新读取配置文件 kill -HUP 'cat logs/n ...
- 了解jsonp
<script> //创建全局函数,用来处理 跨域 获取到的信息: function name(data){ .... ...