結果

問題 No.720 行列のできるフィボナッチ数列道場 (2)
ユーザー cielciel
提出日時 2018-07-28 00:38:19
言語 Ruby
(3.2.2)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 426 bytes
コンパイル時間 630 ms
コンパイル使用メモリ 11,456 KB
実行使用メモリ 15,304 KB
最終ジャッジ日時 2023-09-19 05:18:08
合計ジャッジ時間 3,658 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
15,116 KB
testcase_01 AC 86 ms
15,096 KB
testcase_02 AC 86 ms
15,244 KB
testcase_03 AC 87 ms
15,292 KB
testcase_04 AC 88 ms
15,148 KB
testcase_05 AC 87 ms
15,116 KB
testcase_06 AC 89 ms
15,116 KB
testcase_07 AC 85 ms
15,032 KB
testcase_08 AC 90 ms
15,240 KB
testcase_09 AC 90 ms
15,304 KB
testcase_10 AC 87 ms
15,284 KB
testcase_11 AC 87 ms
15,160 KB
testcase_12 AC 87 ms
15,228 KB
testcase_13 AC 88 ms
15,300 KB
testcase_14 AC 89 ms
15,016 KB
testcase_15 AC 88 ms
15,100 KB
testcase_16 AC 90 ms
15,096 KB
testcase_17 AC 90 ms
15,072 KB
testcase_18 AC 90 ms
15,304 KB
testcase_19 AC 86 ms
15,072 KB
testcase_20 AC 83 ms
15,296 KB
testcase_21 AC 87 ms
15,108 KB
testcase_22 AC 89 ms
15,156 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

M=10**9+7
A=[[1,1],[1,0]]
def mul(a,b)
	r=(0...a.size).map{[0]*b[0].size}
	a.size.times{|y|
		b[0].size.times{|x|
			r[y][x]=b.size.times.reduce(0){|s,i|(s+a[y][i]*b[i][x])%M}
		}
	}
	r
end
def pow(a,n)
	e=(0...a.size).map{|i|[0]*i+[1]+[0]*(a.size-1-i)}
	while n>0
		e=mul(a,e) if n%2>0
		a=mul(a,a)
		n/=2
	end
	e[-1][0]
end
n,m=gets.split.map &:to_i
a=pow(A,m+1)
b=pow(A,m)
c=pow(A,m-1)
p pow([[a,b,0],[b,c,0],[0,1,1]],n+1)
0