結果

問題 No.1706 Many Bus Stops (hard)
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-10-10 17:46:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 3,170 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 11,352 KB
実行使用メモリ 10,216 KB
最終ジャッジ日時 2023-10-12 14:30:37
合計ジャッジ時間 4,215 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,124 KB
testcase_01 AC 32 ms
10,164 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import typing

class Matrix:
	def __init__(self, n: int, m: int, mat: typing.Union[list, None] = None, mod: int = 998244353) -> None:
		self.n = n
		self.m = m
		self.mat = [[0] * self.m for i in range(self.n)]
		self.mod = mod
		if mat:
			for i in range(self.n):
				self.mat[i] = mat[i]
	
	def is_square(self) -> None:
		return self.n == self.m
	
	def __getitem__(self, key: int) -> int:
		if isinstance(key, slice):
			return self.mat[key]
		else:
			assert key >= 0
			return self.mat[key]

	def id(n: int):
		res = Matrix(n, n)
		for i in range(n):
			res[i][i] = 1
		return res

	def __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.mod
		return Matrix(self.n, self.m, res)

	def __pos__(self):
		return self

	def __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.mod
		return 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.mod
		return 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.mod
			return 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 *= tmp
			tmp *= tmp
			k >>= 1
		return res

	def determinant(self):
		res = 1
		tmp  = 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: break
				else:
					return 0
				tmp.mat[j], tmp.mat[i] = tmp.mat[i], tmp.mat[j]
				res *= -1
			inv = invmod(tmp[j][j], self.mod)
			for i in range(j + 1, self.n):
				c = -inv * tmp[i][j] % self.mod
				for k in range(self.n):
					tmp[i][k] += c * tmp[j][k]
					tmp[i][k] %= self.mod
		for i in range(self.n):
			res *= tmp[i][i]
			res %= self.mod
		return res
# 拡張Euclidの互除法
def extgcd(a: int, b: int, d: int = 0) -> typing.Tuple[int, int, int]:
	g = a
	if b == 0:
		x, y = 1, 0
	else:
		x, y, g = extgcd(b, a % b)
		x, y = y, x - a // b * y
	return x, y, g
 
# mod p における逆元
def invmod(a: int, p: int) -> int:
	x, y, g = extgcd(a, p)
	x %= p
	return x

mod = 10 ** 9 + 7
c, 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 **= n
a *= Matrix(4, 1, [[1], [0], [c], [0]], mod)
ans = (1 - pow(1 - a[2][0] * pow(invmod(c, mod), n + 1, mod), m, mod)) % mod
print(ans)
0