【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=5446

【题目大意】

  给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M。

【题解】

  首先我们可以用Lucas定理求出对答案对每个质因子的模,然后我们发现只要求解这个同余方程组就可以得到答案,所以我们用中国剩余定理解决剩下的问题。

【代码】

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N=100005;
LL f[N],rf[N];
LL mul(LL x,LL y,LL P){return (x*y-(LL)(x/(long double)P*y+1e-3)*P+P)%P;}
LL pow(LL a,LL b,LL P){LL t=1;for(;b;b>>=1,a=mul(a,a,P))if(b&1)t=mul(t,a,P);return t;}
void init(int n){
f[0]=1;for(int i=1;i<n;i++)f[i]=f[i-1]*i%n;
rf[n-1]=pow(f[n-1],n-2,n);
for(int i=n-1;i;i--)rf[i-1]=rf[i]*i%n;
}
LL C(int n,int m,int mod){
if(m>n||m<0||n<0)return 0;
return f[n]*rf[m]%mod*rf[n-m]%mod;
}
LL lucas(LL n,LL m,LL P){
if(n<m)return 0;
if(!m||n==m)return 1;
return C(n%P,m%P,P)*lucas(n/P,m/P,P)%P;
}
LL exgcd(LL a,LL b,LL& x,LL& y){
if(b==0){x=1;y=0;return a;}
LL d=exgcd(b,a%b,y,x);
y-=x*(a/b);
return d;
}
LL CRT(LL* a,LL* b,int n){
LL P=1,d,y,x=0;
for(int i=0;i<n;i++)P*=a[i];
for(int i=0;i<n;i++){
LL w=P/a[i];
exgcd(a[i],w,d,y); y=(y%P+P)%P;
x=(x+mul(mul(y,w,P),b[i],P));
}return (x+P)%P;
}int T,k;
LL n,m,a[15],p[15];
int main(){
scanf("%d",&T);
while(T--){
scanf("%lld%lld%d",&n,&m,&k);
for(int i=0;i<k;i++){
scanf("%lld",&p[i]);
init(p[i]);
a[i]=lucas(n,m,p[i]);
}printf("%lld\n",CRT(p,a,k));
}return 0;
}

HDU 5446 Unknown Treasure(Lucas定理+CRT)的更多相关文章

  1. hdu 5446 Unknown Treasure lucas和CRT

    Unknown Treasure Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  2. hdu 5446 Unknown Treasure Lucas定理+中国剩余定理

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  3. HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘

    HDU 5446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k])     0< n,m < 1018 思路:这题基本上算是模版题了 ...

  4. HDU 5446 Unknown Treasure Lucas+中国剩余定理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...

  5. HDU 5446 Unknown Treasure(lucas + 中国剩余定理 + 模拟乘法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, ...

  6. Hdu 5446 Unknown Treasure (2015 ACM/ICPC Asia Regional Changchun Online Lucas定理 + 中国剩余定理)

    题目链接: Hdu 5446 Unknown Treasure 题目描述: 就是有n个苹果,要选出来m个,问有多少种选法?还有k个素数,p1,p2,p3,...pk,结果对lcm(p1,p2,p3.. ...

  7. HDU 5446——Unknown Treasure——————【CRT+lucas+exgcd+快速乘+递推求逆元】

    Each test case starts with three integers n,m,k(1≤m≤n≤1018,1≤k≤10) on a line where k is the number o ...

  8. hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  9. HDU 5446 Unknown Treasure

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

随机推荐

  1. JVM学习之对象的状态

    堆中存放着几乎所有的对象实例,垃圾收集器在堆堆进行回收前,首先要确定这些对象哪些还“活着”,哪些已经“死去”.方法有如下两种: (1)引用计数法 算法思想:为对象添加一个引用计数器,每当有一个地方引用 ...

  2. DB2 相关操作

      查看数据库版本:db2level DB21061E  Command line environment not initialized. windows命令行下,需要先调用db2cmd,或者DB2 ...

  3. UDP聊天实现(简单版)

    两者代码基本一致 关于套接字: 甲方: #include <stdio.h> #include <string.h> #include <netinet/in.h> ...

  4. 查看ORACLE中正在运行的存储过程 kill

    1:登陆PLSQL Developer,写一个存储过程,向一个表中插入值,并运行存储过程 2:打开PLSQL Developer的命令窗口 .--终止procedure   11.select * f ...

  5. maintenance ShellScripts

    1.Linux挂载Winodws共享文件夹 2.查看http的并发请求数及其TCP连接状态: 3.用tcpdump嗅探80端口的访问看看谁最高 4.统计/var/log/下文件个数 5.查看当前系统每 ...

  6. sharepoint 2013 列表和库标签 元数据导航配置(2)

    接前面提到的,如何创建一个术语库.sharepoint 2013 列表和库标签 元数据导航配置(1), 现在要做的,就是如何在自定义或者文档库中使用这个术语库,实现标签功能,通过这些标签,找到对应的文 ...

  7. Trailing Zeroes (III)(lightoj 二分好题)

    1138 - Trailing Zeroes (III)   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit:  ...

  8. AS3事件机制概述

    事件机制是AS3的核心功能之一,没有充分掌握事件机制的方方面面,就不能算是精通AS3语言. 1. AS3事件机制的主要成员 IEventDispatcher:事件派发对象接口,定义了添加.派发.移除. ...

  9. Brew install for mac

    安装命令例如以下: curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz -C/usr/local --st ...

  10. spm3 基本

    spm3 命令 spm init //初始化一个spm模块,会生成基本配置以及测试文件等(下图). //注 初始化以后一般需要 鲜执行一下 spm install 安装默认依赖模块 index.js就 ...