結果

問題 No.1105 Many Triplets
ユーザー titan23titan23
提出日時 2022-12-08 11:20:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 4,623 bytes
コンパイル時間 590 ms
コンパイル使用メモリ 82,488 KB
実行使用メモリ 73,156 KB
最終ジャッジ日時 2024-04-22 10:53:09
合計ジャッジ時間 3,660 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
70,008 KB
testcase_01 AC 66 ms
70,076 KB
testcase_02 AC 65 ms
69,892 KB
testcase_03 AC 66 ms
69,072 KB
testcase_04 AC 66 ms
69,856 KB
testcase_05 AC 64 ms
69,136 KB
testcase_06 AC 66 ms
68,704 KB
testcase_07 AC 65 ms
68,220 KB
testcase_08 AC 68 ms
69,632 KB
testcase_09 AC 65 ms
69,712 KB
testcase_10 AC 71 ms
72,304 KB
testcase_11 AC 71 ms
72,712 KB
testcase_12 AC 72 ms
71,968 KB
testcase_13 AC 71 ms
71,736 KB
testcase_14 AC 71 ms
72,920 KB
testcase_15 AC 70 ms
72,016 KB
testcase_16 AC 72 ms
73,156 KB
testcase_17 AC 73 ms
72,164 KB
testcase_18 AC 71 ms
72,812 KB
testcase_19 AC 71 ms
72,368 KB
testcase_20 AC 66 ms
69,308 KB
testcase_21 AC 66 ms
68,296 KB
testcase_22 AC 65 ms
68,596 KB
testcase_23 AC 65 ms
68,560 KB
testcase_24 AC 65 ms
69,040 KB
testcase_25 AC 64 ms
68,724 KB
testcase_26 AC 65 ms
69,224 KB
testcase_27 AC 65 ms
69,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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])
0