結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー Mao-betaMao-beta
提出日時 2024-04-03 19:40:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 103 ms / 5,000 ms
コード長 2,190 bytes
コンパイル時間 770 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 141,136 KB
最終ジャッジ日時 2024-04-03 19:40:54
合計ジャッジ時間 4,936 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
57,824 KB
testcase_01 AC 46 ms
57,824 KB
testcase_02 AC 89 ms
72,544 KB
testcase_03 AC 56 ms
66,140 KB
testcase_04 AC 67 ms
68,444 KB
testcase_05 AC 64 ms
68,444 KB
testcase_06 AC 66 ms
68,444 KB
testcase_07 AC 75 ms
70,496 KB
testcase_08 AC 57 ms
66,140 KB
testcase_09 AC 71 ms
68,444 KB
testcase_10 AC 62 ms
68,452 KB
testcase_11 AC 61 ms
66,140 KB
testcase_12 AC 66 ms
68,444 KB
testcase_13 AC 57 ms
66,140 KB
testcase_14 AC 54 ms
66,140 KB
testcase_15 AC 80 ms
70,496 KB
testcase_16 AC 76 ms
70,496 KB
testcase_17 AC 58 ms
66,140 KB
testcase_18 AC 77 ms
70,496 KB
testcase_19 AC 87 ms
70,496 KB
testcase_20 AC 102 ms
138,680 KB
testcase_21 AC 103 ms
141,136 KB
testcase_22 AC 101 ms
138,680 KB
testcase_23 AC 55 ms
68,156 KB
testcase_24 AC 79 ms
98,428 KB
testcase_25 AC 79 ms
98,172 KB
testcase_26 AC 77 ms
94,720 KB
testcase_27 AC 81 ms
104,476 KB
testcase_28 AC 60 ms
74,064 KB
testcase_29 AC 98 ms
130,464 KB
testcase_30 AC 89 ms
72,544 KB
testcase_31 AC 53 ms
57,816 KB
testcase_32 AC 78 ms
76,132 KB
testcase_33 AC 69 ms
68,444 KB
testcase_34 AC 67 ms
68,444 KB
testcase_35 AC 75 ms
76,132 KB
testcase_36 AC 80 ms
70,496 KB
testcase_37 AC 57 ms
66,140 KB
testcase_38 AC 83 ms
70,496 KB
testcase_39 AC 64 ms
68,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
import bisect
from heapq import heapify, heappop, heappush
from collections import deque, defaultdict, Counter
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD99 = 998244353

input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()
SMI = lambda: input().split()
SLI = lambda: list(SMI())
EI = lambda m: [NLI() for _ in range(m)]


# 行列積(任意サイズ)
def mul_matrix(A, B, mod=998244353):
    Ah = len(A)
    Aw = len(A[0])
    Bh = len(B)
    Bw = len(B[0])
    assert Aw == Bh
    C = [[0] * Bw for _ in range(Ah)]
    for h in range(Ah):
        Arow = A[h]
        Crow = C[h]
        for i in range(Aw):
            a = Arow[i]
            Brow = B[i]
            for w in range(Bw):
                Crow[w] = (Crow[w] + a * Brow[w]) % mod
    return C

# 正方行列の累乗 mod
def pow_matrix(A, n, mod=998244353):
    assert len(A) == len(A[0])
    bitn = len(bin(n)) - 2
    pows = []
    size = len(A)
    E = [[0] * size for _ in range(size)]
    for i in range(size):
        E[i][i] = 1

    pows.append(A)
    ans = E

    for i in range(bitn):
        if (n >> i) & 1:
            ans = mul_matrix(pows[-1], ans, mod)
        pows.append(mul_matrix(pows[-1], pows[-1], mod))

    return ans


def main():
    N, K = NMI()
    A = NLI()

    if K <= 10**6:
        S = sum(A)
        for i in range(K-N):
            A.append(S % MOD)
            S = S - A[i] + A[i+N]
        print(A[-1], sum(A) % MOD)

    else:
        B = [[0]*(N+1) for _ in range(N+1)]
        B[0] = [1] * (N+1)
        B[0][-1] = 0
        for i in range(N-1):
            B[i+1][i] = 1
        B[-1] = [1] * (N+1)
        C = pow_matrix(B, K-N, MOD)
        A = A[::-1]
        A.append(sum(A))

        f, s = 0, 0
        for j in range(N+1):
            f += C[0][j] * A[j] % MOD
            f %= MOD

        for j in range(N+1):
            s += C[-1][j] * A[j] % MOD
            s %= MOD

        print(f, s)


if __name__ == "__main__":
    main()
0