zoukankan      html  css  js  c++  java
  • FZU 2020 :组合 【lucas】

    Problem Description

    给出组合数C(n,m), 表示从n个元素中选出m个元素的方案数。例如C(5,2) = 10, C(4,2) = 6.可是当n,m比较大的时候,C(n,m)很大!于是xiaobo希望你输出 C(n,m) mod p的值!

    思路:水题,练一下lucas

    #include<iostream>
    #include<cstdio>
    #include <math.h>
    #include<algorithm>
    #include<string.h>
    #include<queue>
    #define MOD 1000003
    #define maxn 2009
    #define LL long long
    using namespace std;
    LL mpow(LL a,LL n,LL p)
    {
            if(n==0)return 1;
            if(n==1)return a%p;
            if(n&1)return (a*mpow(a,n-1,p))%p;
            else
            {
                    LL u=mpow(a,n>>1,p)%p;
                    return (u*u)%p;
            }
    }
    LL C(LL n,LL m,LL p)
    {
            if(m==0)return 1;
            if(m>n-m)m=n-m;
            LL up=1,down=1;
            for(int i=1;i<=m;i++){
                    up=(up*(n-i+1))%p;
                    down=(down*i)%p;
            }
            return up*mpow(down,p-2,p)%p;
    }
    long long lucas(long long n,long long m,long long p)
    {
            if(m==0)return 1;
            return C(n%p,m%p,p)*lucas(n/p,m/p,p);
    }
    int main()
    {
            long long m,n,p;
            int t;
            scanf("%d",&t);
            while(t--)
            {
                    scanf("%I64d%I64d%I64d",&n,&m,&p);
                    printf("%I64d ",lucas(n,m,p));
            }
            return 0;
    }

  • 相关阅读:
    iOS中静态库的制作——使用创建静态库项目的方式创建静态库
    在Xcode中自定义代码块——快速代码
    在博客园的第一篇博客
    委派
    As I Began to Love Myself
    When you are old
    RESOURCE SUMMARY
    First One
    js 动态生成div显示id
    js 层的显示和隐藏
  • 原文地址:https://www.cnblogs.com/philippica/p/4278110.html
Copyright © 2011-2022 走看看