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; ...
随机推荐
- a href 相对路径 与绝对路径
<a href="/abc/">内容</a> <a href="abc/">内容</a>的区别,相对路径绝对路径 ...
- iOS中—触摸事件详解及使用
iOS中--触摸事件详解及使用 (一)初识 要想学好触摸事件,这第一部分的基础理论是必须要学会的,希望大家可以耐心看完. 1.基本概念: 触摸事件 是iOS事件中的一种事件类型,在iOS中按照事件划分 ...
- 【风马一族_Python】 决策树
<机器学习实战>第三章 决策树 ------------------------------------- #1 trees.py 计算给定数据集的香农熵 ---------------- ...
- SATA SAS SSD 硬盘介绍和评测
SATA SATA的全称是Serial Advanced Technology Attachment,是由Intel.IBM.Dell.APT.Maxtor和Seagate公司共同提出的硬盘接口规范. ...
- webpack入门(译)
本文由官方Tutorial Getting Started整理翻译,因为该指南解决了我在上手webpack过程中遇到的诸多问题.所以在这里推荐给各位新手们~ WELCOME 这份指南始终围绕一个简单例 ...
- [转]PHP中fopen,file_get_contents,curl的区别
1. fopen /file_get_contents 每次请求都会重新做DNS查询,并不对 DNS信息进行缓存.但是CURL会自动对DNS信息进行缓存.对同一域名下的网页或者图片的请求只 ...
- Android little error records
1:在清单文件(Manifest)中加入权限时要注意,如下例: <uses-permission android:name="android.permission.CHANGE_NET ...
- 如何重建Octopress本地环境
# 安装rvm, ruby, bundler 略 # 克隆octopress $ git clone git://github.com/imathis/octopress.git octopress ...
- Android Studio笔记(2)——快捷键
在朋友推荐下,上个星期黄老师我用上了Google的新黑暗工具,基于Intellij idea的新Android开发集成开发环境 ——Android Studio,用下来感觉还算不错,但作为一个ADT ...
- Linux挂载60T存储
操作系统: CentOS 6.3 存储:总大小为72T,并划分成3个块,每块20T 安装多实例MySQL数据库,不想挂载3个块,弄成一个大的比较方便管理,个人比较懒. 配置多路径:http://blo ...