首页 > 其他 > 详细

886. 求组合数 II(模板)

时间:2020-02-05 20:28:34      阅读:71      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

  数据范围较大, a,b都是1e5

  直接根据公式预处理

 技术分享图片

 

   技术分享图片 1/i就是求i的逆元(逆元求法:mod为质数,逆元就是 i^(mod-2)%mod )

   O(N*logN)

 

import java.util.Scanner;

public class Main{
        static final int N=100005;
        static final int mod=(int)1e9+7;
        static long fact[]=new long[N];
        static long infact[]=new long[N];
        static long quick_pow(long a,long b){
                long res=1;
                while(b>0){
                        if((b&1)==1) res=res*a%mod;
                        a=a*a%mod;
                        b>>=1;
                }
                return res%mod;
        }
        public static void main(String[] args) {
                Scanner scan=new Scanner(System.in);
                fact[0]=infact[0]=1;
                for(int i=1;i<N;i++){
                        fact[i]=fact[i-1]*i%mod;
                        infact[i]=infact[i-1]*quick_pow(i,mod-2)%mod;
                }
               int t=scan.nextInt();
               while(t-->0){
                           int a=scan.nextInt();
                           int b=scan.nextInt();
                           System.out.println(fact[a]*infact[a-b]%mod*infact[b]%mod);
               }
        }
}

 

 

   

886. 求组合数 II(模板)

原文:https://www.cnblogs.com/qdu-lkc/p/12264907.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!