#P5209. Modified Tower of Hanoi with Identical Disks

    ID: 18445 Type: Default 1000ms 256MiB

Modified Tower of Hanoi with Identical Disks

Modified Tower of Hanoi with Identical Disks

This problem is a variation of the classic Tower of Hanoi. In this version, disks can be of equal size. However, it is guaranteed that for a given size there are exactly K disks. In the initial configuration, all disks are placed on the first peg in descending order of size from bottom to top. For disks of the same size, they are labeled from 1 to K from bottom to top.

The objective is to move all disks from the first peg to the third peg while following these rules:

  • There are three pegs.
  • At any step, you may move the top disk from one peg to the top of another peg.
  • During the moves, a larger disk cannot be placed on top of a smaller disk. Disks of equal size, however, can be arranged in any order.

A unique final configuration is specified: all disks must be on the third peg with the same ordering as in the initial state (i.e. descending order from bottom to top, with identical disks in their numerical order). The task is to minimize the number of moves required.

Hint: The minimal number of moves required is given by the formula \[ \text{moves} = K(2^N - 1), \] where N is the number of distinct disk sizes and K is the number of disks for each size.

inputFormat

The input consists of a single line containing two integers N and K, separated by a space, where:

  • N is the number of distinct disk sizes.
  • K is the number of disks for each size.

Note: The total number of disks is N * K.

outputFormat

Output a single integer—the minimum number of moves required to move all disks from the first peg to the third peg following the rules.

The result is computed by the formula:

\[ K(2^N - 1) \]

sample

1 1
1