HDU 1329 Hanoi Tower Troubles Again!

Hanoi Tower Troubles Again!

Time Limit: 1000ms

Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 1329
64-bit integer IO format: %I64d      Java class name: Main

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

Source

OIBH Reminiscence Programming Contest

解题:思维甚妙!通过把已知柱子数量求放置东西的最大量,转化为已知数量,求最小的柱子数量,然后求最小路径覆盖

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 const int maxn = 2000;
 6 bool e[maxn][maxn],ok[maxn<<1],used[maxn];
 7 int ans[55],link[maxn],n;
 8 int dfs(int u) {
 9     for(int i = 1; i <= n; ++i) {
10         if(!used[i] && e[u][i]) {
11             used[i] = true;
12             if(link[i] == -1||dfs(link[i])) {
13                 link[i] = u;
14                 return true;
15             }
16         }
17     }
18     return false;
19 }
20 int solve() {
21     int tmp = 0;
22     memset(link,-1,sizeof(link));
23     for(int i = 1; i <= n; ++i) {
24         memset(used,false,sizeof(used));
25         tmp += dfs(i);
26     }
27     return tmp;
28 }
29 int main() {
30     int i,j,t,o;
31     for(i = 1; i*i < (maxn<<1); ++i) ok[i*i] = 1;
32     for(i = 1; i < maxn; ++i)
33         for(j = i + 1; j < maxn; ++j)
34             e[i][j] = ok[i + j];
35     for(n = 1; n < maxn; ++n) {
36         int tmp = solve();
37         if(n - tmp > 50) break;
38         ans[n - tmp] = n;
39     }
40     scanf("%d",&t);
41     while(t--) {
42         scanf("%d",&o);
43         printf("%d\n",ans[o]);
44     }
45     return 0;
46 }

时间: 2024-11-23 14:24:18

HDU 1329 Hanoi Tower Troubles Again!的相关文章

【HDOJ】1329 Hanoi Tower Troubles Again!

水题,搞清楚hanoi的定义就好做了. 1 /* 1329 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 7 #define MAXN 55 8 9 int b[MAXN]; 10 int a[MAXN]; 11 12 bool isSquare(int x) { 13 int y = (int) sqrt(x*1.0); 14 15

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

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

课本源码部分 第3章  栈和队列 - 汉诺塔(Hanoi Tower)问题 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接??? <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题集解析使用说明        课本源码合辑  链接??? <数据结构>课本源码合辑        习题集全解析  链接??? <数据结构题集>习题解析合辑        本源码引入的文件  链接? 无外链        相关测试数据下载  链接? 无数

Hanoi Tower问题的求解

文章前部分为转载,转自http://www.cnblogs.com/yanlingyin/ 当然.这是一个经典的递归问题~   想必来看这篇博文的同学对汉诺塔应该不会陌生了吧, 写这篇博还是有初衷的: 之前学数据结构的时候自己看书.也上网上查了很多资料,资料都比较散.而且描述的不是很清楚,对于当时刚刚 接触算法的我,要完全理解还是有一定难度.今天刚好有时间就整理了下思路.重写分析了一下之前的疑惑的地方. 没有透彻的地方便都豁然开朗了.所以迫不及待把我的想法记录下来,和大家分享. 如果你也是和之前

hdu 4939 Stupid Tower Defense ( dp )

题目链接 题意:给出一条长为n个单位长度的直线,每通过一个单位长度需要t秒. 有3种塔,红塔可以在当前格子每秒造成x点伤害,绿塔可以在之后的格子每秒造成y点伤害, 蓝塔可以使通过单位长度的时间增加z秒.问如何安排3种塔的顺序使得造成的伤害最大,输出最大伤害值. 分析:比赛的时候实在是没有想出来有三种不同的 塔,每种塔的作用不同,怎么dp.看题解才知道,应该把 所有的红塔放到最后面,因为直线的长度是一定的,而红塔在前面不会增加后面的伤害,然后问题就是如何安排 绿塔和蓝塔,我这里d[i][j]代表前

2014多校第七场1005 || HDU 4939 Stupid Tower Defense (DP)

题目链接 题意 :长度n单位,从头走到尾,经过每个单位长度需要花费t秒,有三种塔: 红塔 :经过该塔所在单位时,每秒会受到x点伤害. 绿塔 : 经过该塔所在单位之后的每个单位长度时每秒都会经受y点伤害. 蓝塔 : 经过该塔所在单位之后,再走每个单位长度的时候时间会变成t+z. 思路 : 官方题解 : 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #define LL long long

汉诺塔 Hanoi Tower

一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度教的主神梵天在创造世界的时候,在其中一根针上从下到上穿好了由大到小的64片金片,这就是所谓的汉诺塔(Hanoi Tower).不论白天黑夜,总有一个僧侣在按照下面的法则移动这些金片:一次只移动一片,不管在哪根针上,小片必须在大片上面. 僧侣们预言,当所有的金片从梵天穿好的金片上移到另一根针上时,世界末日就会来临,而梵塔.寺庙和众生也会随之灭亡...... 故事不多说了,汉诺塔是递归思想的典型应用,上代码: 1 #i

1028. Hanoi Tower Sequence

Description Hanoi Tower is a famous game invented by the French mathematician Edourard Lucas in 1883. We are given a tower of n disks, initially stacked in decreasing size on one of three pegs. The objective is to transfer the entire tower to one of

Codeforces Gym 100114 A. Hanoi tower 找规律

A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description you the conditions of this task. There are 3 pivots: A, B, C. Initially, n disks of different diameter are placed on the pivot A: the smallest dis