結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー qqqq
提出日時 2019-07-08 17:17:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 345 ms / 5,000 ms
コード長 1,418 bytes
コンパイル時間 449 ms
コンパイル使用メモリ 82,240 KB
実行使用メモリ 78,080 KB
最終ジャッジ日時 2024-10-08 22:29:26
合計ジャッジ時間 7,848 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 10**9+7
k, n = map(int, input().split())
a = list(map(int, input().split()))

# 累積和
if n <= 10**6:
    aa = [0]*n
    # 1-indexed
    s = [0]*(n+1)
    for i in range(k):
        aa[i] = a[i]
        s[i+1] = s[i] + a[i]
    for i in range(k, n):
        ai = s[i] - s[i-k]
        aa[i] = ai
        aa[i] %= mod
        s[i+1] = s[i] + ai
        s[i+1] %= mod
    print(aa[-1], s[-1])
# 行列累乗
else:
    def mul(a, b):
        n = len(a)
        c = [[0]*n for i in range(n)]
        for i in range(n):
            for j in range(n):
                for k in range(n):
                    c[i][j] += (a[i][k]*b[k][j])%mod
        return c

    def pow(a, n):
        b = [[0]*len(a) for i in range(len(a))]
        for i in range(len(a)):
            b[i][i] = 1
        while n:
            if n&1:
                b = mul(a, b)
            a = mul(a, a)
            n >>=1
        return b

    A = [[0]*k for i in range(k)]
    A[0] = [1]*k
    for i in range(k-1):
        A[i+1][i] = 1
    A = pow(A, n-k)
    f = 0
    for i in range(k):
        f += A[0][i]*a[k-i-1]
        f %= mod

    B = [[0]*(k+1) for i in range(k+1)]
    B[0][0] = 2
    B[0][-1] = -1
    for i in range(k):
        B[i+1][i] = 1
    B = pow(B, n-k)
    from itertools import accumulate
    s = [0] + list(accumulate(a))
    g = 0
    for i in range(k+1):
        g += B[0][i]*s[k-i]
        g %= mod
    print(f, g)
0