Fibonacci-like sequences P68314


Statement
 

pdf   zip

Inspired by the Fibonacci sequence $F_0 = 0, F_1 = 1, F_n = F_{n-1} + F_{n-2} \mbox{\, for \,} n \ge 2$, Xavier defined his own sequence of numbers: $$X_0 = 0, X_1 = 1, X_n = X_{X_{n-1}} + X_{X_{n-2}} \mbox{\, for \,} n \ge 2 .$$ Max also wanted his own sequence of numbers, so he modified Xavier’s definition a bit: $$M_0 = 1, M_1 = 0, M_n = M_{M_{n-1}} + M_{M_{n-2}} \mbox{\, for \,} n \ge 2 .$$ Can you compute the nn-th term of any of these two new sequences?

Input

Input consists of several cases, each with a character cc, which is ‘X’ or ‘M’, and a natural nn between 0 and 10910^9.

Output

For each case, print XnX_n or MnM_n depending on cc.

Public test cases
  • Input

    X 0
    X 1
    X 2
    X 3
    M 0
    M 1
    M 2
    M 3
    

    Output

    0
    1
    1
    2
    1
    0
    1
    1
    
  • Information
    Author
    Félix Moreno
    Language
    English
    Official solutions
    C++
    User solutions
    C++