結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー toyuzukotoyuzuko
提出日時 2020-07-25 19:44:35
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 580 ms / 5,000 ms
コード長 1,856 bytes
コンパイル時間 136 ms
コンパイル使用メモリ 10,824 KB
実行使用メモリ 47,768 KB
最終ジャッジ日時 2023-09-09 21:27:37
合計ジャッジ時間 6,125 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,540 KB
testcase_01 AC 17 ms
8,504 KB
testcase_02 AC 48 ms
8,592 KB
testcase_03 AC 23 ms
8,380 KB
testcase_04 AC 32 ms
8,388 KB
testcase_05 AC 32 ms
8,404 KB
testcase_06 AC 33 ms
8,412 KB
testcase_07 AC 40 ms
8,472 KB
testcase_08 AC 22 ms
8,552 KB
testcase_09 AC 37 ms
8,416 KB
testcase_10 AC 26 ms
8,388 KB
testcase_11 AC 27 ms
8,392 KB
testcase_12 AC 31 ms
8,388 KB
testcase_13 AC 23 ms
8,404 KB
testcase_14 AC 19 ms
8,432 KB
testcase_15 AC 44 ms
8,560 KB
testcase_16 AC 41 ms
8,532 KB
testcase_17 AC 25 ms
8,504 KB
testcase_18 AC 41 ms
8,448 KB
testcase_19 AC 47 ms
8,536 KB
testcase_20 AC 465 ms
16,352 KB
testcase_21 AC 580 ms
47,684 KB
testcase_22 AC 574 ms
47,768 KB
testcase_23 AC 38 ms
10,148 KB
testcase_24 AC 282 ms
27,092 KB
testcase_25 AC 261 ms
25,632 KB
testcase_26 AC 243 ms
24,696 KB
testcase_27 AC 319 ms
29,892 KB
testcase_28 AC 83 ms
13,144 KB
testcase_29 AC 543 ms
44,468 KB
testcase_30 AC 47 ms
8,580 KB
testcase_31 AC 17 ms
8,408 KB
testcase_32 AC 29 ms
8,344 KB
testcase_33 AC 34 ms
8,416 KB
testcase_34 AC 31 ms
8,524 KB
testcase_35 AC 29 ms
8,508 KB
testcase_36 AC 42 ms
8,452 KB
testcase_37 AC 22 ms
8,452 KB
testcase_38 AC 44 ms
8,472 KB
testcase_39 AC 31 ms
8,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import accumulate

MOD = 1000000007

N, K = map(int, input().split())
A = list(map(int, input().split()))
C = [0] + list(accumulate(A))

if N <= 10000 and K <= 1000000:
    res = [0 for _ in range(K)]
    for i in range(N):
        res[i] = A[i]
    res[N] = sum(A)
    for i in range(N + 1, K):
        res[i] = res[i - 1] * 2 - res[i - N - 1]
        res[i] %= MOD
    print(res[-1], sum(res) % MOD)

else:
    def inc(coeff, base):
        n = len(coeff)
        res = [0 for _ in range(n)]
        res[0] = coeff[n - 1] * base[0]
        for i in range(1, n):
            res[i] = coeff[i - 1] + coeff[n - 1] * base[i]
            res[i] %= MOD
        return res

    def dec(coeff, base):
        n = len(coeff)
        res = [0 for _ in range(n)]
        res[n - 1] = coeff[0] * pow(base[0], MOD - 2, MOD)
        for i in range(n - 1)[::-1]:
            res[i] = coeff[i + 1] - res[n - 1] * base[i + 1]
            res[i] %= MOD
        return res

    def dub(coeff, base):
        n = len(coeff)
        res = [0 for _ in range(n)]
        tmp = [coeff]
        for i in range(n - 1):
            tmp.append(inc(tmp[-1], base))
        for i in range(n):
            for j in range(n):
                res[i] += tmp[0][j] * tmp[j][i]
                res[i] %= MOD
        return res

    def solve(base, arr, exp):
        coeff = base[:]
        n = len(coeff)
        for _ in range(n):
            coeff = dec(coeff, base)

        for bit in range(exp.bit_length())[::-1]:
            coeff = dub(coeff, base)
            if (exp >> bit) & 1:
                coeff = inc(coeff, base)

        return sum([coeff[i] * arr[i] for i in range(n)]) % MOD

    base = [1 for _ in range(N)]
    F = solve(base, A, K - 1)

    base = [0 for _ in range(N + 1)]
    base[0] = -1
    base[-1] = 2
    S = solve(base, C, K)

    print(F, S)
0