joj 1031 Hanoi Tower Troubles Again!解题报告

 

 1031: Hanoi Tower Troubles Again!


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s 8192K 1034 606 Standard
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
这道题网上有很多牛人都是用到二分图的思想来解的,呃。。可惜我是初学者,所以只是用最低级的找规律的思想解的,解决完发现其实如果这道题找到规律,其实非常简单,N=1时结果为1,N=2时结果为3,以后到N=7结果依次为:7 ,11,17,23,31,此时规律已经显现出来了,N=1时与N=2是结果差2,往后结果依次差4,4,6,6,8,8。。。。。。所以,只要根据这个规律,就可以递推出问题的答案了。
代码:
语言:c++
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章