# 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(a, n) if $memo.include? n $memo[n] else $memo[n] = (hoge(a,n/2)*hoge(a,(n+1)/2)).map{|x|x%M} end end p hoge(mat, N)[1,1]%M