結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー 👑 kmjpkmjp
提出日時 2015-02-13 02:12:11
言語 Python2
(2.7.18)
結果
AC  
実行時間 810 ms / 5,000 ms
コード長 1,414 bytes
コンパイル時間 217 ms
コンパイル使用メモリ 6,744 KB
実行使用メモリ 100,504 KB
最終ジャッジ日時 2023-09-06 00:34:02
合計ジャッジ時間 12,939 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
5,952 KB
testcase_01 AC 10 ms
5,944 KB
testcase_02 AC 810 ms
5,932 KB
testcase_03 AC 79 ms
5,880 KB
testcase_04 AC 297 ms
5,984 KB
testcase_05 AC 243 ms
5,972 KB
testcase_06 AC 301 ms
6,072 KB
testcase_07 AC 495 ms
5,920 KB
testcase_08 AC 59 ms
5,940 KB
testcase_09 AC 389 ms
6,040 KB
testcase_10 AC 153 ms
5,972 KB
testcase_11 AC 152 ms
5,880 KB
testcase_12 AC 256 ms
5,892 KB
testcase_13 AC 101 ms
6,020 KB
testcase_14 AC 30 ms
5,868 KB
testcase_15 AC 596 ms
5,980 KB
testcase_16 AC 500 ms
5,916 KB
testcase_17 AC 136 ms
5,952 KB
testcase_18 AC 528 ms
6,040 KB
testcase_19 AC 720 ms
6,040 KB
testcase_20 AC 29 ms
5,956 KB
testcase_21 AC 606 ms
100,504 KB
testcase_22 AC 11 ms
5,868 KB
testcase_23 AC 35 ms
9,172 KB
testcase_24 AC 281 ms
50,360 KB
testcase_25 AC 257 ms
46,748 KB
testcase_26 AC 248 ms
44,776 KB
testcase_27 AC 315 ms
57,360 KB
testcase_28 AC 77 ms
16,400 KB
testcase_29 AC 555 ms
93,224 KB
testcase_30 AC 735 ms
5,924 KB
testcase_31 AC 10 ms
5,944 KB
testcase_32 AC 222 ms
5,904 KB
testcase_33 AC 322 ms
5,984 KB
testcase_34 AC 267 ms
6,040 KB
testcase_35 AC 227 ms
5,996 KB
testcase_36 AC 572 ms
6,068 KB
testcase_37 AC 60 ms
5,980 KB
testcase_38 AC 647 ms
6,060 KB
testcase_39 AC 256 ms
6,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
# 想定解(2)

def matmult(A,B): # 正方行列A*B
	n=len(A)
	C=[[0 for i in range(n)] for j in range(n)]
	for x in range(n):
		for z in range(n):
			for y in range(n):
				C[x][y] += A[x][z]*B[z][y]
				C[x][y] %= mo;
	return list(C)

def matpow(A,p): # 正方行列A^p
	n=len(A)
	A=list(A)
	R=[[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		R[i][i]=1
	while p:
		if p%2:
			R = matmult(A,R)
		A=matmult(A,A)
		p >>= 1
	return R

def GetSum(A,x):
	# S[x]を求める
	
	# 行列累乗
	Ap = matpow(A,x-N)
	ret = 0
	for i in range(N+1):
		ret += S[N-i] * Ap[0][i]
	return ret % mo

N,K = map(int, raw_input().strip().split())
F = map(int, raw_input().strip().split())
F.insert(0,0)

mo = 1000000007

S = [0]
for i in range(1,N+1):
	S.append((S[i-1]+F[i]) % mo)

if N > 50:
	# こちらは想定解(1)と同じです
	for i in range(N+1,K+1):
		F.append((S[i-1]-S[i-N-1]) % mo)
		S.append((S[i-1]+F[i]) % mo)
	
	print "%d %d" % (F[K], S[K])
	
else:
	# Sに関する行列累乗を使うケース
	A=[[0 for i in range(N+1)] for j in range(N+1)]
	
	# S[i] = F[i] + S[i-1]
	#      = sum(F[i-1]...F[i-N]) + S[i-1]
	#      = S[i-1] + S[i-1] - S[i-1-N]
	A[0][0] = 2
	A[0][N] = mo-1
	for i in range(1,N+1):
		A[i][i-1]=1
	
	# S[K]とS[K-1]を求める
	RetS = GetSum(A,K)
	PreS = GetSum(A,K-1)
	
	# F[K]=S[K]-S[K-1]
	RetF = (RetS - PreS + mo) % mo
	
	print "%d %d" % (RetF, RetS)
0