eolymp
bolt
Try our new interface for solving problems
Problems

Nim Matrix

Nim Matrix

\textit{\textbf{Function Sprague-Grundy}} graph (\textbf{X}, \textbf{F}) is a function \textbf{g}: \textbf{X} → \textbf{N} such that \includegraphics{https://static.e-olymp.com/content/a9/a95e3a2fa6a4bb21891b266af71619da5c09face.jpg} \includegraphics{https://static.e-olymp.com/content/a0/a01d6fe5baf86fd274dfc5600a30f76cc672dd3c.jpg} \textbf{g}(\textbf{x}) = \textbf{min}\{\textbf{n} ≥ \textbf{0} : \textbf{n} ≠ \textbf{g}(\textbf{y}) \textbf{y} \textbf{F}(\textbf{x})\}. This formula can be written compactly using the concept of \textbf{mex} (\textit{minimal excludant}). \textit{\textbf{The minimal excludant}} set of stylish non-negative integers defined as the smallest non-negative number not belonging to this set. Then \includegraphics{https://static.e-olymp.com/content/a0/a01d6fe5baf86fd274dfc5600a30f76cc672dd3c.jpg} \textbf{g}(\textbf{x}) = \textbf{mex}\{\textbf{g}(\textbf{y}) : \textbf{y} \textbf{F}(\textbf{x})\}. Suppose that \textbf{S} can only be non-negative integers. We define the \textbf{mex}(\textbf{S}) as the smallest element of not \textbf{S}. Then the infinite matrix \textbf{A} is defined as: \textbf{A}(\textbf{0}, \textbf{0}) = \textbf{0}, \textbf{A}(\textbf{i}, \textbf{j}) = \textbf{mex}(\textbf{S}),where \textbf{S} is found from \textbf{S} = \textbf{S_1} \textbf{AND} \textbf{S_2},\textbf{S_1} = \{\textbf{A}(\textbf{k}, \textbf{j}), \textbf{k} < \textbf{i}\}, \textbf{S_2} = \{\textbf{A}(\textbf{i}, \textbf{k}), \textbf{k} < \textbf{i}\}. Below are a few first values ​​of this matrix: Your task is to compute the relevant elements of this matrix. \InputFile Input data contains a few test cases, each of which is in a separate row contains the row number and column number for which to calculate the value of a matrix. Input does not exceed \textbf{2·10^9}. \OutputFile For each test case in a separate row, a corresponding value of the matrix.
Time limit 1 second
Memory limit 64 MiB
Input example #1
0 0
2 1
5 3
Output example #1
0
3
6