ACM-ICPC 2015 Changchun Preliminary Contest J. Unknown Treasure (卢卡斯定理+中国剩余定理)
2024-08-30 07:15:33
题目链接:https://nanti.jisuanke.com/t/A1842
题目大意:给定整数n,m,k,其中1≤m≤n≤1018,k≤10,
然后给出k个素数,保证M=p[1]*p[2]……*p[k]≤1018,p[i]≤105
求C(n,m)%(p[1]*p[2]……*p[k])
解题思路:因为模数太大,所以我们先用卢卡斯定理求出对每个素数的模,然后再通过中国剩余定理就可以求得对它们的乘积的模。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll M,n,m,k,a[],b[];
ll qmul(ll a,ll b,ll p){
ll res=;
while(b){
if(b&) res=(res+a)%p;
b>>=;
a=(a+a)%p;
}
return res;
}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){
if(!b){
x=,y=,d=a;
}else{
exgcd(b,a%b,y,x,d);
y-=a/b*x;
}
}
ll INV(ll a,ll p){
ll x,y,d;
exgcd(a,p,x,y,d);
return (x%p+p)%p;
}
ll C(ll a,ll b,ll p){
if(a<b)return ;
if(b==)return ;
if(a-b<b)b=a-b;
ll ca=,cb=;
for(int i=;i<b;i++){
ca=ca*(a-i)%p;
cb=cb*(b-i)%p;
}
return ca*INV(cb,p)%p;
}
ll lucas(ll a,ll b,ll p){
ll res=;
while(a&&b){
res=res*C(a%p,b%p,p)%p; //C(n,m)%p=C(n%p,m%p)*C(n/p,m/p)%p
a/=p;
b/=p;
}
return res;
}
ll crt(){
ll x,y,d,res=;
for(int i=;i<=k;i++){
ll Mi=M/b[i];
exgcd(Mi,b[i],x,y,d);
x=(x%b[i]+b[i])%b[i];
ll tmp=qmul(a[i],qmul(Mi,x,M),M);
res=(res+tmp)%M;
}
return (res%M+M)%M;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%lld%lld%lld",&n,&m,&k);
M=;
for(int i=;i<=k;i++){
scanf("%lld",&b[i]);
a[i]=lucas(n,m,b[i]);
M*=b[i];
}
printf("%lld\n",crt());
}
return ;
}
最新文章
- 删除Windows中隐藏的物理网卡和网络虚拟化失败后的虚拟网卡
- Java面试题整理
- 你用java的swing可以做出这么炫的mp3播放器吗?
- css权重是什么
- java经典小算法
- XSS攻击及防御(转)
- 服务器调用JS
- 怎么让LinearLayout充满ScrollView
- NOSQL Mongo入门学习笔记 - 数据的基本插入(二)
- google guava 基本工具
- asp.net 操作INI文件的读写,读写操作本地ini配置文件
- 再探java基础——break和continue的用法
- IOS开发-OC学习-Foundation框架练习
- PostgreSQL数据库web维护客户端工具软件
- css变化代码
- python3 动态import
- 关于Mysql6.0+的时区错乱问题
- spring 2.5.6 错误:Context namespace element &#39;component-scan&#39; and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher
- webDriver.Close() 和 webDriver.Quit() 、webDriver.Dispose() 的区别
- linux -- ubuntuserver 安装Apache后,修改默认目录和分布式配置文件可执行
热门文章
- Eclipse中jar包的导出与导入
- C++ 对象间通信框架 V2.0 &#215;&#215;&#215;&#215;&#215;&#215;&#215; 之(三)
- 3D Computer Grapihcs Using OpenGL - 08 Text File Shaders
- POJ 1380 Equipment Box (暴力枚举)
- ES6 二进制和八进制字面量
- ODB Examples
- commons-collections包中的常用的工具类
- 线性回归 r python 比较
- Django学习之路由系统
- 014-elasticsearch5.4.3【五】-搜索API【三】复合查询boolQuery、constantScoreQuery、disMaxQuery