結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー koi_kotyakoi_kotya
提出日時 2018-07-27 23:14:06
言語 Ruby
(3.3.0)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 475 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 11,284 KB
実行使用メモリ 15,320 KB
最終ジャッジ日時 2023-09-18 13:50:14
合計ジャッジ時間 3,459 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,060 KB
testcase_01 AC 82 ms
15,156 KB
testcase_02 AC 81 ms
15,084 KB
testcase_03 AC 81 ms
15,280 KB
testcase_04 AC 82 ms
15,240 KB
testcase_05 AC 80 ms
15,280 KB
testcase_06 AC 80 ms
15,276 KB
testcase_07 AC 81 ms
15,320 KB
testcase_08 AC 80 ms
15,156 KB
testcase_09 AC 81 ms
15,084 KB
testcase_10 AC 80 ms
15,076 KB
testcase_11 AC 81 ms
15,056 KB
testcase_12 AC 81 ms
15,264 KB
testcase_13 AC 80 ms
15,256 KB
testcase_14 AC 81 ms
15,128 KB
testcase_15 AC 79 ms
15,212 KB
testcase_16 AC 79 ms
15,076 KB
testcase_17 AC 79 ms
15,248 KB
testcase_18 AC 79 ms
15,316 KB
testcase_19 AC 80 ms
15,052 KB
testcase_20 AC 81 ms
15,296 KB
testcase_21 AC 81 ms
15,060 KB
testcase_22 AC 80 ms
15,076 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

$exp = Hash.new(nil)
$mod = 1000000007
def mtr(a,b)
  [[(a[0][0]*b[0][0]+a[0][1]*b[1][0])%$mod,(a[0][0]*b[0][1]+a[0][1]*b[1][1])%$mod],[(a[1][0]*b[0][0]+a[1][1]*b[1][0])%$mod,(a[1][0]*b[0][1]+a[1][1]*b[1][1])%$mod]]
end

def exp(n)
  if $exp[n]
    $exp[n]
  elsif n == 1
    [[1,1],[1,0]]
  elsif n == 0
    [[1,0],[0,1]]
  elsif n%2 == 0
    $exp[n] = mtr(exp(n/2),exp(n/2))
  else
    $exp[n] = mtr(exp(n/2),exp(n/2+1))
  end
end

n = gets.to_i
p exp(n)[0].inject(:*)%$mod
0