zoukankan      html  css  js  c++  java
  • JDOJ 2430: 组合数取模

    JDOJ 2430: 组合数取模

    题目传送门

    Description

    Compute: N choose M mod 1000000007.

    (0≤M≤N≤2×106)

    Input

    每个文件有多组输入数据,数据组数≤50

    每组数据占一行,分别是两个正整数N,M

    Output

    每组测试数据输出一个结果。

    Sample Input

    5 1 5 2 0 0

    Sample Output

    5 10 1


    最优解声明:


    题解:

    直接按组合数通项求,快速幂求逆元即可AC。

    注意读入。

    #include<cstdio>
    #define int long long
    using namespace std;
    const int maxn=2*1e6+10;
    const int mod=1e9+7;
    int n,m;
    int fac[maxn];
    int qpow(int a,int b)
    {
        int ret=1;
        while(b)
        {
            if(b&1)
                ret=(ret*a)%mod;
            b>>=1;
            a=(a*a)%mod;
        }
        return ret%mod;
    }
    signed main()
    {
        fac[0]=1;fac[1]=1;
        for(int i=2;i<=maxn;i++)
            fac[i]=(fac[i-1]*i)%mod;
        while(scanf("%lld%lld",&n,&m)!=EOF)
        {
            int ans=((fac[n]*qpow(fac[m],mod-2))%mod*qpow(fac[n-m],mod-2))%mod;
            printf("%lld
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    Lucas 定理
    C语言II博客作业04
    C语言II博客作业03
    C语言II博客作业02
    C语言II博客作业01
    学期总结
    First project
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
  • 原文地址:https://www.cnblogs.com/fusiwei/p/13845162.html
Copyright © 2011-2022 走看看