結果
| 問題 |
No.1105 Many Triplets
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
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])