結果
| 問題 |
No.980 Fibonacci Convolution Hard
|
| コンテスト | |
| ユーザー |
Eki1009
|
| 提出日時 | 2020-11-01 18:19:00 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,360 bytes |
| コンパイル時間 | 152 ms |
| コンパイル使用メモリ | 82,344 KB |
| 実行使用メモリ | 98,832 KB |
| 最終ジャッジ日時 | 2024-07-22 05:47:38 |
| 合計ジャッジ時間 | 7,277 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | TLE * 1 -- * 16 |
ソースコード
import sys
sys.setrecursionlimit(10**7)
mod = 10**9+7
def convolution(A, B):
la = len(A)
lb = len(B)
res = [0]*(la+lb-1)
for i, a in enumerate(A):
for j, b in enumerate(B):
res[i+j] += a*b
res[i+j] %= mod
return res
def poly_mod(X, Q):
a = len(X)
b = len(Q)
if a < b:
return X
for i in range(a-b, -1, -1):
d = X[i+b-1]
for j, q in enumerate(Q):
X[i+j] -= q*d
X[i+j] %= mod
return X[:b-1]
def poly_pow(C, Q, n):
if n == 1:
return C
d = len(C)
if n%2:
res = convolution(poly_pow(C, Q, n-1), C)
else:
T = poly_pow(C, Q, n//2)
res = convolution(T, T)
res = poly_mod(res, Q)
return res
#a_{n} = c_{1}*a_{n-1} + ... + c_{d}*a_{n-d} の第n項を求める(0~d-1項はE_{i}とする)
def rec_fomula(C, E, n):
d = len(C)
Q = [1]*(d+1)
for i, c in enumerate(C, 1):
Q[i] = -c
P = convolution(Q[:-1], E)[:d]
inv = pow(Q[-1], mod-2, mod)
norm_Q = [q*inv%mod for q in Q]
X = poly_pow(C, norm_Q, n)
res = convolution(X, P)
res = poly_mod(res, norm_Q)
return res[0]
p = int(input())
C = [2*p, 2-p**2, -2*p, -1]
E = [0, 0, 0, 1]
q = int(input())
for _ in range(q):
n = int(input())
ans = rec_fomula(C, E, n-1)
print(ans)
Eki1009