結果

問題 No.1704 Many Bus Stops (easy)
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-10-10 17:10:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,394 ms / 2,000 ms
コード長 3,264 bytes
コンパイル時間 1,829 ms
コンパイル使用メモリ 86,640 KB
実行使用メモリ 86,212 KB
最終ジャッジ日時 2023-10-12 13:40:28
合計ジャッジ時間 46,421 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 196 ms
82,200 KB
testcase_01 AC 1,332 ms
84,976 KB
testcase_02 AC 679 ms
84,720 KB
testcase_03 AC 685 ms
85,976 KB
testcase_04 AC 677 ms
84,736 KB
testcase_05 AC 682 ms
84,232 KB
testcase_06 AC 683 ms
85,056 KB
testcase_07 AC 675 ms
85,132 KB
testcase_08 AC 691 ms
84,464 KB
testcase_09 AC 680 ms
84,872 KB
testcase_10 AC 680 ms
85,208 KB
testcase_11 AC 681 ms
84,720 KB
testcase_12 AC 682 ms
85,328 KB
testcase_13 AC 678 ms
85,404 KB
testcase_14 AC 669 ms
85,348 KB
testcase_15 AC 681 ms
85,368 KB
testcase_16 AC 679 ms
86,212 KB
testcase_17 AC 677 ms
83,976 KB
testcase_18 AC 669 ms
84,948 KB
testcase_19 AC 682 ms
84,008 KB
testcase_20 AC 681 ms
85,356 KB
testcase_21 AC 681 ms
85,064 KB
testcase_22 AC 1,358 ms
84,056 KB
testcase_23 AC 1,361 ms
84,592 KB
testcase_24 AC 1,366 ms
84,700 KB
testcase_25 AC 1,376 ms
84,092 KB
testcase_26 AC 1,359 ms
84,716 KB
testcase_27 AC 1,359 ms
84,148 KB
testcase_28 AC 1,371 ms
84,936 KB
testcase_29 AC 1,367 ms
84,944 KB
testcase_30 AC 1,386 ms
84,884 KB
testcase_31 AC 1,370 ms
84,120 KB
testcase_32 AC 1,366 ms
85,128 KB
testcase_33 AC 1,376 ms
84,392 KB
testcase_34 AC 1,376 ms
84,072 KB
testcase_35 AC 1,368 ms
84,132 KB
testcase_36 AC 1,359 ms
84,492 KB
testcase_37 AC 1,374 ms
84,840 KB
testcase_38 AC 1,369 ms
84,028 KB
testcase_39 AC 1,362 ms
84,252 KB
testcase_40 AC 1,364 ms
84,448 KB
testcase_41 AC 1,394 ms
84,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import typing

# 拡張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

# 行列ライブラリ(遅い)
class Matrix:
	def __init__(self, n: int, m: int, mat: typing.Union[list, None] = None, mod: int = 10 ** 9 + 7) -> 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

mod = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	a = Matrix(6, 6, [[1, 0, 0, 0, 1, 1], [0, 1, 0, 1, 0, 1], [0, 0, 1, 1, 1, 0], [3, 0, 0, 0, 0, 0], [0, 3, 0, 0, 0, 0], [0, 0, 3, 0, 0, 0]], mod)
	a **= n
	a *= Matrix(6, 1, [[1], [0], [0], [3], [0], [0]])
	ans = a[3][0]
	print(ans * pow(invmod(3, mod), n + 1, mod) % mod)
0