• Lucas定理模板


    用于大组合数对p取模的计算。

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cmath>
     4 #include <cstring>
     5 #include <algorithm>
     6 using namespace std;
     7 #define maxn 100010
     8 typedef long long LL;
     9 
    10 LL m,n,p;
    11 LL Pow(LL a,LL b,LL mod)
    12 {
    13     LL ans=1;
    14     while(b)
    15     {
    16         if(b&1)
    17         {
    18             b--;
    19             ans=(ans*a)%mod;
    20         }
    21         b>>=1;
    22         a=(a*a)%mod;
    23     }
    24     return ans;
    25 }
    26 LL C(LL n,LL m)
    27 {
    28     if(n<m) return 0;
    29     if(m == 0) return 1;
    30     if(m < 0) return 0;
    31     LL ans=1;
    32     for(int i=1;i<=m;i++)
    33     {
    34         ans=ans*(((n-m+i)%p)*Pow(i,p-2,p)%p)%p; //除以一个数的话是乘以其逆元,用到费马小定理
    35     }
    36     return ans;
    37 }
    38 LL Lucas(LL n,LL m)
    39 {
    40     if(m==0)
    41         return 1;
    42     return (Lucas(n/p,m/p)*C(n%p,m%p))%p;
    43 }
    44 int main()
    45 {
    46     while(cin>>n>>m){
    47         p=1000000007;
    48         LL l=n+m-4,r=n-2;
    49         printf("%I64d
    ",Lucas(l,r)); //计算C(l, r)
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    非常可乐
    Find The Multiple
    盲点集锦
    Fliptile
    Catch That Cow
    Dungeon Master
    hdoj 1045 Fire Net
    hdoj 1342 Lotto【dfs】
    zoj 2100 Seeding
    poj 3620 Avoid The Lakes【简单dfs】
  • 原文地址:https://www.cnblogs.com/fightfordream/p/5827825.html
Copyright © 2020-2023  润新知