ACM-ICPC 2015 Changchun Preliminary Contest J. Unknown Treasure (卢卡斯定理+中国剩余定理)

题目链接: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[15],b[15];
ll qmul(ll a,ll b,ll p){
    ll res=0;
    while(b){
        if(b&1) res=(res+a)%p;
        b>>=1;
        a=(a+a)%p;
    }
    return res;
}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){
    if(!b){
        x=1,y=0,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 0;
    if(b==0)return 1;
    if(a-b<b)b=a-b;
    ll ca=1,cb=1;
    for(int i=0;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=1;
    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=0;
    for(int i=1;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=1;
        for(int i=1;i<=k;i++){
            scanf("%lld",&b[i]);
            a[i]=lucas(n,m,b[i]);
            M*=b[i];
        }
        printf("%lld\n",crt());
    }
    return 0;
}

原文地址:https://www.cnblogs.com/zjl192628928/p/11252130.html

时间: 2024-10-03 05:17:30

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

hdu 5446 Unknown Treasure (Lucas定理+中国剩余定理+快速乘)

题意:c( n, m)%M    M = P1 * P2 * ......* Pk (其中Pk是素数) 思路:Lucas定理中C(n,m)%M,M必须是素数,当M不是素数时,我们可以把它拆成素数的乘积 如果x=C(n,m)%M ,M=p1*p2*..*pk;  a[i]=Lucas(n,m)%pi: xΞa[1](mod p1) xΞa[2](mod p2) ... xΞa[k](mod pk) 用中国剩余定理就可以把x求出来 注意到这道题ll*ll 由于计算机底层设计的原因,做加法往往比乘法快

Hdu 5446 Unknown Treasure(Lucas+中国剩余定理)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5446 思路:Lucas求出所有a[i]=C(n,m)%m[i],中国剩余定理求出最终结果x (LL*LL会爆掉,手写乘法). 中国剩余定理: 设m1,m2,....mn是两两互质的正整数,对任意给定的整数a1,a2,....an必存在整数,满足 x≡a1 (mod m1),x≡a2 (mod m2),x≡a3 (mod m3)...... 并且满足上列方程组的解x(mod m1m2m3.....mn

ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Krak&#243;w

ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik's RectangleProblem B: What does the fox say?Problem C: Magical GCDProblem D: SubwayProblem E: EscapeProblem F: DraughtsProblem G: History courseProblem H: C

HDU 5437 &amp; ICPC 2015 Changchun Alisha&#39;s Party(优先队列)

Alisha’s Party Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 7971    Accepted Submission(s): 1833 Description Princess Alisha invites her friends to come to her birthday party. Each of her f

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. It is known that y=(5+2 *sqrt(6))^(1+2^x) For a given integer x(0≤x<2^32) and a given prime number M(M≤46337), print [y]%M. ([y]means the integer part

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 (Java/Others)Total Submission(s): 208    Accepted Submission(s): 101 Problem Description You may not know this but it's a fact that Xinghai Square is

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

Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown to the map. The mathematician

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

Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2209    Accepted Submission(s): 821 Problem Description On the way to the next secret treasure hiding place, the mathematician

ICPC 2015 Changchun A Too Rich(贪心)

问题 A: Too Rich 时间限制: 1 Sec  内存限制: 128 MB 题目描述 You are a rich person, and you think your wallet is too heavy and full now. So you want to give me some money by buying a lovely pusheen sticker which costs p dollars from me. To make your wallet lighter,