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++
 

 

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