P5779 [CTSC2001] Clever Students

Description

A professor who teaches logic has three students, A, B, and C, who are very good at reasoning and quick mental arithmetic. One day, the professor gives them a problem: he sticks a note on each person’s forehead and tells them that each note has a positive integer, and that the sum of two of the numbers equals the third. Therefore, each student can see the integers on the other two students’ foreheads, but cannot see their own number. The professor first asks student A: “Can you guess your number?” A answers: “No.” The professor then asks student B: “Can you guess your number?” B thinks for a while and also answers: “No.” The professor asks student C the same question. After thinking for a moment, C shakes his head and answers: “No.” Next, the professor asks A the same question again, then asks B and C, and so on. After several rounds of questioning, when the professor asks someone again, that person suddenly smiles proudly and reports the exact number on their own forehead. Now, if you are told that: at the professor’s $N$-th question, the person whose turn it was guessed that the number on their forehead was $M$, can you infer what numbers are on the other two students’ foreheads? #### Hint Before anyone guesses their own number, everyone’s answers to the professor’s questions are always “No.” Also, other than this, there is no information exchange among A, B, and C. That is, each person’s reasoning is based only on the numbers on the other two foreheads, and the negative answers everyone has given to the professor’s questions. The professor always starts questioning from student A. You may assume that these three sufficiently smart students can determine their own number at the earliest possible round based on the known conditions, and they will never be wrong. With some analysis and reasoning, you will get the following conclusion: **the person with the largest number on their forehead always guesses their own number first.**

Input Format

The input consists of multiple sets of testdata, where each line represents one set of testdata. For each set of testdata, there are two integers $N$ and $M$, meaning that at the professor’s $N$-th question, the person whose turn it was guessed that the number on their forehead was $M$. The two numbers are separated by a space. The input ends with a line `-1 -1`.

Output Format

Output the results for each set of data in the order they appear in the input. For each set of data, the first line of the output is an integer $p$, the total number of possible cases. The next $p$ lines each contain three numbers, which are the numbers on the foreheads of A, B, and C. All solutions should be output in increasing order of A’s number; if A’s numbers are the same, then sort by increasing order of B’s number.

Explanation/Hint

Before anyone guesses their own number, everyone’s answers to the professor’s questions are always “No.” Also, other than this, there is no information exchange among A, B, and C. That is, each person’s reasoning is based only on the numbers on the other two foreheads, and the negative answers everyone has given to the professor’s questions. The professor always starts questioning from student A. You may assume that these three sufficiently smart students can determine their own number at the earliest possible round based on the known conditions, and they will never be wrong. With some analysis and reasoning, you will get the following conclusion: **the person with the largest number on their forehead always guesses their own number first.** # Input Format The input consists of multiple sets of testdata, where each line represents one set of testdata. For each set of testdata, there are two integers $N$ and $M$, meaning that at the professor’s $N$-th question, the person whose turn it was guessed that the number on their forehead was $M$. The two numbers are separated by a space. The input ends with a line `-1 -1`. # Output Format Output the results for each set of data in the order they appear in the input. For each set of data, the first line of the output is an integer $p$, the total number of possible cases. The next $p$ lines each contain three numbers, which are the numbers on the foreheads of A, B, and C. All solutions should be output in increasing order of A’s number; if A’s numbers are the same, then sort by increasing order of B’s number. # Hint For all data, $N