P2139 Xiao Z's Dice-Throwing Game

Description

Xiao Z obtained a peculiar die (as shown below) and wants to play a dice game with it. ![](https://cdn.luogu.com.cn/upload/pic/1263.png) The game is simple: each time, Xiao Z drops one die vertically from the same position. The die stops when it lands on the plane or on top of another die. However, because this is a special die, it will continue to roll and fall. This die has the following properties: - A normal die might roll in four directions, but this die can only roll toward the faces numbered $4, 5, 6$. In the situation shown below, this die can only roll in two directions. ![](https://cdn.luogu.com.cn/upload/pic/1264.png) - This die rolls only if rolling will cause it to descend (as shown below). When there are multiple directions available, it rolls toward the direction with the largest face number. ![](https://cdn.luogu.com.cn/upload/pic/1265.png) - After rolling exactly $90^\circ$, the die falls vertically. When it lands on the plane or on another die (as $B$ and $C$ in the figure below), it stops, and then continues the process of rolling and falling. ![](https://cdn.luogu.com.cn/upload/pic/1266.png) The figure above shows the process of dropping $4$ dice in sequence, each initially with top face $6$ and front face $4$. Now Xiao Z has dropped $n$ dice. He wants to know, after the game ends, how many times each number appears on the top when viewed from above (as shown below). Please help him. ![](https://cdn.luogu.com.cn/upload/pic/1267.png)

Input Format

There are multiple test cases. Each test case is formatted as follows: - Line $1$: an integer $n$, the number of dice dropped. - Lines $2$ to $n+1$: each line contains two integers $t, f$, meaning one die is dropped with top face $t$ and front face $f$.

Output Format

For each test case, output one line containing six integers, representing how many times each number appears on the top after the game ends, when viewed from above.

Explanation/Hint

This problem has $4$ test points, each worth $25$ points. For $25\%$ of the testdata, $n = 1$. For another $25\%$ of the testdata, $n = 2$. For $100\%$ of the testdata, $1 \leq n \leq 100$, and there are at most $30$ test cases. Translated by ChatGPT 5