Skip to content

Commit 1519cd4

Browse files
committed
solved(python): baekjoon 12850
1 parent b100307 commit 1519cd4

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

baekjoon/python/12850/__init__.py

Whitespace-only changes.

baekjoon/python/12850/main.py

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

baekjoon/python/12850/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+
"100000000"
5+
],
6+
"expected": [
7+
"261245548"
8+
]
9+
}
10+
]

baekjoon/python/12850/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)