Problem D
Ordinary Ordinals
Sets, sets, sets. Everything in math is just a set. Even the natural numbers can be represented as sets. For example, we can represent the the number $0$ as the empty set $\{ \} $. The number $1$ can be represented as $\{ \{ \} \} $.
But what about $2$? Consider $\{ \{ \} , \{ \{ \} \} \} $, the set containing both the empty set and $\{ \{ \} \} $. This is a nice choice for $2$ for two reasons: we have that $0$ and $1$ are elements of $2$ and we also have that $0$ and $1$ are subsets of $2$.
In general, for $N>0$ we can represent $N$ as the set $\{ 0, 1, \ldots , N-1\} $ where we recursively apply the representations for $0, \ldots , N-1$. For example:
\begin{eqnarray*} 3 & = & \{ 0, 1, 2\} \\ & = & \{ \{ \} , \{ 0\} , \{ 0, 1\} \} \\ & = & \{ \{ \} , \{ \{ \} \} , \{ \{ \} , \{ 0\} \} \} \\ & = & \{ \{ \} , \{ \{ \} \} , \{ \{ \} , \{ \{ \} \} \} \} \end{eqnarray*}Thus, for each $0 \leq i < N$, $i$ is both a member of $N$ and a subset of $N$. Another nice feature is the size of the set representing $N$ is also $N$. But what is not so nice is the number of characters it takes to write such a set. Given a natural number $N \geq 0$, how many braces and commas are required to write out the set representing $N$ in the above manner?
More specifically, let $f(N)$ be the number of bracket and comma characters required to write out the set representing $N$. Since $f(N)$ can be quite large, your job is to determine $f(N)$ modulo some positive integer $M$.
Input
Input consists of two integers $N$ ($0 \leq N < 2^{63}$) and $M$ ($1 \leq M < 2^{31}$) as described above.
Output
Display the value of $f(N)$ reduced modulo $M$. That is, the remainder that would be left if you divided $f(N)$ by $M$.
Sample Input 1 | Sample Output 1 |
---|---|
2 100 |
9 |
Sample Input 2 | Sample Output 2 |
---|---|
3 100 |
19 |
Sample Input 3 | Sample Output 3 |
---|---|
3 7 |
5 |
Sample Input 4 | Sample Output 4 |
---|---|
1000000000 1000000007 |
851562505 |