eolymp
bolt
Try our new interface for solving problems

Hexodoku

Sudoku is an amazing game. Many people have fun solving it. They say that a legendary programmer had spent just \textbf{7} minutes on writing a program that solves standard sudoku. That's über cool, don't you think? And now he has solved another problem. Can you do the same? Consider a non-standard hexagonal Sudoku board: \includegraphics{https://static.e-olymp.com/content/7e/7e0c79e1baf96d1777ff8d0166af7149bb688c6b.jpg} The cells are numbered from \textbf{1} to \textbf{31}. According to the rules, numbers (from \textbf{1} to \textbf{K}) can be placed in the cells with the condition that all numbers in the same row (rows are located in three directions) must be different. \includegraphics{https://static.e-olymp.com/content/46/469a2f8ee96401bfb8e08ee9340b1920a4aee20a.jpg} Additionally, for each of the marked cells, the number in marked cell and all numbers in adjacent cells must also differ from each other. \includegraphics{https://static.e-olymp.com/content/41/412e5cda43a232266638b2562afb540857322f8b.jpg} Must be different \includegraphics{https://static.e-olymp.com/content/7f/7fb0fdd59be97abf9fb42682826638b84d874ede.jpg} Some numbers may already be placed in the cells according to the rules. You are to find \textbf{N}^\{-th\} solution in lexicographical order, if it exists. Let \textbf{A_i} be the number in the cell \textbf{i} in solution \textbf{A}, and \textbf{B_i} --- the number in the cell \textbf{i} in solution \textbf{B}. Solution \textbf{A} is lexicographically smaller than solution \textbf{B}, if such \textbf{j} exists that for each \textbf{i} where \textbf{i} < \textbf{j}: \textbf{A_i = B_i} and \textbf{A_j} < \textbf{B_j}. \InputFile First line of input contains two integers \textbf{K} and \textbf{N}. \begin{itemize} \item \textbf{7} ≤ \textbf{K} ≤ \textbf{31} \item \textbf{1} ≤ \textbf{N} ≤ \textbf{100000} \end{itemize} Second line contains \textbf{31} integer numbers: \textbf{A_i} (\textbf{1} ≤ \textbf{i} ≤ \textbf{31}) is the number standing in the cell \textbf{i}. \begin{itemize} \item \textbf{1} ≤ \textbf{A_i} ≤ \textbf{K}, or \textbf{0}, if there is no number in this cell. \end{itemize} \OutputFile First line of output should contain an answer: \begin{itemize} \item "\textbf{Found}" --- if the solution has been found. \item "\textbf{No way}" --- if there is no N-th solution. \end{itemize} If the solution has been found, the second line of output should contain the \textbf{N}^\{-th\} solution in the same format as in input. This is the first example above: \includegraphics{https://static.e-olymp.com/content/33/33c305074d68cdfe135bf6ddd0adb2290d71e88e.jpg}
Zaman məhdudiyyəti 4 saniyə
Yaddaşı istafadə məhdudiyyəti 256 MiB
Giriş verilənləri #1
8 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Çıxış verilənləri #1
Found
1 2 1 3 4 5 2 2 4 6 7 1 3 7 5 1 8 6 2 1 3 4 5 7 8 6 7 2 3 5 8
Mənbə Izhevsk STU Contest, Petrozavodsk training camp, February 6, 2009