Hanoi Tower Troubles Again! (找规律)

Description

People stopped moving discs from peg to peg after they know the number of steps needed to complete the entire task. But on the other hand, they didn't not stopped thinking about similar puzzles with the Hanoi Tower. Mr.S invented a little game on it. The game consists of N pegs and a LOT of balls. The balls are numbered 1,2,3... The balls look ordinary, but they are actually magic. If the sum of the numbers on two balls is NOT a square number, they will push each other with a great force when they're too closed, so they can NEVER be put together touching each other. 



The player should place one ball on the top of a peg at a time. He should first try ball 1, then ball 2, then ball 3... If he fails to do so, the game ends. Help the player to place as many balls as possible. You may take a look at the picture above, since it shows us a best result for 4 pegs. 
 

Input

The first line of the input contains a single integer T, indicating the number of test cases. (1<=T<=50) Each test case contains a single integer N(1<=N<=50), indicating the number of pegs available. 
 

Output

For each test case in the input print a line containing an integer indicating the maximal number of balls that can be placed. Print -1 if an infinite number of balls can be placed. 
 

Sample Input

    
    
2 4 25
 

Sample Output

    
    
11 337


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 10000;
int f[111];
int main()
{
	f[1]=1;f[2]=3;
	int i,j,n,t;
	int temp=2;
	for(i=3;i<=50;i++) {
		if(i%2) temp+=2;
		f[i]=f[i-1]+temp;
	}
	cin>>t;
	while(t--) {
		cin>>n;
		cout<<f[n]<<endl;
	}
	return 0;
} 


四柱汉诺塔是汉诺塔问题的一个变种,与经典的三柱汉诺塔问题不同,它有四个柱子而不是三个。规则如下: 1. 有四个柱子,标记为 A、B、C 和 D。 2. 一开始时,所有的盘子都在柱子 A 上,从大到小排列。 3. 目标是将所有盘子从柱子 A 移动到柱子 D 上,保持它们的顺序不变。 4. 在移动盘子时,可以执行以下操作之一: a. 将一个盘子从一个柱子移动到另一个柱子。 b. 将一个盘子从一个柱子移动到另一个柱子上的另一个盘子上,使其成为该柱子上的顶部盘子。 5. 在任何时候,不能将较大的盘子放在较小的盘子上面。 要解决四柱汉诺塔问题,可以使用递归算法,将问题分解为多个子问题,然后再逐步解决这些子问题。具体方法如下: 1. 如果要移动的盘子数目为 1,则将它直接从柱子 A 移动到柱子 D。 2. 如果要移动的盘子数目为 2,则可以先将一个盘子从柱子 A 移动到柱子 B,再将另一个盘子从柱子 A 移动到柱子 D,最后将柱子 B 上的盘子移动到柱子 D 上。 3. 如果要移动的盘子数目大于 2,则可以将它们分为两部分,其中一部分包括最上面的盘子,另一部分包括剩余的盘子。将第一部分的盘子从柱子 A 移动到柱子 B,然后将剩余的盘子从柱子 A 移动到柱子 C。接下来,将柱子 B 上的盘子和柱子 C 上的盘子移动到柱子 D 上。最后,将柱子 C 上的盘子移动到柱子 B 上,然后将柱子 D 上的盘子移动到柱子 C 上。现在,所有盘子都在柱子 B 和柱子 C 上,可以使用递归算法将它们移动到柱子 D 上。 下面是一个用 Python 语言实现的示例代码: ``` def four_column_hanoi(n, A, B, C, D): if n == 1: print(f"Move disk {n} from {A} to {D}") elif n == 2: print(f"Move disk {1} from {A} to {B}") print(f"Move disk {2} from {A} to {D}") print(f"Move disk {1} 我不熟悉汉诺塔。四柱汉诺塔是汉诺塔问题的一个变体,需要将一堆不同大小的圆盘从一个起始柱子移动到目标柱子,但是这次有四个柱子可用于移动圆盘,而不是原来的三个。 要解决四柱汉诺塔问题,可以采用递归的方法。下面是一个简单的解法: 1. 如果圆盘数量为 0,则返回。 2. 将所有小于等于当前圆盘的圆盘移动到辅助柱子 A。 3. 将当前圆盘移动到目标柱子 D。 4. 将辅助柱子 A 上的所有圆盘移动到辅助柱子 B。 5. 将所有小于等于当前圆盘的圆盘移动到目标柱子 D。 6. 将辅助柱子 B 上的所有圆盘移动到辅助柱子 A。 7. 重复步骤 3 到 6,直到所有圆盘都移动到目标柱子 D。 以下是一个 Python 代码示例: ``` def four_column_hanoi_tower(n, start, end, aux1, aux2): if n == 0: return if n == 1: print(f"Move disk 1 from {start} to {end}") return four_column_hanoi_tower(n-1, start, aux1, aux2, end) print(f"Move disk {n} from {start} to {end}") four_column_hanoi_tower(n-1, aux1, end, aux2, start) print(f"Move disk {n} from {end} to {start}") four_column_hanoi_tower(n-1, aux2, aux1, end, start) print(f"Move disk {n} from {start} to {end}") four_column_hanoi_tower(n-1, aux1, end, aux2, start) ``` 其中,n 表示圆盘的数量,start、end、aux1 和 aux2 分别表示起始柱子、目标柱子和两个辅助柱子的名称。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值