結果
問題 | No.1105 Many Triplets |
ユーザー | titan23 |
提出日時 | 2022-12-08 11:20:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 74 ms / 2,000 ms |
コード長 | 4,623 bytes |
コンパイル時間 | 265 ms |
コンパイル使用メモリ | 82,192 KB |
実行使用メモリ | 72,556 KB |
最終ジャッジ日時 | 2024-10-14 10:09:01 |
合計ジャッジ時間 | 3,405 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 67 ms
69,220 KB |
testcase_01 | AC | 68 ms
69,344 KB |
testcase_02 | AC | 66 ms
70,436 KB |
testcase_03 | AC | 67 ms
69,172 KB |
testcase_04 | AC | 65 ms
69,232 KB |
testcase_05 | AC | 66 ms
69,732 KB |
testcase_06 | AC | 66 ms
69,708 KB |
testcase_07 | AC | 65 ms
69,240 KB |
testcase_08 | AC | 67 ms
69,304 KB |
testcase_09 | AC | 66 ms
69,152 KB |
testcase_10 | AC | 72 ms
71,760 KB |
testcase_11 | AC | 72 ms
71,644 KB |
testcase_12 | AC | 72 ms
71,836 KB |
testcase_13 | AC | 71 ms
71,924 KB |
testcase_14 | AC | 71 ms
72,112 KB |
testcase_15 | AC | 70 ms
72,308 KB |
testcase_16 | AC | 73 ms
72,160 KB |
testcase_17 | AC | 72 ms
71,776 KB |
testcase_18 | AC | 74 ms
72,556 KB |
testcase_19 | AC | 72 ms
71,716 KB |
testcase_20 | AC | 64 ms
70,024 KB |
testcase_21 | AC | 65 ms
69,028 KB |
testcase_22 | AC | 64 ms
69,344 KB |
testcase_23 | AC | 65 ms
68,548 KB |
testcase_24 | AC | 63 ms
68,424 KB |
testcase_25 | AC | 64 ms
68,752 KB |
testcase_26 | AC | 65 ms
68,520 KB |
testcase_27 | AC | 64 ms
68,824 KB |
ソースコード
from typing import List, Union class Matrix: mod = 998244353 mod = 10**9+7 def __init__(self, a: List[List[int]]) -> None: self.n = len(a) self.m = len(a[0]) if self.n > 0 else 0 self.a = a def __add__(self, other: Union[int, "Matrix"]) -> "Matrix": if isinstance(other, int): return Matrix([[(sij+other)%Matrix.mod for sij in si] for si, oi in self.a]) elif isinstance(other, Matrix): assert self.n == other.n and self.m == other.m return Matrix([[(sij+oij)%Matrix.mod for sij, oij in zip(si, oi)] for si, oi in zip(self.a, other.a)]) else: raise TypeError def __sub__(self, other: Union[int, "Matrix"]) -> "Matrix": if isinstance(other, int): return Matrix([[(sij-other)%Matrix.mod for sij in si] for si, oi in self.a]) elif isinstance(other, Matrix): assert self.n == other.n and self.m == other.m return Matrix([[(sij+oij)%Matrix.mod for sij, oij in zip(si, oi)] for si, oi in zip(self.a, other.a)]) else: raise TypeError def __mul__(self, other: Union[int, "Matrix"]) -> "Matrix": if isinstance(other, int): new = [a[:] for a in self.a] for i in range(self.n): for j in range(self.m): new[i][j] *= other new[i][j] %= Matrix.mod return Matrix(new) elif isinstance(other, Matrix): assert self.n == other.n and self.m == other.m new = [a[:] for a in self.a] for i in range(self.n): for j in range(self.m): new[i][j] *= other.a[i][j] new[i][j] %= Matrix.mod return Matrix(new) else: raise TypeError def __matmul__(self, other: "Matrix") -> "Matrix": if isinstance(other, Matrix): assert self.m == other.n return Matrix([[sum(map(lambda x, y: x*y[j]%Matrix.mod, ai, other.a))%Matrix.mod for j in range(other.m)] for ai in self.a]) else: raise TypeError def __pow__(self, n: int) -> "Matrix": assert isinstance(n, int) and self.n == self.m res = [[0]*self.m for _ in range(self.n)] for i in range(self.n): res[i][i] = 1 res = Matrix(res) a = Matrix([a[:] for a in self.a]) while n > 0: if n & 1 == 1: res @= a a @= a n >>= 1 return res def __radd__(self, other: Union[int, "Matrix"]) -> "Matrix": return self.__add__(other) def __rsub__(self, other: Union[int, "Matrix"]) -> "Matrix": if isinstance(other, int): return Matrix([[(other-sij)%Matrix.mod for sij in si] for si, oi in self.a]) elif isinstance(other, Matrix): assert self.n == other.n and self.m == other.m return Matrix([[(oij-sij)%Matrix.mod for sij, oij in zip(si, oi)] for si, oi in zip(self.a, other.a)]) else: raise TypeError def __rmul__(self, other: Union[int, "Matrix"]) -> "Matrix": return self.__mul__(other) def __iadd__(self, other: Union[int, "Matrix"]) -> "Matrix": if isinstance(other, int): for i in range(self.n): for j in range(self.m): self.a[i][j] += other self.a[i][j] %= Matrix.mod elif isinstance(other, Matrix): assert self.n == other.n and self.m == other.m for i in range(self.n): for j in range(self.m): self.a[i][j] += other.a[i][j] self.a[i][j] %= Matrix.mod else: raise TypeError return self def __isub__(self, other: Union[int, "Matrix"]) -> "Matrix": if isinstance(other, int): for i in range(self.n): for j in range(self.m): self.a[i][j] -= other self.a[i][j] %= Matrix.mod elif isinstance(other, Matrix): assert self.n == other.n and self.m == other.m for i in range(self.n): for j in range(self.m): self.a[i][j] -= other.a[i][j] self.a[i][j] %= Matrix.mod else: raise TypeError return self def __imul__(self, other: Union[int, "Matrix"]) -> "Matrix": if isinstance(other, int): for i in range(self.n): for j in range(self.m): self.a[i][j] *= other self.a[i][j] %= Matrix.mod elif isinstance(other, Matrix): assert self.n == other.n and self.m == other.m for i in range(self.n): for j in range(self.m): self.a[i][j] *= other.a[i][j] self.a[i][j] %= Matrix.mod else: raise TypeError return self def __getitem__(self, k): return self.a[k] def __str__(self): return str(self.a) A = Matrix([[1,-1,0],[0,1,-1],[-1,0,1]]) n = int(input()) a, b, c = map(int, input().split()) res = A ** (n-1) @ Matrix([[a],[b],[c]]) print(res[0][0], res[1][0], res[2][0])