Skip to content

Commit c1d7c57

Browse files
committed
solved(python): baekjoon 13977
1 parent 3b720c1 commit c1d7c57

4 files changed

Lines changed: 95 additions & 0 deletions

File tree

baekjoon/python/13977/__init__.py

Whitespace-only changes.

baekjoon/python/13977/main.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sys
2+
3+
read = lambda: sys.stdin.readline().rstrip()
4+
5+
6+
class Problem:
7+
def __init__(self):
8+
self.n = int(read())
9+
self.data = [tuple(map(int, read().split())) for _ in range(self.n)]
10+
11+
self.mod = 1_000_000_007
12+
13+
def solve(self) -> None:
14+
factorial, inverse_factorial = self.precompute_factorial()
15+
16+
for n, r in self.data:
17+
print(self.comb(factorial, inverse_factorial, n, r))
18+
19+
def precompute_factorial(self) -> tuple[list[int], list[int]]:
20+
maximum = max(n for n, _ in self.data)
21+
factorial, inverse_factorial = [1] * (maximum + 1), [1] * (maximum + 1)
22+
23+
for idx in range(1, maximum + 1):
24+
factorial[idx] = factorial[idx - 1] * idx % self.mod
25+
26+
inverse_factorial[maximum] = pow(factorial[maximum], self.mod - 2, self.mod)
27+
for idx in range(maximum - 1, -1, -1):
28+
inverse_factorial[idx] = inverse_factorial[idx + 1] * (idx + 1) % self.mod
29+
30+
return factorial, inverse_factorial
31+
32+
def comb(self, factorial: list[int], inverse_factorial: list[int], n: int, r: int) -> int:
33+
if r < 0 or r > n:
34+
return 0
35+
36+
return factorial[n] * inverse_factorial[r] % self.mod * inverse_factorial[n - r] % self.mod
37+
38+
39+
if __name__ == "__main__":
40+
Problem().solve()

baekjoon/python/13977/sample.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"input": [
4+
"5",
5+
"5 2",
6+
"5 3",
7+
"10 5",
8+
"20 10",
9+
"10 0"
10+
],
11+
"expected": [
12+
"10",
13+
"10",
14+
"252",
15+
"184756",
16+
"1"
17+
]
18+
}
19+
]

baekjoon/python/13977/test_main.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
import os.path
3+
import unittest
4+
from io import StringIO
5+
from unittest.mock import patch
6+
7+
from parameterized import parameterized
8+
9+
from main import Problem
10+
11+
12+
def load_sample(filename: str):
13+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
14+
15+
with open(path, "r") as file:
16+
return [(case["input"], case["expected"]) for case in json.load(file)]
17+
18+
19+
class TestCase(unittest.TestCase):
20+
@parameterized.expand(load_sample("sample.json"))
21+
def test_case(self, case: str, expected: list[str]):
22+
# When
23+
with (
24+
patch("sys.stdin.readline", side_effect=case),
25+
patch("sys.stdout", new_callable=StringIO) as output,
26+
):
27+
Problem().solve()
28+
29+
result = output.getvalue().rstrip()
30+
31+
# Then
32+
self.assertEqual("\n".join(expected), result)
33+
34+
35+
if __name__ == "__main__":
36+
unittest.main()

0 commit comments

Comments
 (0)