1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#include <stdio.h>
#include <utility>
#define NMAX 4000001
#define MOD 1000000007
using namespace std;
typedef long long int lint;
typedef pair<lint, lint> PAIR;
int M, N, K;
lint ret;
lint fac[NMAX];
lint gcd(lint a, lint b) {
if(a == 0) return b;
else if(b == 0) return a;
else return gcd(b, a%b);
}
// kx = 1 (mod MOD)
// >> ax + by = b(a/bx + y) + (a%b)x = 1
PAIR inv(lint a, lint b) {
if(a == 0) return make_pair(0, 1);
else if(b == 0) return make_pair(1, 0);
else {
PAIR prev = inv(b, a%b);
lint x = ((prev.second%MOD)+MOD)%MOD;
lint y = prev.first - (a/b)*x;
return make_pair(x, y);
}
}
int main() {
// init
fac[0] = 1;
for(int i=1;i<NMAX;i++) fac[i] = (fac[i-1]*i)%MOD;
// input
scanf("%d", &M);
while(M--) {
scanf("%d %d", &N, &K);
// nCk = n! / (k! * (n-k)!)
ret = ((fac[N] * inv(fac[K], MOD).first)%MOD * inv(fac[N-K], MOD).first )%MOD;
// print
printf("%lld\n", ret);
}
}
|
cs |
참고
'Study' 카테고리의 다른 글
SCC 구하기 (0) | 2022.07.26 |
---|---|
람다식을 활용한 중첩 반복문 탈출( BOJ 20410 ) (0) | 2022.07.11 |
정렬 시간 비교 (0) | 2022.04.09 |
소수 판별법(밀러-라빈 소수판별법) (0) | 2022.02.09 |
순열 알고리즘 (0) | 2021.10.01 |