#P15855. [蓝桥杯第二届国际赛] 汉诺塔问题

[蓝桥杯第二届国际赛] 汉诺塔问题

Problem Description

The Tower of Hanoi is a classic math problem.

Given three pegs A, B, and C, peg A has nn disks of different sizes stacked in order, with the largest at the bottom and the smallest at the top. Now you need to move all disks from peg A to peg C. What is the minimum number of moves required?

The minimum number of moves is 2n−12^n - 1. Moreover, if you must finish in the minimum number of moves, there is only one possible sequence of moves.

For example, when n=3n = 3, you need a total of 77 moves:

Move 11: move the smallest disk from A to C, written as A->C;

Move 22: move the 2nd smallest disk from A to B, written as A->B;

Move 33: move the smallest disk from C to B, written as C->B;

Move 44: move the 3rd smallest disk from A to C, written as A->C;

Move 55: move the smallest disk from B to A, written as B->A;

Move 66: move the 2nd smallest disk from B to C, written as B->C;

Move 77: move the smallest disk from A to C, written as A->C.

Now, between move xx and move yy, how many times does A->B occur, how many times does A->C occur, how many times does B->A occur, how many times does B->C occur, how many times does C->A occur, and how many times does C->B occur?

Input Format

The first line contains an integer nn.

The second line contains two integers x,yx, y, separated by a space.

Output Format

Output six lines, each containing one integer, representing the answers to the six questions above.

3
2 6
1
1
1
1
0
1

Hint

Constraints

For 30%30\% of the testdata, 1≤n≤101 \le n \le 10, 1≤x≤y≤2n−11 \le x \le y \le 2^n - 1.

For 60%60\% of the testdata, 1≤n≤301 \le n \le 30, 1≤x≤y≤2n−11 \le x \le y \le 2^n - 1.

For all testdata, 1≤n≤1001 \le n \le 100, 1≤x≤y≤2n−11 \le x \le y \le 2^n - 1, and y≤1018y \le 10^{18}.

Translated by ChatGPT 5