链接:ZOJ1239

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

HINT

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdio>
#define ll long long
using namespace std; ll sum = ;
ll n;
int flag = ; int haha[] = {};
ll work(ll x, int i)
{ if( flag == n)
return ; ll t = x + haha[i]; double tt = pow(double(t),1.0 / );
if(fabs(tt - (ll)(tt)) < 0.000001)
{
flag = ;
haha[i] = x;
return + work(x+,);
}
else
{
if(haha[ ( i + ) % n ] == )
{
haha[i+] = x;
flag = ;
return +work(x + , );
}
else
{
flag++;
return work(x, (i+)%n);
}
}
}
int main()
{
//freopen("made.txt","w",stdout);
int T;
cin>>T;
while(T--)
{
cin>>n;
haha[] = ;
sum = work(, );
cout<<sum<<endl;
memset(haha,,sizeof(haha));
flag = ;
}
return ;
}

ZOJ-1239 Hanoi Tower Troubles Again!的更多相关文章

  1. HDU1329 Hanoi Tower Troubles Again!——S.B.S.

    Hanoi Tower Troubles Again! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  2. HDU 1329 Hanoi Tower Troubles Again!(乱搞)

    Hanoi Tower Troubles Again! Problem Description People stopped moving discs from peg to peg after th ...

  3. zoj 2954 Hanoi Tower

    Hanoi Tower Time Limit: 2 Seconds Memory Limit: 65536 KB You all must know the puzzle named "Th ...

  4. 10276 - Hanoi Tower Troubles Again!(思维,模拟)

    People stopped moving discs from peg to peg after they know the number of steps needed to complete t ...

  5. 【HDOJ】1329 Hanoi Tower Troubles Again!

    水题,搞清楚hanoi的定义就好做了. /* 1329 */ #include <cstdio> #include <cstring> #include <cstdlib ...

  6. hdu 1329 Hanoi Tower Troubles Again!

    找规律的题目an=an-1+(i+i%2)/2*2; ;}

  7. Codeforces Gym 100114 A. Hanoi tower 找规律

    A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...

  8. 汉诺塔 Hanoi Tower

    电影<猩球崛起>刚开始的时候,年轻的Caesar在玩一种很有意思的游戏,就是汉诺塔...... 汉诺塔源自一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度 ...

  9. 3-6-汉诺塔(Hanoi Tower)问题-栈和队列-第3章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第3章  栈和队列 - 汉诺塔(Hanoi Tower)问题 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版> ...

随机推荐

  1. java实现a+b的多重输入

    import java.util.Scanner; public class hello { public static void main(String[] args) { Scanner cin ...

  2. Mysql创建新用户方法

    1. CREATE USER 语法: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 例子: CREATE USER 'dog'@'lo ...

  3. java分享第七天-03(递归打印文件目录的树状结构)

    public static void main(String[] args) { File file= new File("e:/list"); printFile(file, 0 ...

  4. JavaScript模拟鼠标右键菜单

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Unity3D优化总结(一)

    1.如使用碰撞器,简单的模型尽量使用自带的碰撞器如BoxCollider,少使用Mesh Collider. 2.如要使用Mesh Collider,可以做一个专用的模型(尽量少网格)做为Mesh C ...

  6. Python traceback【转】

    1. Python中的异常栈跟踪 Python,在2.x中,异常对象可以是任何对象,异常对象和异常栈是分开的.python中用于处理异常栈的模块是traceback模块,它提供了print_excep ...

  7. Java集合类源码学习- Iterabel<T>,Colection<E>,AbstractCollection<E>

    Collection<E>接口extends Iteratable<E>接口. Iteratable<T>:实现接口使得对象能够成为“for-each loop”的 ...

  8. ionic 踩过的坑

    内联模板 : script可能你没有注意过,HTML中常用的script标签在AngularJS中被重新定义了: 除了原来的脚本声明功能之外,如果script元素的type属性 定义为text/ng- ...

  9. Vue.js起手式+Vue小作品实战

    本文是小羊根据Vue.js文档进行解读的第一篇文章,主要内容涵盖Vue.js的基础部分的知识的,文章顺序基本按照官方文档的顺序,每个知识点现附上代码,然后根据代码给予个人的一些理解,最后还放上在线编辑 ...

  10. pt-table-checksum和pt-heartbeat的使用

    一.pt-table-checksum 1.主从上均新建账户GRANT SELECT, PROCESS, SUPER, REPLICATION SLAVE ON *.* TO 'checksum'@' ...