Acwing---886. Find Combination Number II (Java)_Mathematics Knowledge_Quick Power Finding Inverse Element_Preprocessing Combination Formula

Original title link

①. Title

Insert picture description here

②. Thinking

Insert picture description here

时间复杂度分析

The time complexity of this question is O(n), n is the number of questions in the question
Insert picture description here

③. Learning points

快速幂求逆元阶乘

④. Code implementation

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    
    
	static int N=100010,p=(int)1e9+7;
	static int[] fact=new int[N],infact=new int[N];
	
	public static void main(String[] args) throws Exception {
    
    
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		fact[0] = infact[0] = 1;  //初始化
		for (int i = 1; i <N; i++) {
    
    
			fact[i]=(int)((long)fact[i-1]*i%p);  //递归求i的阶乘
			infact[i]=(int)((long)infact[i-1]*qmi(i,p-2,p)%p); //费马小定理求i的逆元阶乘
		}
		while(t-->0) {
    
    
			String[] s = br.readLine().split(" ");
			int a=Integer.parseInt(s[0]);
			int b=Integer.parseInt(s[1]);
			//直接套用组合数模板求解
			int res=(int)((long)fact[a]*infact[a-b]%p*infact[b]%p);
			System.out.println(res);
		}
	}
	//快速幂预处理 a^k%p  将k拆成二进制
	static int qmi(int a,int k,int p) {
    
    
		long res=1;
		while(k>0) {
    
    
			if((k&1)!=0)res=res*a%p;
			a=(int)((long)a*a%p);
			k>>=1;
		}
		return (int)res;
	}

}

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45480785/article/details/113896599