結果

問題 No.2883 K-powered Sum of Fibonacci
ユーザー hiro1729hiro1729
提出日時 2024-09-07 15:50:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 68 ms / 3,000 ms
コード長 1,449 bytes
コンパイル時間 575 ms
コンパイル使用メモリ 82,400 KB
実行使用メモリ 73,652 KB
最終ジャッジ日時 2024-09-07 15:51:02
合計ジャッジ時間 4,630 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import comb

# a + b √5

mod = 998244353

class Q_v5:
	def __init__(self, a, b):
		self.a = a % mod
		self.b = b % mod
	def __add__(self, other):
		return self.__class__((self.a + other.a) % mod, (self.b + other.b) % mod)
	def __sub__(self, other):
		return self.__class__((self.a - other.a) % mod, (self.b - other.b) % mod)
	def __mul__(self, other):
		return self.__class__((self.a * other.a + self.b * other.b * 5) % mod, (self.a * other.b + self.b * other.a) % mod)
	def __truediv__(self, other):
		other_inv = pow(other.a * other.a - 5 * other.b * other.b, mod - 2, mod)
		return self.__class__((self.a * other.a - 5 * self.b * other.b) * other_inv % mod, (other.a * self.b - self.a * other.b) * other_inv % mod)
	def __neg__(self):
		return self.__class__(-self.a % mod, -self.b % mod)
	def __str__(self):
		return str(self.a) + ' + ' + str(self.b) + ' √5'

# 繰り返し二乗法
def pow_(a: Q_v5, n: int):
	res = Q_v5(1, 0)
	now = a
	for i in range(100):
		if n & (1 << i):
			res = res * now
		now = now * now
	return res

N, K = map(int, input().split())

sm = Q_v5(0, 0)
inv2 = 499122177
a = Q_v5(inv2, inv2)
b = Q_v5(inv2, -inv2)
for i in range(K + 1):
	p = comb(K, i) * (1 if i % 2 == 0 else -1) % mod
	x = pow_(a, K - i) * pow_(b, i)
	# x^1 + ... + x^N
	if x.a == 1 and x.b == 0:
		sm = sm + Q_v5(p * N, 0)
	else:
		sm = sm + Q_v5(p, 0) * (pow_(x, N + 1) - x) / (x - Q_v5(1, 0))
sm = sm / pow_(Q_v5(0, 1), K)
print(sm.a)
0