Skip to content

Commit 8f67639

Browse files
committed
solved(python): baekjoon 11444
1 parent 9c7ef71 commit 8f67639

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

baekjoon/python/11444/__init__.py

Whitespace-only changes.

baekjoon/python/11444/main.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
3+
read = lambda: sys.stdin.readline().rstrip()
4+
5+
6+
class Problem:
7+
def __init__(self):
8+
self.num = int(read())
9+
self.divide = 1_000_000_007
10+
11+
def solve(self) -> None:
12+
if self.num == 0 or self.num == 1:
13+
print(self.num)
14+
return
15+
16+
num, matrix, result = self.num - 1, [[1, 1], [1, 0]], [[1, 0], [0, 1]]
17+
while num > 0:
18+
if num % 2 != 0:
19+
result = self.mat_mul(result, matrix)
20+
matrix = self.mat_mul(matrix, matrix)
21+
num //= 2
22+
23+
print(result[0][0])
24+
25+
def mat_mul(
26+
self,
27+
matrix_a: list[list[int]],
28+
matrix_b: list[list[int]],
29+
) -> list[list[int]]:
30+
return [
31+
[
32+
(matrix_a[0][0] * matrix_b[0][0] + matrix_a[0][1] * matrix_b[1][0])
33+
% self.divide,
34+
(matrix_a[0][0] * matrix_b[0][1] + matrix_a[0][1] * matrix_b[1][1])
35+
% self.divide,
36+
],
37+
[
38+
(matrix_a[1][0] * matrix_b[0][0] + matrix_a[1][1] * matrix_b[1][0])
39+
% self.divide,
40+
(matrix_a[1][0] * matrix_b[0][1] + matrix_a[1][1] * matrix_b[1][1])
41+
% self.divide,
42+
],
43+
]
44+
45+
46+
if __name__ == "__main__":
47+
Problem().solve()

baekjoon/python/11444/sample.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"input": [
4+
"1000"
5+
],
6+
"expected": [
7+
"517691607"
8+
]
9+
}
10+
]

baekjoon/python/11444/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().strip()
30+
31+
# Then
32+
self.assertEqual("\n".join(expected), result)
33+
34+
35+
if __name__ == "__main__":
36+
unittest.main()

0 commit comments

Comments
 (0)