Hanoi Tower Troubles Again!

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.

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
 
Answer
可以找规律解=_=,输入1时答案是1,后面的答案间隔是:2,4,4,6,6,8,8...(答案是3,7,11,17,23,31,39...)
所以答案就出来了。当然也可以模拟。
 
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#define PI acos(-1.0)
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
#define msd memset(dp,0,sizeof(dp))
using namespace std;
//#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
//freopen("out.txt","w",stdout);
#endif // LOCAL
ios::sync_with_stdio(false);
int N;
cin>>N;
while(N--)
{
int n;
cin>>n;
int ans=,ca=;
if(n==)printf("1\n");
else if(n==)printf("3\n");
else
{
for(int i=;i<=n;i++)
{
if(i%)ca+=;
ans+=ca;
}
printf("%d\n",ans);
}
}
return ;
}

HDU 1329 Hanoi Tower Troubles Again!(乱搞)的更多相关文章

  1. hdu 1329 Hanoi Tower Troubles Again!

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

  2. 【HDOJ】1329 Hanoi Tower Troubles Again!

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

  3. 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 ...

  4. ZOJ-1239 Hanoi Tower Troubles Again!

    链接:ZOJ1239 Hanoi Tower Troubles Again! Description People stopped moving discs from peg to peg after ...

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

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

  6. hdu 4112 Break the Chocolate(乱搞题)

    题意:要把一块n*m*k的巧克力分成1*1*1的单元,有两种操作方式:1,用手掰(假设力量无穷大),每次拿起一块,掰成两块小的:2,用刀切(假设刀无限长),可以把多块摆在一起,同时切开.问两种方式各需 ...

  7. HDU 4614 Vases and Flowers(线段树+记录区间始末点或乱搞)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题目大意:有n个空花瓶,有两种操作: 操作①:给出两个数字A,B,表示从第A个花瓶开始插花,插B ...

  8. hdu 4506 小明系列故事——师兄帮帮忙【幂取模乱搞】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4506 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  9. hdu 5246 乱搞

    题意:题目太长直接看链接 链接:点我 乱搞题 显然,一个人要想成功,必须大于等于最强的人的战斗力,所以我们从后往前看 这里直接拿例1解释,首先递减排个序 15,13,10,9,8 作差得2,3,1,1 ...

随机推荐

  1. MySQL 常用命令大全

    Mysql常用命令行大全 第一招.mysql服务的启动和停止 net stop mysql net start mysql 第二招.登陆mysql 语法如下: mysql -u用户名 -p用户密码 键 ...

  2. 读写分离子系统 - C# SQL分发子系统(ADO.NET,不支持EF)

    读写分离子系统 - C# SQL分发子系统(ADO.NET,不支持EF) 这次介绍的这个框架只适用于中小项目,并且各个读写数据库结构是一致的情况,还要并且是写入数据库只有1台情况. 我们来看看这个子系 ...

  3. network重启失败原因

    /etc/sysconfig/network-scripts/ifcfg-eth0   DEVICE='eth0'  eth0后面千万不能加空格之类的  

  4. Nginx之旅系列 - Nginx的configuration

    题记:Nginx之旅系列是用来记录Nginx从使用到源码学习的点点滴滴,分享学习Nginx的快乐 Nginx 首页: http://nginx.org/ Nginx的configuration 今天对 ...

  5. UVa1003-Cutting sticks

    试题描述 将一段木棒按要求切割,每次切割都要付出与木棒长度相同的代价,求最小代价切割. (多组数据) 输入描述 长度L. 切割点数n(n<=50). n个切割点. 输出描述 "The ...

  6. IOS设计模式学习(6)生成器

    1 前言 有时候,构建某些对象有多种不同方式.如果这些逻辑包含在构建这些对象的类中的单一方法中,构建的逻辑会非常荒唐(例如,针对各种构建需求的一大片嵌套if-else或者switch-case语句). ...

  7. ONLY三行脚本 SQL数据恢复到指定时间点

    经常看到有人误删数据,或者误操作,特别是Update和Delete的时候没有加WHERE ... 然后就喊爹喊娘了,怕是亲爹妈也无奈摇肩. 话说,如果没有犯过错误,那你还算是程序猿(媛)麽?!没了偶尔 ...

  8. Python_day1 基础语法

    1.基础语法变量: 在左侧自定义输入变量名,右侧可以输入任意类型赋值给左侧,如需制定类型,可以强转name = Jason, age = int(24) provience = ['beijing', ...

  9. Python 购物车----之用户部分

    知识点: 文件读,写操作,if 判断, for 循环 salary = input("输入你的工资:") bought_list = [] product_list = {} wi ...

  10. html表单---学习笔记03

    第8章 表单 8.1 表单标记<form> <form name="表单名称" method="提交方法" action="处理程序 ...