結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー LeonardoneLeonardone
提出日時 2017-06-10 05:53:36
言語 Ruby
(3.3.0)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 351 bytes
コンパイル時間 469 ms
コンパイル使用メモリ 11,812 KB
実行使用メモリ 16,704 KB
最終ジャッジ日時 2023-10-24 10:09:08
合計ジャッジ時間 3,088 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
16,700 KB
testcase_01 AC 95 ms
16,700 KB
testcase_02 AC 97 ms
16,700 KB
testcase_03 AC 95 ms
16,700 KB
testcase_04 AC 95 ms
16,700 KB
testcase_05 AC 96 ms
16,700 KB
testcase_06 AC 95 ms
16,700 KB
testcase_07 AC 96 ms
16,700 KB
testcase_08 AC 98 ms
16,704 KB
testcase_09 AC 97 ms
16,700 KB
testcase_10 AC 96 ms
16,700 KB
testcase_11 AC 97 ms
16,700 KB
testcase_12 AC 96 ms
16,700 KB
testcase_13 AC 96 ms
16,700 KB
testcase_14 AC 98 ms
16,700 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]]

$memo = {}
$memo[1] = mat
$memo[2] = (mat*mat).map{|x|x%M}

def hoge(n)
    if $memo.include? n
        $memo[n]
    else
        $memo[n] = (hoge(n/2)*hoge((n+1)/2)).map{|x|x%M}
    end
end

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