結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー yuppe19 😺yuppe19 😺
提出日時 2015-05-25 23:43:46
言語 Python2
(2.7.18)
結果
AC  
実行時間 342 ms / 5,000 ms
コード長 1,413 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 6,744 KB
実行使用メモリ 37,552 KB
最終ジャッジ日時 2023-09-20 13:14:58
合計ジャッジ時間 7,663 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
5,828 KB
testcase_01 AC 11 ms
5,848 KB
testcase_02 AC 342 ms
5,976 KB
testcase_03 AC 45 ms
6,012 KB
testcase_04 AC 139 ms
5,900 KB
testcase_05 AC 114 ms
5,960 KB
testcase_06 AC 138 ms
6,068 KB
testcase_07 AC 219 ms
6,028 KB
testcase_08 AC 35 ms
6,036 KB
testcase_09 AC 171 ms
5,972 KB
testcase_10 AC 75 ms
6,016 KB
testcase_11 AC 77 ms
5,864 KB
testcase_12 AC 117 ms
5,960 KB
testcase_13 AC 54 ms
5,904 KB
testcase_14 AC 19 ms
5,848 KB
testcase_15 AC 263 ms
6,104 KB
testcase_16 AC 226 ms
5,964 KB
testcase_17 AC 70 ms
5,864 KB
testcase_18 AC 236 ms
5,964 KB
testcase_19 AC 322 ms
6,140 KB
testcase_20 AC 214 ms
13,788 KB
testcase_21 AC 269 ms
37,448 KB
testcase_22 AC 266 ms
37,552 KB
testcase_23 AC 22 ms
6,764 KB
testcase_24 AC 132 ms
20,604 KB
testcase_25 AC 123 ms
19,280 KB
testcase_26 AC 117 ms
18,560 KB
testcase_27 AC 150 ms
22,952 KB
testcase_28 AC 41 ms
9,348 KB
testcase_29 AC 246 ms
34,788 KB
testcase_30 AC 329 ms
6,148 KB
testcase_31 AC 13 ms
5,948 KB
testcase_32 AC 106 ms
6,076 KB
testcase_33 AC 153 ms
6,076 KB
testcase_34 AC 124 ms
6,048 KB
testcase_35 AC 106 ms
5,876 KB
testcase_36 AC 256 ms
5,980 KB
testcase_37 AC 35 ms
5,868 KB
testcase_38 AC 284 ms
6,020 KB
testcase_39 AC 121 ms
5,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python

mod = int(1e9) + 7

def solve1(n, k, a):
    y = 0
    for i in xrange(n):
        y = (y + a[i]) % mod
    z = y
    for i in xrange(n, k):
        a.append(z)
        y = (y + z) % mod
        z = (z + z - a[i-n]) % mod
    return a[-1], y

def matmul(A, B):
    p, q = len(A[0]), len(B)
    if p != q:
        print 'len(A[0])={} len(B)={}'.format(p, q)
        return None
    rows, cols, times = len(A), len(B[0]), len(A[0])
    res = [[0 for _ in xrange(cols)] for _ in xrange(rows)]
    for r in xrange(rows):
        for c in xrange(cols):
            res[r][c] = sum(A[r][i]*B[i][c] for i in xrange(times)) % mod
    return res

def matpow(A, n):
    sz = len(A)
    res = [[1 if i==j else 0 for j in xrange(sz)] for i in xrange(sz)]
    while n > 0:
        if n & 1:
            res = matmul(res, A)
        A = matmul(A, A)
        n >>= 1
    return res

def solve2(n, k, a):
    mat = [[0] * (n+1) for _ in xrange(n+1)]
    mat[0][0] = 2
    mat[0][-1] = -1
    for i in xrange(n):
        mat[i+1][i] = 1
    powered = matpow(mat, k-n)
    s = [0] + [sum(a[:i+1]) for i in xrange(n)]
    s = zip(s[::-1])
    res = matmul(powered, s)
    res00 = res[0][0]
    res10 = res[1][0]
    return (res00 - res10) % mod, res00 % mod

n, k = map(int, raw_input().split())
a = map(int, raw_input().split())
if k <= 1e6:
    x, y = solve1(n, k, a)
else:
    x, y = solve2(n, k, a)
print x, y
0