天天看點

HDU 2064 漢諾塔III

Description

約19世紀末,在歐州的商店中出售一種智力玩具,在一塊銅闆上有三根杆,最左邊的杆上自上而下、由小到大順序串着由64個圓盤構成的塔。目的是将最左邊杆上的盤全部移到右邊的杆上,條件是一次隻能移動一個盤,且不允許大盤放在小盤的上面。 

現在我們改變遊戲的玩法,不允許直接從最左(右)邊移到最右(左)邊(每次移動一定是移到中間杆或從中間移出),也不允許大盤放到下盤的上面。 

Daisy已經做過原來的漢諾塔問題和漢諾塔II,但碰到這個問題時,她想了很久都不能解決,現在請你幫助她。現在有N個圓盤,她至少多少次移動才能把這些圓盤從最左邊移到最右邊? 

Input

包含多組資料,每次輸入一個N值(1<=N=35)。

Output

對于每組資料,輸出移動最小的次數。

Sample Input

1
3
12      

Sample Output

2
26
531440      

找規律遞推一下就好了

#include<iostream>  
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5;
const int low(int x){ return x&-x; }
int T, n;
LL f[maxn];

int main(){
  //scanf("%d", &T);
  for (int i = 1; i <= 35; i++) f[i] = f[i - 1] * 3 + 2;
  while (~scanf("%d", &n))
  {
    printf("%lld\n", f[n]);
  }
  return 0;
}