結果
| 問題 | No.314 ケンケンパ |
| コンテスト | |
| ユーザー |
mkawa2
|
| 提出日時 | 2020-02-08 00:36:31 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 33 ms / 1,000 ms |
| コード長 | 2,285 bytes |
| コンパイル時間 | 248 ms |
| コンパイル使用メモリ | 12,928 KB |
| 実行使用メモリ | 11,008 KB |
| 最終ジャッジ日時 | 2024-09-25 08:29:29 |
| 合計ジャッジ時間 | 1,532 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 |
ソースコード
class mint:
def __init__(self, x):
self.__x = x % md
def __str__(self):
return str(self.__x)
def __add__(self, other):
if isinstance(other, mint): other = other.__x
return mint(self.__x + other)
def __sub__(self, other):
if isinstance(other, mint): other = other.__x
return mint(self.__x - other)
def __rsub__(self, other):
return mint(other - self.__x)
def __mul__(self, other):
if isinstance(other, mint): other = other.__x
return mint(self.__x * other)
__radd__ = __add__
__rmul__ = __mul__
def __truediv__(self, other):
if isinstance(other, mint): other = other.__x
return mint(self.__x * pow(other, md - 2, md))
def __pow__(self, power, modulo=None):
return mint(pow(self.__x, power, md))
class Fibonacci:
def __init__(self):
coff = [1,1,0]
self.f0 = [1,2,2]
# 上2つは問題ごとに手作業で設定
# af(n)+bf(n+1)+cf(n+2)+df(n+3)=f(n+4)みたいなとき
# coff=[a,b,c,d]
# 初期値f0(f(0)からf(3))
n = len(coff)
ff = [[0] * n for _ in range(2 * n - 1)]
for i in range(n): ff[i][i] = mint(1)
for i in range(n, 2 * n - 1):
ffi = ff[i]
for j, c in enumerate(coff, i - n):
ffj = ff[j]
for k in range(n): ffi[k] += c * ffj[k]
self.bn = 1 << (n - 1).bit_length()
self.base = ff[self.bn]
self.ff = ff
self.n = n
def __mm(self, aa, bb):
n = self.n
res = [0] * (n * 2 - 1)
for i, a in enumerate(aa):
for j, b in enumerate(bb):
res[i + j] += a * b
for i in range(n, 2 * n - 1):
c = res[i]
ffi = self.ff[i]
for j in range(n):
res[j] += c * ffi[j]
return res[:n]
def v(self, x):
base = self.base
aa = self.ff[x % self.bn]
x //= self.bn
while x:
if x & 1: aa = self.__mm(aa, base)
base = self.__mm(base, base)
x >>= 1
return sum(a * f for a, f in zip(aa, self.f0))
md = 10**9+7
def main():
n=int(input())
f=Fibonacci()
print(f.v(n-1))
main()
mkawa2