zoukankan      html  css  js  c++  java
  • LUCAS 定理

    原来一张图就就能证明:C(N,M)%P,p是素数。

    简直太炫酷

    先膜拜会

    #include<iostream>
    #include<cstdio>
    #include<ctime>
    #include<cstring>
    #include<cstdlib>
    #include<vector>
    #define C 240
    #define TIME 10
    #define LL long long
    using namespace std;
    LL PowMod(LL a,LL b,LL MOD){
    LL ret=1;
    while(b){
    if(b&1) ret=(ret*a)%MOD;
    a=(a*a)%MOD;
    b>>=1;
    }
    return ret;
    }
    LL fac[100005];
    LL Get_Fact(LL p){
    fac[0]=1;
    for(int i=1;i<=p;i++)
    fac[i]=(fac[i-1]*i)%p;
    }
    LL Lucas(LL n,LL m,LL p){
    LL ret=1;
    while(n&&m){
    LL a=n%p,b=m%p;
    if(a<b) return 0;
    ret=(ret*fac[a]*PowMod(fac[b]*fac[a-b]%p,p-2,p))%p;
    n/=p;
    m/=p;
    }
    return ret;
    }
    int main(){
    int t;
    scanf("%d",&t);
    while(t--){
    LL n,m,p;
    scanf("%I64d%I64d%I64d",&n,&m,&p);
    Get_Fact(p);
    printf("%I64d ",Lucas(n+m,m,p));
    }
    return 0;
    }

     模板

    http://blog.csdn.net/acdreamers/article/details/8220787 解释逆元的解法

  • 相关阅读:
    IDEA快捷的添加包名
    Spring源码(一)
    Java的理解
    剪绳子
    机器人的运动范围
    矩阵中的路径
    N皇后问题
    回溯法
    全排列
    反转链表
  • 原文地址:https://www.cnblogs.com/forgot93/p/4190856.html
Copyright © 2011-2022 走看看