第二节,数据范围扩大。
采用逆元优化,快速幂优化。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
static int N = 10005, mod = (int) (1e9 + 7);
static int fact[] = new int[N], infact[] = new int[N];
public static void main(String[] args) throws IOException {
fact[0] = infact[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = (int) ((long) fact[i - 1] * i % mod);
infact[i] = (int) ((long) infact[i - 1] * qmi(i, mod - 2, mod) % mod);
}
String[] s = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
for (int i = 0; i < n; i++) {
s = br.readLine().split(" ");
int a = Integer.parseInt(s[0]);
int b = Integer.parseInt(s[1]);
pw.println((long) fact[a] % mod * infact[b] % mod * infact[a - b] % mod);
}
pw.flush();
br.close();
}
private static int qmi(int a, int k, int mod) {
int res = 1;
while (k != 0) {
if ((k & 1) != 0) res = (int) ((long) res * a % mod);
a = (int) ((long) a * a % mod);
k >>= 1;
}
return res;
}
}