joj 1031 Hanoi Tower Troubles Again!解题报告

 

 1031: Hanoi Tower Troubles Again!

Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
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++
#include<iostream> using namespace std; int main() { int n,t; cin>>t; for(int i=1;i<=t;++i) { cin>>n; if(n==1) cout<<1<<endl; else { int a=1,c=2; for(int j=2;j<=n;++j) { if(j%2==1) c+=2; a+=c; } cout<<a<<endl; } } return 0; }  

 

你可能感兴趣的:(Integer,input,语言,each,output,Numbers)