UVA 10254 The Priest Mathematician

题意:四个柱子的汉诺塔游戏,按照题目的思路得出:f(n)=min{f(k)*2+2^(n-k)-1}(1=<k<=n),找出最小值,但数据很大,所以要用到大数,还有的是因为n太大,所以会超时,找规律,这里有好的证明:点击打开链接

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int MAXN = 10005;
int d2[MAXN][MAXN];
int hanoi[MAXN][MAXN];

int main(){
    int n;
    memset(d2,0,sizeof(d2));
    d2[0][0] = 1;
    for (int i = 1; i <= 150; i++)
        for (int j = 0; j < 60; j++){
            d2[i][j] += d2[i-1][j] * 2;
            if (d2[i][j] > 9){
                d2[i][j+1]++;
                d2[i][j] %= 10;
            }
        }
    memset(hanoi,0,sizeof(hanoi));
    hanoi[0][0] = 0;
    int p = 1;
    for (int i = 0; i <= 150 && p <= 10000; i++){
        for (int j = 1; j <= i+1 && p <= 10000; j++){
            for (int k = 0; k < 60; k++){
                hanoi[p][k] += hanoi[p-1][k] + d2[i][k];
                if (hanoi[p][k] > 9){
                    hanoi[p][k+1]++;
                    hanoi[p][k] %= 10;
                }
            }
            p++;
        }
    }
    while (scanf("%d",&n) != EOF){
        int s;
        for (int i = 59; i >= 0; i--)
            if (hanoi[n][i] != 0){
                s = i;
                break;
            }
        for (int i = s; i >= 0; i--)
            printf("%d",hanoi[n][i]);
        printf("\n");
    }
    return 0;
}



你可能感兴趣的:(UVA 10254 The Priest Mathematician)