Fibonacci numbers (1) P21926


Statement
 

pdf   zip

The Fibonacci numbers FnF_n are defined as follows: Fn={0if n=01if n=1Fn1+Fn2if n2F_n = \left\{ \begin{array}{ll} 0 & \mbox{if $n = 0$} \\ 1 & \mbox{if $n = 1$} \\ F_{n-1} + F_{n-2} & \mbox{if $n \ge 2$} \end{array} \right. Therefore, the first Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …

For every given pair of natural numbers nn and mm, compute FnmodmF_n \bmod m.

Input

Input consists of several pairs of nn and mm. Assume 0n10000 \le n \le 1000 and 2m1082 \le m \le 10^8.

Output

For every given pair, print FnmodmF_n \bmod m.

Public test cases
  • Input

    0 100
    10 100
    10 9
    1000 87654321
    

    Output

    0
    55
    1
    41825580
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Official solutions
    C++ Python
    User solutions
    C++ Fortran Python