题目传送门
/*
题意:汉诺塔问题变形,多了第四个盘子可以放前k个塔,然后n-k个是经典的汉诺塔问题,问最少操作次数
递推+高精度+找规律:f[k]表示前k放在第四个盘子,g[n-k]表示经典三个盘子,2 ^ (n - k) - 1
所以f[n] = min (f[k] * 2 + g[n-k]),n<=10000,所要要用高精度,另外打表能看出规律
*/
/************************************************
* Author :Running_Time
* Created Time :2015-8-18 9:14:21
* File Name :UVA_10254.cpp
************************************************/
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
struct bign {
short s[MAXN] , len ;
bign () { memset ( s , , sizeof ( s ) ) ; len = ; }
bign operator = (const char *num) {
len = strlen ( num ) ;
for ( int i = ; i < len ; i ++ ) s[i] = num[len-i-] - '' ;
return *this ;
}
bign operator = (int num) {
char s[MAXN];
sprintf (s , "%d" , num);
*this = s ;
return *this ;
}
bign(const char *num) { *this = num ; }
bign(int num) { *this = num ; }
string str () const {
string res ;
res = "" ;
for (int i = ; i < len; i ++) res = (char) (s[i] + '') + res ;
if (res == "") res = '';
return res ;
}
bign operator + (const bign& b) const {
bign c ;
c.len = ;
for(int i = , g = ; g || i < max (len, b.len); i ++) {
int x = g ;
if (i < len) x += s[i] ;
if (i < b.len) x += b.s[i] ;
c.s[c.len++] = x % ;
g = x / ;
}
return c ;
}
void print() {
for(int i = len - ; i >= ; i --) printf("%hd", s[i]);
printf("\n");
}
}f[];
int main(void) { //UVA 10254 The Priest Mathematician
bign g = ; f[] = ;
for (int i=, j=; i<=; j++, g=g+g) {
for (int k=; k<=j && i<=; k++,i++) {
f[i] = f[i-] + g;
}
}
int n;
while (scanf ("%d", &n) == ) {
f[n].print ();
}
return ;
}