結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー LeonardoneLeonardone
提出日時 2017-06-10 05:09:03
言語 Ruby
(3.2.2)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 393 bytes
コンパイル時間 974 ms
コンパイル使用メモリ 11,880 KB
実行使用メモリ 16,912 KB
最終ジャッジ日時 2023-10-24 10:02:57
合計ジャッジ時間 4,064 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
16,736 KB
testcase_01 AC 103 ms
16,736 KB
testcase_02 AC 104 ms
16,736 KB
testcase_03 AC 102 ms
16,736 KB
testcase_04 AC 102 ms
16,736 KB
testcase_05 AC 103 ms
16,736 KB
testcase_06 AC 103 ms
16,736 KB
testcase_07 AC 102 ms
16,736 KB
testcase_08 AC 105 ms
16,736 KB
testcase_09 AC 106 ms
16,848 KB
testcase_10 AC 109 ms
16,848 KB
testcase_11 AC 125 ms
16,912 KB
testcase_12 AC 124 ms
16,912 KB
testcase_13 AC 126 ms
16,912 KB
testcase_14 AC 122 ms
16,912 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

# yukicoder My Practice
# author: Leonardone @ NEETSDKASU

require 'matrix'

N, M = gets.strip.split.map &:to_i

mat = Matrix[[1, 1], [1, 0]]

def hoge(a, n)
    if n == 1
        a
    elsif n == 2
        (a * a).map{|x|x%M}
    elsif n.even?
        b = hoge(a, n / 2)
        (b * b).map{|x|x%M}
    else
        (hoge(a,n/2)*hoge(a,(n+1)/2)).map{|x|x%M}
    end
end

p hoge(mat, N)[1,1]%M
0