ZOJ-1239 Hanoi Tower Troubles Again!
链接: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!的更多相关文章
- 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 ...
- HDU 1329 Hanoi Tower Troubles Again!(乱搞)
Hanoi Tower Troubles Again! Problem Description People stopped moving discs from peg to peg after th ...
- zoj 2954 Hanoi Tower
Hanoi Tower Time Limit: 2 Seconds Memory Limit: 65536 KB You all must know the puzzle named "Th ...
- 10276 - Hanoi Tower Troubles Again!(思维,模拟)
People stopped moving discs from peg to peg after they know the number of steps needed to complete t ...
- 【HDOJ】1329 Hanoi Tower Troubles Again!
水题,搞清楚hanoi的定义就好做了. /* 1329 */ #include <cstdio> #include <cstring> #include <cstdlib ...
- hdu 1329 Hanoi Tower Troubles Again!
找规律的题目an=an-1+(i+i%2)/2*2; ;}
- Codeforces Gym 100114 A. Hanoi tower 找规律
A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...
- 汉诺塔 Hanoi Tower
电影<猩球崛起>刚开始的时候,年轻的Caesar在玩一种很有意思的游戏,就是汉诺塔...... 汉诺塔源自一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度 ...
- 3-6-汉诺塔(Hanoi Tower)问题-栈和队列-第3章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第3章 栈和队列 - 汉诺塔(Hanoi Tower)问题 ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版> ...
随机推荐
- js学习
2014-02-21 var p=function(){}(); //表示定义一个变量P,变量后面的函数为返回值 var p = function(){return 'abc';}(); alert( ...
- 基于淘宝弹性布局方案lib-flexible的问题研究
上篇文章<淘宝弹性布局方案lib-flexible实践>结合一个简单的实例,说明了lib-flexible的基本用法,但是lib-flexible的这种适配方式在适配的时候会修改viewp ...
- ZeroMQ接口函数之 :zmq_ctx_new – 创建一个新的ZMQ 环境上下文
ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_ctx_new zmq_ctx_new(3) ØMQ Manual - ØMQ/3.2 ...
- 基于socket的TCP和UDP编程
一.概述 TCP(传输控制协议)和UDP(用户数据报协议是网络体系结构TCP/IP模型中传输层一层中的两个不同的通信协议. TCP:传输控制协议,一种面向连接的协议,给用户进程提供可靠的全双工的字节流 ...
- UVa #11582 Colossal Fibonacci Numbers!
巨大的斐波那契数 The i'th Fibonacci number f (i) is recursively defined in the following way: f (0) = 0 and ...
- 【python】安装python第三方库lxml时,遇到问题:[ERROR: 'xslt-config' 不是内部或外部命令,也不是可运行的程序]
一.概述 lxml介绍http://lxml.de/ 二.问题 ERROR: 'xslt-config' 不是内部或外部命令,也不是可运行的程序 三.解决方法 Scrapy在Windows上的安装笔记 ...
- Areas on the Cross-Section Diagram
Areas on the Cross-Section Diagram Aizu - ALDS1_3_D Areas on the Cross-Section Diagram 地域の治水対策として.洪 ...
- 解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法
方式一:Coding JavaScript <!--[if lt IE9]> <script> (function() { var e = "abbr, articl ...
- [转]RMAN检测数据库坏块
backup validate check logical database; select * from v$database_block_corruption; RMAN> backup v ...
- C# 方法返回值的个数
方法返回值类型总的来说分为值类型,引用类型,Void 有些方法显示的标出返回值 public int Add(int a,int b) { return a+b; } 有些方法隐式的返回返回值,我们可 ...