10212. Paper, Scissors, Stone

I'm a slow walker, but I never walk backwards.

Task Description

You are given two integer sequences $X(t)$ and $Y(t)$ for $t$ = 0, 1, 2, .... Sequence $X(t)$ is generated by the formula $X(t+1) = (aX(t) + b)$ $mod$ $c$. And sequence $Y(t)$ is generated by the formula $Y(t+1) = (dY(t) + e)$ $mod$ $f$. $a$, $b$, $c$, $d$, $e$, and $f$ are positive integers.

Write a program to play $paper$, $scissors$, $stone$ with the two integer sequences. For any integer $X$, we can determine whether $X$ is $paper$, $scissors$, or $stone$ with the value of $X$ $mod$ 3. $X$ is $paper$ when the value of $X$ $mod$ 3 is 0. $X$ is $scissors$ when the value of $X$ $mod$ 3 is 1. And $X$ is $stone$ when the value of $X$ $mod$ 3 is 2. $Scissors$ beat $paper$, $stone$ beats $scissors$, and $paper$ beats $stone$.

We will have many rounds of games by two players. In a round of game, we pick a new integer pair $(X(t)$, $Y(t))$ from each sequence and check which player wins. We keep picking new integer pairs until one player wins this round.

For example, $X(0)$ is 4 , and $X(t+1) = (5X(t) + 6)$ $mod$ $7$. Also $Y(0)$ is 6, and $Y(t+1) = (5Y(t) + 4)$ $mod$ $3$. We play three rounds. In the first round, we pick $(X(0)$, $Y(0)) = (4$, $6)$. $X(0)$ is $scissors$, $Y(0)$ is $paper$, so $X$ wins this round. In the second round, we have $(X(1)$, $Y(1)) = (5$, $1)$. $X(1)$ is $stone$. $Y(1)$ is $scissors$, so $X$ wins this round. In the third round, we have $(X(2)$, $Y(2)) = (3$, $0)$. Both $X(2)$ and $Y(2)$ are $papers$, so this pick ends in a draw. We keep picking a new pair $(X(3)$, $Y(3)) = (0$, $1)$. $X(3)$ is $paper$, $Y(3)$ is $scissors$, so $Y$ wins this round.

Input Format

The input contains only one test case. There are four positive integers $X(0)$, $a$, $b$, and $c$ in the first line. There are four positive integers $Y(0)$, $d$, $e$, and $f$ in the second line. And there is one positive integer $N$ in the third line. $N$ is number of round we will play.

Output Format

The output contains $N$ lines. There are two integers $w$ and $k$ in each line. The first integer $w$ determines which player wins this round. When $X$ wins, $w$ is 0, and $w$ is 1 when $Y$ wins. The second integer $k$ determines how many pairs we picked in this round.

Subtask

  • 10 points: $N$ is 1. We play only one round, and the are no draws in the game.
  • 10 points: $N$ is larger than 1, and there are no draws
  • 80 points: N is larger than 1, and there are draws in the games.

Sample Input 1

4 5 6 7
6 5 4 3
1

Sample Output 1

0 1

Sample Input 2

4 5 6 7
6 5 4 3
2

Sample Output 2

0 1
0 1

Sample Input 3

4 5 6 7
6 5 4 3
3

Sample Output 3

0 1
0 1
1 2

Discussion