zoukankan      html  css  js  c++  java
  • HDU5446 Unknown Treasure(组合数膜合数-->Lucas+中国剩余定理)

    >On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown to the map. The mathematician entered the cave because it is there. Somewhere deep in the cave, she found a treasure chest with a combination lock and some numbers on it. After quite a research, the mathematician found out that the correct combination to the lock would be obtained by calculating how many ways are there to pick mm different apples among nn of them and modulo it with MM. MM is the product of several different primes.

    Input

    On the first line there is an integer T(T20)T(T≤20) representing the number of test cases. 

    Each test case starts with three integers n,m,k(1mn1018,1k10)n,m,k(1≤m≤n≤1018,1≤k≤10) on a line where kk is the number of primes. Following on the next line are kk different primes p1,...,pkp1,...,pk. It is guaranteed that M=p1p2pk1018M=p1·p2···pk≤1018 and pi105pi≤105 for every i{1,...,k}i∈{1,...,k}.
    OutputFor each test case output the correct combination on a line.Sample Input

    1
    9 5 2
    3 5

    Sample Output

    6

    题意:

    让你求出C(n,m)%M的值。

    思路:

    此题的 n和m非常大,因此不能用快速幂取模,这里我们只能用lucas定理,但lucas定理有一个条件,要求C(n,m)%M的M必须要为素数,因此,我们又要用到中国剩余定理。

     经验:

    • 按照这样的方法,现在大的组合数都可以化小了。
    • 注意long long范围,超范围时用快速乘法的方法做乘,欧拉算法时里有用过。即代码里的mul()函数。
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #define LL long long
    using namespace std;
    const int maxn=100010;
    LL fac[maxn],mod[maxn],odd[maxn],M,Mod;
    void factorial()
    {
        fac[0]=1;  for(int i=1;i<=Mod;i++) fac[i]=fac[i-1]*i%Mod;
    }
    LL f_pow(LL a,LL x)
    {
        LL res=1; a%=Mod;
        while(x){  if(x&1) res=res*a%Mod;a=a*a%Mod; x>>=1; }return res;
    }
    LL C(LL n,LL m)
    {
        if(m>n) return 0; return fac[n]*f_pow(fac[m]*fac[n-m]%Mod,Mod-2)%Mod;
    }
    LL Lucas(LL n,LL m)
    {
        if(m==0) return 1;  return C(n%Mod,m%Mod)*Lucas(n/Mod,m/Mod)%Mod; 
    }
    LL mul(LL x,LL y,LL p)
    {
        LL res=0;
        while(y){
            if(y&1) res=(res+x)%p;y>>=1;x=(x+x)%p;
        }return res%p;
    }
    void China(int k)
    {
        LL ans=0;
        for(int i=1;i<=k;i++){
            Mod=mod[i];
            ans=ans+mul(mul(M/mod[i],f_pow(M/mod[i],mod[i]-2),M),odd[i],M);
        }printf("%lld
    ",(ans+M)%M);
    }
    int main()
    {    
        LL T,n,m,k;
        scanf("%lld",&T);
        while(T--){
            M=1;
            scanf("%lld%lld%lld",&n,&m,&k);
            for(int i=1;i<=k;i++){
                scanf("%d",&mod[i]);Mod=mod[i];M*=mod[i];
                factorial();
                odd[i]=Lucas(n,m)%Mod;
            }
            China(k);
        }return 0;
    }

    再总结一下剩余定理

    设正整数两两互素,则同余方程组

                                 

    有整数解。并且在模下的解是唯一的,解为

                                   

    其中,而的逆元。

  • 相关阅读:
    iOS cannot find folder xcdatamodeld Xcode 7
    ios swift generator 文章推荐
    ios swift 2 新的OptionSetType使用方法
    Maven 教程(11)— Maven远程仓库的各种配置
    Maven 教程(10)— Maven依赖详解
    Maven 教程(9)— Maven坐标详解
    Maven设置MAVEN_OPTS环境变量
    Maven 教程(7)— Maven使用的最佳实践
    Maven 教程(6)— Maven之pom.xml文件简单说明
    Maven 教程(5)— Maven目录结构及常用命令说明
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8046602.html
Copyright © 2011-2022 走看看