// The priest mathematician (Priest mathematician) // PC/Ultraviolet IDs: 110606/10254, popularity: C, success rate: high level: 2 // verdict: accepted // submission date: 2011-06-07 // UV Run Time: 0.048 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net // set the minimum number of steps for moving n dishes in the case of 4-bar mode to T (n ), if the minimum number of moving steps under the condition of the three pillars is H (N), then // the minimum number of moving steps T (n) can be obtained through the following method ). Assume that one plate is first moved to a non-target column through four columns, // then n-1 plate is moved to the target column through three columns, finally, move the first plate to the target column. The total number of steps is H (n-1) + 2 * t (1). If you move two plates for the first time, the number of steps is H (n-2) + 2 * t (2). According to //, when n is set, 2 * t (m) + H (n-M) (0 <= m <= N) minimum value. Since a maximum of // 10000 plates are allowed, the number of moving methods will be a large integer, which is beyond the long expression range. In fact, you can also calculate and compare the minimum number of moving steps. We can observe that the difference between the last one of T (N) and the previous one forms the following series (n> = 1 ): /// 1 2 2 2 4 4 8 8 8 16 16 16 16 32 32 32 32 32 ...... /// The M power of 2 appears (m + 1) times, m> = 0. # Include <iostream> # include <vector> # include <iomanip> # include <algorithm> using namespace STD; Class INTEGER {friend ostream & operator <(ostream &, const integer &); Public: INTEGER () {}; // converts an unsigned integer to a large integer. INTEGER (unsigned int A) {if (a = 0) digits. push_back (0); else {While (a) {digits. push_back (a % base); A/= base ;}}};~ INTEGER () {}; integer operator * (const integer &); integer & operator * = (unsigned INT); integer & operator * = (const integer &); integer operator + (integer &); Private: void zero_justify (void); vector <unsigned int> digits; // number. Static unsigned int const base = 10000; // base number. Static unsigned int const width = 4; // digital width .}; // Reload the output symbol <. Ostream & operator <(ostream & OS, const integer & number) {OS <number. digits [number. digits. size ()-1]; for (INT I = number. digits. size ()-2; I> = 0; I --) OS <SETW (number. width) <setfill ('0') <number. digits [I]; return OS;} // remove the leading 0 produced by the large number operation. Void INTEGER: zero_justify (void) {for (INT I = digits. size ()-1; I> = 1; I --) {If (digits [I] = 0) digits. erase (digits. begin () + I); elsebreak;} integer & INTEGER: Operator * = (const integer & B) {return * This = * This * B ;} // multiply the large number by the unsigned integer. Integer & INTEGER: Operator * = (unsigned int B) {If (B> = base) {* This * = INTEGER (B); return * This ;} int carry = 0; For (INT I = 0; I <digits. size (); I ++) {digits [I] = digits [I] * B + carry; carry = digits [I]/base; digits [I] % = base;} If (carry) digits. push_back (carry); return * This;} // multiplication. Integer INTEGER: Operator * (const integer & B) {INTEGER C; // pre-allocate sufficient space. C. digits. resize (digits. size () + B. digits. size (); fill (C. digits. begin (), C. digits. end (), 0); // calculates the number of rows and adds them one by one. For (INT I = 0; I <B. digits. size (); I ++) for (Int J = 0; j <digits. size (); j ++) {C. digits [I + J] + = digits [J] * B. digits [I]; C. digits [I + J + 1] + = C. digits [I + J]/base; C. digits [I + J] % = base;} // remove the leading 0. C. zero_justify (); Return C;} // addition. Integer INTEGER: Operator + (integer & B) {INTEGER C; C. digits. resize (max (digits. size (), B. digits. size () + 1); fill (C. digits. begin (), C. digits. end (), 0); int carry = 0, x, y, t; for (INT I = 0; I <C. digits. size (); I ++) {x = y = 0; if (I <digits. size () x = digits [I]; if (I <B. digits. size () y = B. digits [I]; t = x + y + carry; carry = T/base; C. digits [I] = T % base;} // clear the leading 0. C. zero_justify (); Return C ;}# define maxdisks 10001 // series characteristics formed by the difference between the first and second items of the minimum number of steps. The M power of 2 appears (m + 1. Void Hanoi (vector <int> disks) {// calculates the minimum number of moving steps for the number of plates. Vector <integer> steps (maxdisks); steps [0] = 0; integer exponent (1); For (INT Index = 1, sequence = 1; index <maxdisks; sequence ++) {for (INT start = 1; Start <= SEQUENCE & index <maxdisks; Start ++, index ++) steps [Index] = steps [index-1] + exponent; exponent * = 2;} // output result. For (INT I = 0; I <disks. size (); I ++) cout <steps [disks [I] <Endl;} int main (INT AC, char * AV []) {vector <int> disks; int D; // Number of plates read. While (CIN> d) disks. push_back (d); // calculates the minimum number of moving steps. Hanoi (disks); Return 0 ;}