結果

問題 No.720 行列のできるフィボナッチ数列道場 (2)
ユーザー cielciel
提出日時 2018-07-28 00:35:05
言語 Ruby
(3.3.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 433 bytes
コンパイル時間 147 ms
コンパイル使用メモリ 11,536 KB
実行使用メモリ 15,328 KB
最終ジャッジ日時 2023-09-19 05:12:19
合計ジャッジ時間 3,442 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
15,116 KB
testcase_01 AC 86 ms
15,076 KB
testcase_02 AC 86 ms
15,160 KB
testcase_03 AC 86 ms
15,236 KB
testcase_04 AC 86 ms
15,312 KB
testcase_05 AC 85 ms
15,260 KB
testcase_06 AC 85 ms
15,124 KB
testcase_07 AC 86 ms
15,144 KB
testcase_08 AC 86 ms
15,320 KB
testcase_09 AC 85 ms
15,248 KB
testcase_10 AC 85 ms
15,116 KB
testcase_11 AC 85 ms
15,144 KB
testcase_12 AC 86 ms
15,320 KB
testcase_13 AC 85 ms
15,124 KB
testcase_14 AC 85 ms
15,024 KB
testcase_15 AC 86 ms
15,276 KB
testcase_16 AC 85 ms
15,116 KB
testcase_17 AC 84 ms
15,328 KB
testcase_18 AC 84 ms
15,064 KB
testcase_19 AC 83 ms
15,320 KB
testcase_20 AC 82 ms
15,252 KB
testcase_21 AC 83 ms
15,108 KB
testcase_22 AC 85 ms
15,104 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

M=10**9+7
A=[[1,1],[1,0]]
def mul(a,b)
	r=a.size.times.map{[0]*b[0].size}.to_a
	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=a.size.times.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=c=pow(A,m)
d=pow(A,m-1)
p pow([[a,b,0],[c,d,0],[0,1,1]],n+1)
0