結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー はむ吉🐹はむ吉🐹
提出日時 2018-07-29 14:58:58
言語 Ruby
(3.3.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 545 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 11,436 KB
実行使用メモリ 15,904 KB
最終ジャッジ日時 2023-09-24 22:28:05
合計ジャッジ時間 4,050 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
15,596 KB
testcase_01 AC 98 ms
15,820 KB
testcase_02 AC 101 ms
15,864 KB
testcase_03 AC 98 ms
15,860 KB
testcase_04 AC 97 ms
15,600 KB
testcase_05 AC 97 ms
15,736 KB
testcase_06 AC 96 ms
15,656 KB
testcase_07 AC 97 ms
15,668 KB
testcase_08 AC 97 ms
15,860 KB
testcase_09 AC 97 ms
15,812 KB
testcase_10 AC 96 ms
15,840 KB
testcase_11 AC 95 ms
15,604 KB
testcase_12 AC 95 ms
15,604 KB
testcase_13 AC 95 ms
15,680 KB
testcase_14 AC 96 ms
15,896 KB
testcase_15 AC 98 ms
15,608 KB
testcase_16 AC 99 ms
15,824 KB
testcase_17 AC 97 ms
15,600 KB
testcase_18 AC 97 ms
15,904 KB
testcase_19 AC 99 ms
15,716 KB
testcase_20 AC 98 ms
15,840 KB
testcase_21 AC 96 ms
15,724 KB
testcase_22 AC 97 ms
15,668 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

#!/usr/bin/env ruby


require "matrix"


M = 1000000007


def mod_mul(a, b, mod=M)
  return (a * b).map{|x| x % mod}
end


def mod_pow(a, n, mod=M)
  raise "assert" unless a.square?
  b = Matrix.I(a.column_size)
  while n > 0
    if n % 2 == 1
      b = mod_mul(b, a, mod)
    end
    a = mod_mul(a, a, mod)
    n /= 2
  end
  return b
end


def solve(n, mod=M)
  a = Matrix[[1, 1], [1, 0]]
  res = mod_mul(mod_pow(a, n, mod), Vector[1, 0])
  return res[0] * res[1] % mod
end


def main()
  n = gets.to_i
  ans = solve(n)
  puts(ans)
end


main
0