結果
問題 | No.1706 Many Bus Stops (hard) |
ユーザー |
|
提出日時 | 2021-10-10 17:50:18 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 46 ms / 2,000 ms |
コード長 | 3,198 bytes |
コンパイル時間 | 94 ms |
コンパイル使用メモリ | 13,056 KB |
実行使用メモリ | 11,904 KB |
最終ジャッジ日時 | 2024-09-14 13:27:35 |
合計ジャッジ時間 | 3,124 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 41 |
ソースコード
import typingclass Matrix:def __init__(self, n: int, m: int, mat: typing.Union[list, None] = None, mod: int = 10 ** 9 + 7) -> None:self.n = nself.m = mself.mat = [[0] * self.m for i in range(self.n)]self.mod = modif mat:for i in range(self.n):self.mat[i] = mat[i]def is_square(self) -> None:return self.n == self.mdef __getitem__(self, key: int) -> int:if isinstance(key, slice):return self.mat[key]else:assert key >= 0return self.mat[key]def id(n: int):res = Matrix(n, n)for i in range(n):res[i][i] = 1return resdef __len__(self) -> int:return len(self.mat)def __str__(self) -> str:return "\n".join(" ".join(map(str, self[i])) for i in range(self.n))def times(self, k: int):res = [[0] * self.m for i in range(self.n)]for i in range(self.n):for j in range(self.m):res[i][j] = k * self[i][j] % self.modreturn Matrix(self.n, self.m, res)def __pos__(self):return selfdef __neg__(self):return self.times(-1)def __add__(self, other):res = [[0] * self.m for i in range(self.n)]for i in range(self.n):for j in range(self.m):res[i][j] = (self[i][j] + other[i][j]) % self.modreturn Matrix(self.n, self.m, res)def __sub__(self, other):res = [[0] * self.m for i in range(self.n)]for i in range(self.n):for j in range(self.m):res[i][j] = (self[i][j] - other[i][j]) % self.modreturn Matrix(self.n, self.m, res)def __mul__(self, other):if other.__class__ == Matrix:res = [[0] * other.m for i in range(self.n)]for i in range(self.n):for k in range(self.m):for j in range(other.m):res[i][j] += self[i][k] * other[k][j]res[i][j] %= self.modreturn Matrix(self.n, other.m, res)else:return self.times(other)def __rmul__(self, other):return self.times(other)def __pow__(self, k):tmp = Matrix(self.n, self.n, self.mat)res = Matrix.id(self.n)while k:if k & 1:res *= tmptmp *= tmpk >>= 1return resdef determinant(self):res = 1tmp = Matrix(self.n, self.n, self.mat)for j in range(self.n):if tmp[j][j] == 0:for i in range(j + 1, self.n):if tmp[i][j] != 0: breakelse:return 0tmp.mat[j], tmp.mat[i] = tmp.mat[i], tmp.mat[j]res *= -1inv = invmod(tmp[j][j], self.mod)for i in range(j + 1, self.n):c = -inv * tmp[i][j] % self.modfor k in range(self.n):tmp[i][k] += c * tmp[j][k]tmp[i][k] %= self.modfor i in range(self.n):res *= tmp[i][i]res %= self.modreturn res# 拡張Euclidの互除法def extgcd(a: int, b: int, d: int = 0) -> typing.Tuple[int, int, int]:g = aif b == 0:x, y = 1, 0else:x, y, g = extgcd(b, a % b)x, y = y, x - a // b * yreturn x, y, g# mod p における逆元def invmod(a: int, p: int) -> int:x, y, g = extgcd(a, p)x %= preturn xmod = 10 ** 9 + 7c, n, m = map(int, input().split())a = Matrix(4, 4, [[1, 0, 0, 1], [0, 1, c - 1, c - 2], [c, 0, 0, 0], [0, c, 0, 0]], mod)a **= na *= Matrix(4, 1, [[1], [0], [c], [0]], mod)x = a[2][0] * pow(invmod(c, mod), n + 1, mod)x = (1 - x) % modx = pow(x, m, mod)x = (1 - x) % modprint(x)