結果
問題 | No.194 フィボナッチ数列の理解(1) |
ユーザー | ayaoni |
提出日時 | 2020-12-06 14:44:05 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 199 ms / 5,000 ms |
コード長 | 1,991 bytes |
コンパイル時間 | 275 ms |
コンパイル使用メモリ | 82,116 KB |
実行使用メモリ | 215,004 KB |
最終ジャッジ日時 | 2024-09-17 13:06:34 |
合計ジャッジ時間 | 6,495 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
53,560 KB |
testcase_01 | AC | 38 ms
53,812 KB |
testcase_02 | AC | 199 ms
76,372 KB |
testcase_03 | AC | 92 ms
76,620 KB |
testcase_04 | AC | 133 ms
76,824 KB |
testcase_05 | AC | 122 ms
76,488 KB |
testcase_06 | AC | 130 ms
76,388 KB |
testcase_07 | AC | 157 ms
76,280 KB |
testcase_08 | AC | 95 ms
76,640 KB |
testcase_09 | AC | 137 ms
76,500 KB |
testcase_10 | AC | 103 ms
76,360 KB |
testcase_11 | AC | 104 ms
76,524 KB |
testcase_12 | AC | 121 ms
76,432 KB |
testcase_13 | AC | 94 ms
76,180 KB |
testcase_14 | AC | 83 ms
76,200 KB |
testcase_15 | AC | 177 ms
77,528 KB |
testcase_16 | AC | 157 ms
76,532 KB |
testcase_17 | AC | 103 ms
76,796 KB |
testcase_18 | AC | 164 ms
76,652 KB |
testcase_19 | AC | 194 ms
76,888 KB |
testcase_20 | AC | 137 ms
212,780 KB |
testcase_21 | AC | 141 ms
215,004 KB |
testcase_22 | AC | 140 ms
213,824 KB |
testcase_23 | AC | 49 ms
67,940 KB |
testcase_24 | AC | 98 ms
133,000 KB |
testcase_25 | AC | 97 ms
127,368 KB |
testcase_26 | AC | 96 ms
124,528 KB |
testcase_27 | AC | 104 ms
141,224 KB |
testcase_28 | AC | 61 ms
79,712 KB |
testcase_29 | AC | 132 ms
193,304 KB |
testcase_30 | AC | 198 ms
76,328 KB |
testcase_31 | AC | 45 ms
59,860 KB |
testcase_32 | AC | 118 ms
76,520 KB |
testcase_33 | AC | 135 ms
76,984 KB |
testcase_34 | AC | 124 ms
76,748 KB |
testcase_35 | AC | 117 ms
76,360 KB |
testcase_36 | AC | 176 ms
76,592 KB |
testcase_37 | AC | 96 ms
76,352 KB |
testcase_38 | AC | 185 ms
76,380 KB |
testcase_39 | AC | 128 ms
76,212 KB |
ソースコード
import sys from itertools import accumulate sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) def matrix_multiplication_mod(A,B,p): l = len(A) m = len(B) n = len(B[0]) C = [[0]*n for _ in range(l)] for i in range(l): for j in range(n): C[i][j] = sum((A[i][k]*B[k][j]) % p for k in range(m)) % p return C def matrix_power_mod(A,n,p): l = len(A) C = [[0] * l for _ in range(l)] for i in range(l): C[i][i] = 1 while n > 0: if n % 2 == 1: C = matrix_multiplication_mod(C,A,p) A = matrix_multiplication_mod(A,A,p) n >>= 1 return C N,K = MI() A = [0]+LI() mod = 10**9+7 if K <= 10**6: if K <= N: print(A[K],sum(A[:K+1])) exit() F = A S = list(accumulate(F)) for i in range(N+1,K+1): f = S[i-1]-S[i-1-N] f %= mod F.append(f) S.append((S[-1]+f)% mod) print(F[K],S[K]) else: if K <= N: print(A[K],sum(A[:K+1])) exit() X = [[0]*N for _ in range(N)] for i in range(N): X[0][i] = 1 for i in range(1,N): X[i][i-1] = 1 Y = [[0]*(N+1) for _ in range(N+1)] Y[0][0] = 2 Y[0][N] = -1 for i in range(1,N+1): Y[i][i-1] = 1 S = list(accumulate(A)) A = list(reversed(A[1:])) S.reverse() XX = matrix_power_mod(X,K-N,mod) YY = matrix_power_mod(Y,K-N,mod) ans_F,ans_S = 0,0 for i in range(N): ans_F += XX[0][i]*A[i] ans_F %= mod for i in range(N+1): ans_S += YY[0][i]*S[i] ans_S %= mod print(ans_F,ans_S)