# 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