結果
| 問題 |
No.194 フィボナッチ数列の理解(1)
|
| コンテスト | |
| ユーザー |
maspy
|
| 提出日時 | 2020-03-28 16:08:29 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 701 ms / 5,000 ms |
| コード長 | 2,306 bytes |
| コンパイル時間 | 409 ms |
| コンパイル使用メモリ | 12,288 KB |
| 実行使用メモリ | 70,116 KB |
| 最終ジャッジ日時 | 2025-01-02 11:40:08 |
| 合計ジャッジ時間 | 20,556 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
MOD = 10 ** 9 + 7
import numpy as np
def fft_convolve(f, g, MOD=MOD):
"""
数列 (多項式) f, g の畳み込みの計算.上下 15 bitずつ分けて計算することで,
30 bit以下の整数,長さ 250000 程度の数列での計算が正確に行える.
"""
fft = np.fft.rfft
ifft = np.fft.irfft
Lf = len(f)
Lg = len(g)
L = Lf + Lg - 1
fft_len = 1 << L.bit_length()
fl = f & (1 << 15) - 1
fh = f >> 15
gl = g & (1 << 15) - 1
gh = g >> 15
def conv(f, g):
return ifft(fft(f, fft_len) * fft(g, fft_len))[:L]
x = conv(fl, gl) % MOD
y = conv(fl + fh, gl + gh) % MOD
z = conv(fh, gh) % MOD
a, b, c = map(lambda x: (x + .5).astype(np.int64), [x, y, z])
return (a + ((b - a - c) << 15) + (c << 30)) % MOD
def coef_of_generating_function(P, Q, N):
"""compute the coefficient [x^N] P/Q of rational power series.
Parameters
----------
P : np.ndarray
numerator.
Q : np.ndarray
denominator
Q[0] == 1 and len(Q) == len(P) + 1 is assumed.
N : int
The coefficient to compute.
"""
def convolve(f, g):
return fft_convolve(f, g, MOD)
while N:
Q1 = Q.copy()
Q1[1::2] = np.negative(Q1[1::2])
if N & 1:
P = convolve(P, Q1)[1::2]
else:
P = convolve(P, Q1)[::2]
Q = convolve(Q, Q1)[::2]
N >>= 1
return P[0]
def solve_1(N, K, A):
S = [0] * (K + 1)
for i, x in enumerate(A, 1):
S[i] = S[i - 1] + x
for n in range(N + 1, K + 1):
S[n] = 2 * S[n - 1] - S[n - N - 1]
S[n] %= MOD
return (S[K] - S[K - 1]) % MOD, S[K]
def solve_2(N, K, A):
S = [0] * (N + 1)
for i, x in enumerate(A, 1):
S[i] = S[i - 1] + x
S = np.array(S[:N + 1], np.int64)
Q = np.zeros(N + 2, np.int64)
Q[0] = 1
Q[1] = -2
Q[N + 1] = 1
P = np.convolve(S, Q)[:N + 1]
x = coef_of_generating_function(P, Q, K)
y = coef_of_generating_function(P, Q, K - 1)
return (x - y) % MOD, x
N, K, *A = map(int, read().split())
solve = solve_2 if N <= 30 else solve_1
print(*solve(N, K, A))
maspy