{"trustable":true,"prependHtml":"\u003cstyle type\u003d\"text/css\"\u003e\n div.illustration {\n float: right;\n padding-left: 20px;\n }\n div.illustration .illustration {\n width: 100%;\n border-radius: 4px;\n }\n pre {\n display: block;\n margin: 0 0 10px;\n font-size: 13px;\n line-height: 1.42857143;\n color: #333;\n word-break: break-all;\n word-wrap: break-word;\n }\n\u003c/style\u003e\n\u003cscript\u003e\n window.katexOptions \u003d {\n delimiters: [\n {left: \u0027\\\\[\u0027, right: \u0027\\\\]\u0027, display: true}\n ]\n };\n\u003c/script\u003e\n","sections":[{"title":"","value":{"format":"HTML","content":"\n \u003cdiv style\u003d\"width:45.00%\" class\u003d\"illustration\"\u003e\n \u003cimg src\u003d\"CDN_BASE_URL/f65bc1a3d579555278ec9002a8eef7f2?v\u003d1715802924\" alt\u003d\"/problems/primonimo/file/statement/en/img-0001.gif\" class\u003d\"illustration\"\u003e\n\n \n \u003c/div\u003e\n\n \u003cp\u003ePrimonimo is a game played on an \u003cspan class\u003d\"tex2jax_process\"\u003e$n \\times m$\u003c/span\u003e board filled with numbers\n taken from the range \u003cspan class\u003d\"tex2jax_process\"\u003e$1 \\ldots\n p$\u003c/span\u003e for some prime number \u003cspan class\u003d\"tex2jax_process\"\u003e$p$\u003c/span\u003e. At each move, a player selects a\n square and adds \u003cspan class\u003d\"tex2jax_process\"\u003e$1$\u003c/span\u003e to the\n numbers in all squares in the same row and column as the\n selected square. If a square already shows the number\n \u003cspan class\u003d\"tex2jax_process\"\u003e$p$\u003c/span\u003e, it wraps around to\n \u003cspan class\u003d\"tex2jax_process\"\u003e$1$\u003c/span\u003e.\u003c/p\u003e\n\n \u003cp\u003eThe game is won if all squares show \u003cspan class\u003d\"tex2jax_process\"\u003e$p$\u003c/span\u003e. Given an initial board, find a\n sequence of moves that wins the game!\u003c/p\u003e\n\n \u003ch2\u003eInput\u003c/h2\u003e\n\n \u003cp\u003eThe input consists of a single test case. The first line\n contains three numbers \u003cspan class\u003d\"tex2jax_process\"\u003e$n \\ m \\\n p$\u003c/span\u003e denoting the number of rows \u003cspan class\u003d\"tex2jax_process\"\u003e$n$\u003c/span\u003e (\u003cspan class\u003d\"tex2jax_process\"\u003e$1\n \\le n \\le 20$\u003c/span\u003e), the number of columns \u003cspan class\u003d\"tex2jax_process\"\u003e$m$\u003c/span\u003e (\u003cspan class\u003d\"tex2jax_process\"\u003e$1\n \\le m \\le 20$\u003c/span\u003e), and a prime number \u003cspan class\u003d\"tex2jax_process\"\u003e$p$\u003c/span\u003e (\u003cspan class\u003d\"tex2jax_process\"\u003e$2\n \\le p \\le 97$\u003c/span\u003e). Each of the next \u003cspan class\u003d\"tex2jax_process\"\u003e$n$\u003c/span\u003e lines consists of \u003cspan class\u003d\"tex2jax_process\"\u003e$m$\u003c/span\u003e numbers in the range \u003cspan class\u003d\"tex2jax_process\"\u003e$1 \\ldots p$\u003c/span\u003e.\u003c/p\u003e\n\n \u003ch2\u003eOutput\u003c/h2\u003e\n\n \u003cp\u003eIf a winning sequence of at most \u003cspan class\u003d\"tex2jax_process\"\u003e$p \\cdot m \\cdot n$\u003c/span\u003e moves exists,\n output an integer \u003cspan class\u003d\"tex2jax_process\"\u003e$k \\le p \\cdot\n m \\cdot n$\u003c/span\u003e denoting the number of moves in the sequence.\n Then output \u003cspan class\u003d\"tex2jax_process\"\u003e$k$\u003c/span\u003e moves as a\n sequence of integers that numbers the board in row-major order,\n starting with \u003cspan class\u003d\"tex2jax_process\"\u003e$1$\u003c/span\u003e. If\n there are multiple such sequences, you may output any one of\n them. If no winning sequence exists, output \u003ctt class\u003d\"ttfamily\"\u003e-1\u003c/tt\u003e.\u003c/p\u003e\n\n \u003ch2\u003eSample 1\u003c/h2\u003e\u003cbody\u003e\u003ctable class\u003d\"vjudge_sample\"\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e4 5 5\n2 1 1 1 2\n5 3 4 4 3\n4 3 3 3 2\n3 1 3 3 1\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e6\n19 12 2 18 5 5\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/body\u003e\n\n \u003ch2\u003eSample 2\u003c/h2\u003e\u003cbody\u003e\u003ctable class\u003d\"vjudge_sample\"\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e3 3 3\n3 1 1\n1 3 2\n3 2 3\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e13\n4 2 6 1 9 7 5 5 7 1 2 3 3\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/body\u003e\n\n \u003ch2\u003eSample 3\u003c/h2\u003e\u003cbody\u003e\u003ctable class\u003d\"vjudge_sample\"\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e3 2 2\n1 2\n2 1\n1 2\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e-1\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/body\u003e\n\n \u003ch2\u003eSample 4\u003c/h2\u003e\u003cbody\u003e\u003ctable class\u003d\"vjudge_sample\"\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e3 2 2\n2 1\n2 1\n1 1\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e1\n6\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/body\u003e\n "}}]}