题目链接: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 ;
}

ACM-ICPC 2015 Changchun Preliminary Contest J. Unknown Treasure (卢卡斯定理+中国剩余定理)的更多相关文章

  1. ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków

    ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik’s Rect ...

  2. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering

    Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...

  3. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time

    Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...

  4. ACM-ICPC 2015 Shenyang Preliminary Contest B. Best Solver

    The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart. ...

  5. 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.. ...

  6. hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

    Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K ...

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

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

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

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

  9. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 I. Illegal or Not?

    I. Illegal or Not? time limit per test 1 second memory limit per test 512 megabytes input standard i ...

随机推荐

  1. git pull失误提交

    git pull 提示错误,Your local changes to the following files would be overwritten by merge 到公司后本来打算git pu ...

  2. leetcode-mid-Linked list- 230 Kth Smallest Element in a BST

    mycode  81.40% # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x ...

  3. 安装fedora23后的一些杂项设置

    Boxes是创建虚拟机的技术 tweak: 拧, 捏; 微调 he gave the boy's ear a painful tweak. it's a small tweak over the ra ...

  4. Python selenium 三种等待方式详解(必会)

    很多人在群里问,这个下拉框定位不到.那个弹出框定位不到…各种定位不到,其实大多数情况下就是两种问题:1 有frame,2 没有加等待.殊不知,你的代码运行速度是什么量级的,而浏览器加载渲染速度又是什么 ...

  5. 【转】最全的 pip 使用指南,50% 你可能没用过

    [转]最全的 pip 使用指南,50% 你可能没用过 所有的 Python 开发者都清楚,Python 之所以如此受欢迎,能够在众多高级语言中,脱颖而出,除了语法简单,上手容易之外,更多还要归功于 P ...

  6. Delphi XE2 之 FireMonkey 入门(27) - 数据绑定: TBindingsList: TBindScope

    Delphi XE2 之 FireMonkey 入门(27) - 数据绑定: TBindingsList: TBindScope 如果在编写表达式时, 如果能够随意指认需要的控件就好了(通过 Owne ...

  7. write()与writelines()

    f = open('user','a+') f.write('abcde')   #write只能写字符串 f.writelines(['444','rrrr','uuu'])  #writeline ...

  8. 2019了,给自己立一个flag吧

    新年伊始,元旦已过,虽然有迟了,但是,相对于整年来说,还是比较早.年度总结,年度规划,除过上交的报告以外,还得自己给自己立个flag,一次来督促自己,而不是为了别的.做这些事,不仅仅是为了能更好的工作 ...

  9. python实现建立websocket通信

    实现代码如下: #websocket协议通信 import threading import time import websocket def when_message(ws, message): ...

  10. 《Selenium 2自动化测试实战 基于Python语言》中发送最新邮件无内容问题的解决方法

    虫师的<Selenium 2自动化测试实战 基于Python语言>是我自动化测试的启蒙书 也是我推荐的自动化测试入门必备书,但是书中有一处明显的错误,会误导很多读者,这处错误就是第8章自动 ...