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. cocos2d-x 实现跨平台的目录遍历

    可能各位看官们有更好的方法,请不吝赐教. #ifdef _WIN32 #include <io.h> #else #include <unistd.h> #include &l ...

  2. Opengl坐标转换

    有时候我们需要手动计算三维点对应的二维坐标,下面的矩阵公式就是模拟了一遍三维图形的流程.这里已假设读者具备了图形学的基础知识,比如矩阵乘法代表的三维变换,放射变换:还有齐次坐标等. 这里不考虑模型自身 ...

  3. python cookbook学习笔记 第一章 文本(1)

    1.1每次处理一个字符(即每次处理一个字符的方式处理字符串) print list('theString') #方法一,转列表 结果:['t', 'h', 'e', 'S', 't', 'r', 'i ...

  4. dede搜索引擎

    1.dede模板中的html: <form action="{dede:fieldname='phpurl'/}/search.php" name="formsea ...

  5. Win7下python Scrapy一站式搭建全攻略(内附相关下载链接)

    写在前面: 好久没有登录博客了,意外看到之前的几篇文章都有不错的阅读量,开心极了,不过没有什么点赞和评论,大概是没有给大家带来什么切实的帮助吧.o(*////▽////*)q. 最近在NTU负责一个国 ...

  6. C++ STD accumulate函数

    1. 介绍 用来计算特定范围内(包括连续的部分和初始值)所有元素的和,除此之外,还可以用指定的二进制操作来计算特定范围内的元素结果.其头文件在numeric中. 用次函数可以求和,构造前n项和的向量, ...

  7. app外包开发注意事项大全『最新』

    随着移动互联网的高速发展,很多互联网创业公司或传统向互联网转型的公司都急需发开一款app软件,多数公司会选择让外包公司来开发.问题来了,App外包市场鱼龙混杂,我们要如何在这里面选择一个靠谱的公司,这 ...

  8. The Linux Mint 18.1:Eclipse Run The C++ And Python ConfigorationWhen You achieve above step,you can run the c++ and python! (Next OTL ,PYOTL is Project That Write By Ruimin Shen(ability man) )

    # Copyright (c) 2016, 付刘伟 (Liuwei Fu)# All rights reserved.# 转载请注明出处 1.Install The Eclipse,g++ Use T ...

  9. ng-class,与ng-click

    要求,后台传过来的数据,要求:(样式)性别为男的,变为灰色.(事件)并且没有点击事件,但女的有 <html> <head> <meta charset="utf ...

  10. js 判断是否为正数

    function validate(num) { var reg = /^\d+(?=\.{0,1}\d+$|$)/   if(reg.test(num)) return true;   return ...