No tags yet

No tag edit access

time limit per test: 0.25 sec.

memory limit per test: 65536 KB

memory limit per test: 65536 KB

input: standard

output: standard

output: standard

You all must know the puzzle named ``The Towers of Hanoi''. The puzzle has three pegs and N discs of different radii, initially all disks are located on the first peg, ordered by their radii - the largest at the bottom, the mallest at the top. In a turn you may take the topmost disc from any peg and move it to another peg, the only rule says that you may not place the disc atop any smaller disk. The problem is to move all disks to the last peg making the smallest possible number of moves.

There is the legend that somewhere in Tibet there is a monastery where monks tirelessly move disks from peg to peg solving the puzzle for 64 discs. The legend says that when they finish, the end of the world would come.

Since it is well known that to solve the puzzle you need to make 2^{N}-1 moves, a small calculation shows that the world seems to be a quite safe place for a while.

However, recent archeologists discoveries have shown that the things can be a bit worse. The manuscript found in Tibet mountains says that the puzzle the monks are solving has not 3 but M pegs. This is the problem, because when increasing the number of pegs, the number of moves needed to move all discs from the first peg to the last one following the rules described, decreases dramatically.

Calculate how many moves one needs to move N discs from the first peg to the last one when the puzzle has M pegs and provide the scenario for moving the discs.

There is the legend that somewhere in Tibet there is a monastery where monks tirelessly move disks from peg to peg solving the puzzle for 64 discs. The legend says that when they finish, the end of the world would come.

Since it is well known that to solve the puzzle you need to make 2

However, recent archeologists discoveries have shown that the things can be a bit worse. The manuscript found in Tibet mountains says that the puzzle the monks are solving has not 3 but M pegs. This is the problem, because when increasing the number of pegs, the number of moves needed to move all discs from the first peg to the last one following the rules described, decreases dramatically.

Calculate how many moves one needs to move N discs from the first peg to the last one when the puzzle has M pegs and provide the scenario for moving the discs.

Input file contains N and M (1 ≤ N ≤ 64, 4 ≤ M ≤ 65).

On the first line output L - the number of moves needed to solve the puzzle. Next L lines must contain the moves themselves. For each move print the line of the form

move <disc-radius> from <source-peg> to <target-peg>

if the disc is moved to the empty peg or

move <disc-radius> from <source-peg> to <target-peg> atop <target-top-disc-radius>

if the disc is moved atop some other disc.

Disc radii are integer numbers from 1 to N, pegs are numbered from 1 to M.

move <disc-radius> from <source-peg> to <target-peg>

if the disc is moved to the empty peg or

move <disc-radius> from <source-peg> to <target-peg> atop <target-top-disc-radius>

if the disc is moved atop some other disc.

Disc radii are integer numbers from 1 to N, pegs are numbered from 1 to M.

Input

5 4

Output

13

move 1 from 1 to 3

move 2 from 1 to 2

move 1 from 3 to 2 atop 2

move 3 from 1 to 4

move 4 from 1 to 3

move 3 from 4 to 3 atop 4

move 5 from 1 to 4

move 3 from 3 to 1

move 4 from 3 to 4 atop 5

move 3 from 1 to 4 atop 4

move 1 from 2 to 1

move 2 from 2 to 4 atop 3

move 1 from 1 to 4 atop 2

move 1 from 1 to 3

move 2 from 1 to 2

move 1 from 3 to 2 atop 2

move 3 from 1 to 4

move 4 from 1 to 3

move 3 from 4 to 3 atop 4

move 5 from 1 to 4

move 3 from 3 to 1

move 4 from 3 to 4 atop 5

move 3 from 1 to 4 atop 4

move 1 from 2 to 1

move 2 from 2 to 4 atop 3

move 1 from 1 to 4 atop 2

Author: | Andrew Stankevich |

Resource: | Petrozavodsk Summer Trainings 2003 |

Date: | 2003-08-23 |

Codeforces (c) Copyright 2010-2019 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Oct/20/2019 00:06:50 (f1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|