Hanoi Tower Troubles Again!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 602 Accepted Submission(s):
418
Problem 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.
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.
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.
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
4
25
Sample Output
11
337
337
Source
Recommend
————————————————我是分割线————————————————————————————————
一道水题。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int main()
{
std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
int t,n,i,f[];
f[]=;f[]=;
for(i=;i<=;i++)
{
if(i%)
f[i]=f[i-]+i+;//当最小路径覆盖为奇数时,就=f[i-1]+i+1;
else
f[i]=f[i-]+i;//否则就是上一个数+i;
}
cin>>t;
while(t--)
{
cin>>n;
cout<<f[n]<<endl;
}
return ;
}