結果

問題 No.1810 RGB Biscuits
ユーザー MasKoaTSMasKoaTS
提出日時 2022-11-29 23:09:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 380 ms / 2,000 ms
コード長 3,286 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 82,300 KB
実行使用メモリ 79,008 KB
最終ジャッジ日時 2024-04-16 13:36:52
合計ジャッジ時間 5,571 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
53,120 KB
testcase_01 AC 80 ms
71,168 KB
testcase_02 AC 49 ms
54,400 KB
testcase_03 AC 106 ms
76,288 KB
testcase_04 AC 114 ms
76,288 KB
testcase_05 AC 352 ms
78,848 KB
testcase_06 AC 380 ms
78,752 KB
testcase_07 AC 378 ms
78,572 KB
testcase_08 AC 367 ms
79,008 KB
testcase_09 AC 369 ms
78,584 KB
testcase_10 AC 294 ms
78,804 KB
testcase_11 AC 127 ms
77,056 KB
testcase_12 AC 116 ms
77,192 KB
testcase_13 AC 125 ms
77,440 KB
testcase_14 AC 206 ms
78,284 KB
testcase_15 AC 150 ms
77,568 KB
testcase_16 AC 184 ms
77,312 KB
testcase_17 AC 280 ms
78,636 KB
testcase_18 AC 280 ms
78,776 KB
testcase_19 AC 116 ms
77,056 KB
testcase_20 AC 51 ms
54,528 KB
testcase_21 AC 199 ms
78,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class ModInt:
	mod = 998244353

	def __init__(self, x):
		self.x = x % ModInt.mod

	@classmethod
	def set_mod(cls, mod: int) -> None:
		ModInt.mod = mod
		
	def __str__(self):
		return str(self.x)

	__repr__ = __str__

	def __add__(self, other):
		return (ModInt(self.x + other.x) if isinstance(other, ModInt) else ModInt(self.x + other))

	def __sub__(self, other):
		return (ModInt(self.x - other.x) if isinstance(other, ModInt) else ModInt(self.x - other))

	def __mul__(self, other):
		return (ModInt(self.x * other.x) if isinstance(other, ModInt) else ModInt(self.x * other))

	def __truediv__(self, other):
		return (ModInt(self.x * pow(other.x, ModInt.mod - 2, ModInt.mod)) if instance(other, ModInt)
		  else ModInt(self.x * pow(other, ModInt.mod - 2, ModInt.mod)))

	def __pow__(self, other):
		return ModInt(pow(self.x, other.x, ModInt.mod)) if isinstance(other, ModInt) else ModInt(pow(self.x, other, ModInt.mod))

	__radd__ = __add__

	def __rsub__(self, other):
		return (ModInt(other.x - self.x) if isinstance(other, ModInt) else ModInt(other - self.x))

	__rmul__ = __mul__

	def __rtruediv__(self, other):
		return (ModInt(other.x * pow(self.x, ModInt.mod - 2, ModInt.mod)) if instance(other, ModInt)
		  else ModInt(other * pow(self.x, ModInt.mod - 2, ModInt.mod)))

	def __rpow__(self, other):
		return ModInt(pow(other.x, self.x, ModInt.mod)) if instance(other, ModInt) else ModInt(pow(other, self.x, ModInt.mod))

	def __iadd__(self, other):
		self = self + other
		return self

	def __isub__(self, other):
		self = self - other
		return self

	def __imul__(self, other):
		self = self * other
		return self

	def __itruediv__(self, other):
		self = self / other
		return self
	
class Matrix:
	def __init__(self, A: list):
		if not(A and isinstance(A[0], list)):
			A = [A]
		self.A = A
		self.row = len(A)
		self.col = len(A[0])
		self.I = None

	def set_A(self, A: list) -> None:
		self.A = A

	def set_I(self, I) -> None:
		self.I = I

	def __str__(self):
		return str(self.A)

	__repr__ = __str__

	def __add__(self, other):
		ret = Matrix(self)
		for i in range(self.row):
			for j in range(self.col):
				ret.A[i][j] = self.A[i][j] + other.A[i][j]
		return ret

	def __sub__(self, other):
		ret = Matrix(self)
		for i in range(self.row):
			for j in range(self.col):
				ret.A[i][j] = self.A[i][j] - other.A[i][j]
		return ret

	def __mul__(self, other):
		assert(self.col == other.row)
		ret = Matrix([[0]*other.col for _ in [0]*self.row])
		for i in range(self.row):
			for j in range(other.col):
				ret.A[i][j] = sum(self.A[i][k] * other.A[k][j] for k in range(self.col))
		return ret

	def __pow__(self, other: int):
		mat = Matrix(self.A)
		ret = Matrix(self.I)
		n = other
		while(n):
			if(n & 1):
				ret = mat * ret
			mat = mat * mat
			n >>= 1
		return ret

"""
Main Code
"""

ModInt.set_mod(10 ** 9 + 7)

a, b = map(int, input().split())
n = int(input())
P = Matrix([[ModInt(a), ModInt(b)], [ModInt(1), ModInt(0)]])
P.set_I([[ModInt(1), ModInt(0)], [ModInt(0), ModInt(1)]])
v = Matrix([[ModInt(1)], [ModInt(1)]])

for t in [int(input()) for _ in [0]*n]:
	k = t >> 1
	r = t & 1
	v1 = (P ** k) * v
	v2 = P * v1
	if(r == 1):
		print(v1.A[0][0] + v1.A[1][0] + v2.A[0][0])
	else:
		print(v1.A[0][0] + v1.A[1][0])
0