eolymp
bolt
Try our new interface for solving problems
Problems

Rock-Paper-Scissors on a Plane

Rock-Paper-Scissors on a Plane

In this problem, you have to travel from one corner of a checkered field to the opposite corner conforming to certain rules and avoiding making too much moves. \includegraphics{https://static.e-olymp.com/content/22/223dd6b7d297e56baacae6a714c0a0362b3b726b.jpg} There is a checkered field on a plane consisting of \textbf{m} rows and \textbf{n} columns. Each cell of the field contains one of three items: rock, paper or scissors. We start moving from the cell (\textbf{1}, \textbf{1}) and want to reach the cell (\textbf{m}, \textbf{n}) by performing a sequence of moves. A single move consists of jumping into any cell that is no farther than \textbf{d} from the current cell. The distance is measured between cell centers: the distance between cells (\textbf{x_1}, \textbf{y_1}) and (\textbf{x_2}, \textbf{y_2}) is . Additionally, each move must lead to a cell containing an item which defeats the item on the previous cell. As in the game “Rock-Paper-Scissors”, rock defeats scissors, scissors defeat paper, and paper defeats rock. When we arrive at a cell, we take the item from that cell, so it is forbidden to visit a single cell twice. The field is fairly large, so the types of items in its cells are determined by use of a random number generator, more precisely, a linear congruential generator. This generator has a state which is an integer s from \textbf{0} to \textbf{2^32} - \textbf{1}. In order to obtain the next random number from \textbf{0} to \textbf{r} - \textbf{1}, we carry out the assignment \textbf{s} := (\textbf{s} ∙ \textbf{1 664 525} + \textbf{1 013 904 223}) mod \textbf{2^32} and return the remainder of dividing \textbf{s} by \textbf{r}. The initial value of \textbf{s} is given in the input. The items in the cells are determined in the following way. For each cell, we obtain the next random number from the set \{\textbf{0}, \textbf{1}, \textbf{2}\} (that is, \textbf{r} = \textbf{3}). If this number is a zero, the item is rock, if it is one, paper, and in case it is two, scissors. Cells are considered row-by-row from top to bottom, cells in one row are considered from left to right. So, the order of cells is the following: (\textbf{1}, \textbf{1}), (\textbf{1}, \textbf{2}), ... , (\textbf{1}, \textbf{n}), (\textbf{2}, \textbf{1}), (\textbf{2}, \textbf{2}), ... , (\textbf{2}, \textbf{n}), ... , (\textbf{m}, \textbf{1}), (\textbf{m}, \textbf{2}), ... , (\textbf{m}, \textbf{n}). \includegraphics{https://static.e-olymp.com/content/d9/d98be79eb148ae50821afda889189efe62f16b70.jpg} An important additional requirement is that we must finish in time no greater than the time it would take to travel from cell (\textbf{1}, \textbf{1}) to cell (\textbf{m}, \textbf{n}) along a straight line with half the maximum speed, plus three extra moves. Formally, the number of moves must not exceed the real number . Given the dimensions of the field, the maximum travel distance for a single move and the initial state of the random number generator, find a path satisfying all the constraints. \InputFile The first line contains integers \textbf{m}, \textbf{n}, \textbf{d} and \textbf{s} separated by spaces: the height and width of the field, the maximum travel distance for a single move and the initial state of the random number generator (\textbf{100} ≤ \textbf{m}, \textbf{n} ≤ \textbf{2^16}, \textbf{10} ≤ \textbf{d} ≤ \textbf{100}, \textbf{0} ≤ \textbf{s} ≤ \textbf{2^32} - \textbf{1}). Note that the lower bounds for the numbers \textbf{m}, \textbf{n} and \textbf{d} are fairly large. \OutputFile On the first line, print the number of moves \textbf{k} in the path you found. In the next (\textbf{k} + \textbf{1}) lines, print the coordinates of the visited cells in the order of traversal. Print the row first and the column second. \includegraphics{https://static.e-olymp.com/content/d9/d98be79eb148ae50821afda889189efe62f16b70.jpg} The first cell must be (\textbf{1}, \textbf{1}), and the last one must be (\textbf{m}, \textbf{n}). Each cell may be visited at most once. The item in each next cell must defeat the item in the previous cell. The distance between any two consecutive cells of the path must not exceed \textbf{d}, and the total number of moves \textbf{k} must not exceed the real number . \includegraphics{https://static.e-olymp.com/content/36/36097bed7dcdc97591bacb91bf9038bd0bbc44f6.jpg} If there are several possible paths, print any one of them. It is guaranteed that in all the judges tests, there exists a path satisfying all constraints with length not exceeding the real number . \Note The following table contains the values of \textbf{s} and the items for the cells visited in the example. \includegraphics{https://static.e-olymp.com/content/1c/1ceaa4e76d1a4530cee9b24457c52d38b4b1214c.jpg} \includegraphics{https://static.e-olymp.com/content/1d/1d094fce4b97cfd3d8e65accf5d89895d475c773.jpg} The value of the fraction is \textbf{3.0959}..., so in this example, it is required to make no more than nine moves, and the judges formally guarantee that there exists a path consisting of seven moves or less.
Time limit 1 second
Memory limit 64 MiB
Input example #1
100 120 50 5
Output example #1
5
1 1
31 41
62 80
98 114
99 119
100 120
Source 2014 Petrozavodsk, Ivan Kazmenko Contest, August 22, Problem H