UVA 10254 - The Priest Mathematician(规律)

Problem I

The Priest Mathematician

Input: standard input

Output: standard output

Time Limit: 10 seconds

 

The ancient folklore behind the "Towers of Hanoi" puzzle invented by E. Lucas in 1883 is quite well known to us. One more recent legend tells us that the Brahmin monks from Benares never believed that the world could vanish at the moment they finished to transfer the 64 discs from the needle on which they were to one of the other needles, and they decided to finish the task as soon as possible.

 

Fig: The Four Needle (Peg) Tower of Hanoi

 

One of the priests at the Benares temple (who loved the mathematics) assured their colleagues to achieve the transfer in the afternoon (the rhythm they had thought was a disc-per-second) by using an additional needle. They couldn't believe him, but he proposed them the following strategy:

 

-- First move the topmost discs (say the top k discs) to one of the spare needles.

 

-- Then use the standard three needles strategy to move the remaining n-k discs (for a general case with n discs) to their destination.

 

-- Finally, move the top k discs into their final destination using the four needles.

 

He calculated the value to k in order to minimize the number of movements and get a total of 18433 transfers, so they spent just 5 hours, 7minutes and 13 seconds against the more than 500000 millions years without the additional needle (as they would have to do 2^64-1 disc transfers. Can you believe it?)

 

Try to follow the clever priest's strategy and calculate the number of transfer using four needles but according with the fixed and immutable laws of Brahma, which require that the priest on duty must not move more than one disc at a time and that he must place this disc on a needle so that there is no smaller disc below it. Of course, the main goal is to calculate the k that minimize the number of transfers (even thought it is not know for sure that this is always the optimal number of movements).

 

Input

The input file contains several lines of input. Each line contains a single integer N, which is the number of disks to be transferred. Here0<=N<=10000. Input is terminated by end of file.

 

Output

For each line of input produce one line of output which indicates the number of movements required to transfer the N disks to the final needle.

 

Sample Input:

1

2

28

64

 

Sample Output:

1

3

769

18433

题意:4根汉诺塔、、

思路:打表找规律, 正常的公式为 f(n) = min(2 * f(k) + 2^(n - k) - 1) (1 <= k <= n - 1)

代码:

#include <stdio.h>
#include <string.h>
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
const int N = 1005;
const int MAXBIGN = 1005;


struct bign {
    int s[MAXBIGN];
    int len;
    bign() {
		len = 1;
		memset(s, 0, sizeof(s));
    }
	
    bign operator = (const char *number) {
		len = strlen(number);
		for (int i = 0; i < len; i++)
			s[len - i - 1] = number[i] - '0';
		return *this;
    }
    bign operator = (const int num) {
		char number[N];
		sprintf(number, "%d", num);
		*this = number;
		return *this;
    }
	
    bign (int number) {*this = number;}
    bign (const char* number) {*this = number;}
	
    bign operator + (const bign &c){  
		bign sum;
		int t = 0;
		sum.len = max(this->len, c.len);
		for (int i = 0; i < sum.len; i++) {
			if (i < this->len) t += this->s[i];
			if (i < c.len) t += c.s[i];
			sum.s[i] = t % 10;
			t /= 10;
		}
		
		while (t) {
			sum.s[sum.len++] = t % 10;
			t /= 10;
		}
		
		return sum;  
    }
	
	bign operator * (const bign &c){  
		bign sum; bign zero;
		if (*this == zero || c == zero)
			return zero;
		int i, j;
		sum.len = this->len + c.len;
		for (i = 0; i < this->len; i++) {
			for (j = 0; j < c.len; j ++) {
				sum.s[i + j] += this->s[i] * c.s[j];
			}
		}
		for (i = 0; i < sum.len; i ++) {
			sum.s[i + 1] += sum.s[i] / 10;
			sum.s[i] %= 10;
		}
		sum.len ++;
		while (!sum.s[sum.len - 1]) {
			sum.len --;
		}
		return sum;  
    }	
	bign operator * (const int &num) {
		bign c = num;
		return *this * c;
	}
	bign operator / (const int &num) {
		bign ans; int k = 0;
		ans.len = len;
		for (int i = ans.len - 1; i >= 0; i --) {
			ans.s[i] = (k * 10 + s[i]) / num;
			k = (k * 10 + s[i]) % num;
		}
		while (!ans.s[ans.len - 1]) {
			ans.len --;
		}
		return ans;
	}
    bign operator - (const bign &c) {
		bign ans;
		ans.len = max(this->len, c.len);
		int i;
		
		for (i = 0; i < c.len; i++) {
			if (this->s[i] < c.s[i]) {
				this->s[i] += 10;
				this->s[i + 1]--;
			}
			ans.s[i] = this->s[i] - c.s[i];
		}
		
		for (; i < this->len; i++) {
			if (this->s[i] < 0) {
				this->s[i] += 10;
				this->s[i + 1]--;
			}
			ans.s[i] = this->s[i];
		}
		while (ans.s[ans.len - 1] == 0) {
			ans.len--;
		}
		if (ans.len == 0) ans.len = 1;
		return ans;
    }
	
    void put() {
		if (len == 1 && s[0] == 0) {
			printf("0");
		} else {
			for (int i = len - 1; i >= 0; i--)
				printf("%d", s[i]);
		}
    }
	
    bool operator < (const bign& b) const {
		if (len != b.len)
			return len < b.len;
		
		for (int i = len - 1; i >= 0; i--)
			if (s[i] != b.s[i])
				return s[i] < b.s[i];
			return false;
    }
    bool operator > (const bign& b) const { return b < *this; }
    bool operator <= (const bign& b) const { return !(b < *this); }
    bool operator >= (const bign& b) const { return !(*this < b); }
    bool operator != (const bign& b) const { return b < *this || *this < b;}
    bool operator == (const bign& b) const { return !(b != *this); }
};

bign f[10005];
int n; 

void init() {
	f[1] = 1; bign m = 2; bign two = 2; int count = 2;
	for (int i = 2; i <= 10000;) {
		for (int j = i; j < count + i && j <= 10000; j ++)
			f[j] = f[j - 1] + m;
		m = m * two;
		i += count;
		count ++;
	}

}

int main() {
	init();
	while (~scanf("%d", &n)) {
		f[n].put();
		printf("\n");
	}
	return 0;
}


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