import java.io.*; import java.util.*; /** * The End of the World * @author vanb * * This is a simple recursive process, using knowledge of * the structure of the most optimal Towers of Hanoi solution. * The key fact: Moving a pyramid with n disks takes (2^n)-1 moves. */ public class theend { public Scanner sc; public PrintStream ps; // Keep track of which peg every disk is on char disks[]; /** * This is the number of moves remaining to solve ToH with n disks, * where we're trying to move from the source peg to the dest peg. * * @param n Largest disk to move * @param source Source peg * @param dest Destination peg * @param other The 'Other' peg * @return The number of moves remaining */ public long remaining( int n, char source, char dest, char other ) { long moves = 0; if( n>0 ) { --n; if( disks[n]==source ) { // If the largest disk is on the source peg, then we've got // to finish moving the n-1 pyramid on top (that's the call to remaining()), // then move this disk (1), then move the n-1 pyramid back. // That takes 2^n-1 (since we're 0-based - the smallest disk is disk 0) // So, that's a total of remaining() + 2^n moves. Note that we've got // to move the n-1 pyramid over to the other peg, so its source // is 'source' and its destination is 'other' moves = (1l<