原题链接
①. 题目
②. 思路
时间复杂度分析
③. 学习点
Lucas定理
④. 代码实现
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
long a,b,p;
while(n-->0) {
a=sc.nextLong();
b=sc.nextLong();
p=sc.nextLong();
System.out.println(lucas(a,b,p));
}
}
static long quickPower(long a,long b,long p) {
long res=1;
while(b>0) {
if((b&1)==1) {
res=res*a%p;
}
a=a*a%p;
b>>=1;
}
return res;
}
static long combination(long a,long b,long p) {
long res=1;
for (long i =1,j=a;i<=b;i++,j--) {
res=res*j%p;
res=res*quickPower(i, p-2, p)%p;
}
return res;
}
static long lucas(long a,long b,long p) {
if(a<p&&b<p) {
return combination(a, b, p);
}
return combination(a%p, b%p, p)*lucas(a/p, b/p, p)%p;
}
}