結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー mkawa2mkawa2
提出日時 2021-09-23 00:16:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 389 ms / 5,000 ms
コード長 1,702 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 10,940 KB
実行使用メモリ 55,468 KB
最終ジャッジ日時 2023-09-18 19:44:42
合計ジャッジ時間 7,912 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,116 KB
testcase_01 AC 17 ms
7,980 KB
testcase_02 AC 290 ms
8,120 KB
testcase_03 AC 42 ms
8,140 KB
testcase_04 AC 119 ms
7,980 KB
testcase_05 AC 99 ms
8,140 KB
testcase_06 AC 119 ms
8,184 KB
testcase_07 AC 187 ms
8,072 KB
testcase_08 AC 35 ms
8,064 KB
testcase_09 AC 145 ms
8,032 KB
testcase_10 AC 65 ms
8,044 KB
testcase_11 AC 69 ms
8,076 KB
testcase_12 AC 101 ms
8,008 KB
testcase_13 AC 50 ms
8,084 KB
testcase_14 AC 24 ms
8,144 KB
testcase_15 AC 226 ms
8,044 KB
testcase_16 AC 193 ms
8,216 KB
testcase_17 AC 63 ms
8,124 KB
testcase_18 AC 199 ms
8,008 KB
testcase_19 AC 270 ms
8,632 KB
testcase_20 AC 238 ms
24,216 KB
testcase_21 AC 389 ms
54,956 KB
testcase_22 AC 361 ms
55,468 KB
testcase_23 AC 31 ms
9,968 KB
testcase_24 AC 189 ms
30,324 KB
testcase_25 AC 175 ms
28,728 KB
testcase_26 AC 168 ms
27,584 KB
testcase_27 AC 216 ms
33,716 KB
testcase_28 AC 60 ms
13,964 KB
testcase_29 AC 356 ms
51,636 KB
testcase_30 AC 282 ms
8,476 KB
testcase_31 AC 16 ms
8,088 KB
testcase_32 AC 93 ms
8,812 KB
testcase_33 AC 132 ms
8,076 KB
testcase_34 AC 106 ms
8,084 KB
testcase_35 AC 93 ms
8,736 KB
testcase_36 AC 215 ms
8,012 KB
testcase_37 AC 34 ms
8,008 KB
testcase_38 AC 243 ms
8,044 KB
testcase_39 AC 103 ms
8,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = 10**16
# md = 998244353
md = 10**9+7

def dot(aa, bb):
    h = len(aa)
    w = len(bb[0])
    res = [[0]*w for _ in range(h)]
    for i, row in enumerate(aa):
        for j, col in enumerate(zip(*bb)):
            v = 0
            # for a, b in zip(row, col): v += a*b
            # res[i][j] = v%md
            for a, b in zip(row, col): v += a*b%md
            res[i][j] = v%md
    return res

def matpow(mat, e):
    n = len(mat)
    res = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
    while e:
        if e & 1: res = dot(res, mat)
        mat = dot(mat, mat)
        e >>= 1
    return res

def solve1():
    s = sum(aa)%md
    for _ in range(k-n):
        aa.append(s)
        s = s*2-aa[-n-1]
        s %= md
    s = 0
    for a in aa[:k]:
        s += a
        s %= md
    print(aa[k-1], s)

def solve2():
    mat = [[0]*(n-1)+[1]*2 for _ in range(n+1)]
    for i in range(1, n): mat[i][i-1] = 1
    mat[n][n-1] = 0
    aa.append(sum(aa))

    mat = matpow(mat, k-n)
    ans = dot([aa], mat)
    print(ans[0][-2], ans[0][-1])

n, k = LI()
aa = LI()

if k <= 10**6:
    solve1()
else:
    solve2()
0