首页 > 其他分享 >HDU 1329 Hanoi Tower Troubles Again!

HDU 1329 Hanoi Tower Troubles Again!

时间:2022-11-09 20:04:20浏览次数:49  
标签:Again HDU balls int Hanoi number ch they include


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

 

简单的模拟一下放置的过程,把答案存起来就好了

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<functional>
using namespace std;
typedef unsigned long long ull;
typedef long long LL;
const int maxn = 1e5 + 10;
int T, n, m, f[maxn], g[maxn];

void Scan(int &x)
{
char ch;
while ((ch = getchar()) > '9' || ch < '0');
int res = ch - '0';
while ((ch = getchar()) <= '9'&&ch >= '0') res = res * 10 + ch - '0';
x = res;
}

bool check(int x)
{
int u = sqrt(x);
return u*u == x;
}

void init()
{
for (int i = 1, j = 0; j <= 50; i++)
{
int flag = 0;
for (int k = 1; k <= j&&!flag; k++)
{
if (check(g[k] + i)) { g[k] = i; flag = 1; }
}
if (!flag) { f[j] = i - 1; g[++j] = i; }
}
}

int main()
{
init();
cin >> T;
while (T--)
{
scanf("%d", &n);
printf("%d\n", f[n]);
}
return 0;
}



标签:Again,HDU,balls,int,Hanoi,number,ch,they,include
From: https://blog.51cto.com/u_15870896/5838695

相关文章

  • HDU 2475 Box
    DescriptionThereareNboxesontheground,whicharelabeledbynumbersfrom1toN.Theboxesaremagical,thesizeofeachonecanbeenlargedorreduc......
  • HDU 5432 Pyramid Split
    ProblemDescriptionXiaoMingisacitizenwho'sgoodatplaying,hehaslot'sofgoldconeswhichhavesquareundersides,let'scallthempyramids.Anyo......
  • HDU 5496 Beauty of Sequence
    ProblemDescriptionSequenceisbeautifulandthebeautyofanintegersequenceisdefinedasfollows:removesallbutthefirstelementfromeveryconse......
  • HDU 5433 Xiao Ming climbing
    ProblemDescriptionDuetothecursemadebythedevil,XiaoMingisstrandedonamountainandcanhardlyescape.Thismountainisprettystrangethat......
  • HDU 1542 Atlantis
    ProblemDescriptionThereareseveralancientGreektextsthatcontaindescriptionsofthefabledislandAtlantis.Someofthesetextsevenincludemaps......
  • HDU 3308 LCIS
    ProblemDescriptionGivennintegers.Youhavetwooperations:UAB:replacetheAthnumberbyB.(indexcountingfrom0)QAB:outputthelength......
  • HDU 5874 Friends and Enemies
    ProblemDescriptionOnanisolatedisland,livedsomedwarves.Aking(notadwarf)ruledtheislandandtheseasnearby,thereareabundantcobblestones......
  • HDU 5876 Sparse Graph
    ProblemDescriptioncomplement ofagraph G isagraph H onthesameverticessuchthattwodistinctverticesof H areadjacentifand......
  • HDU 5877 Weak Pair
    ProblemDescriptionrooted treeof N nodes,labeledfrom1to N.Tothe ithnodeanon-negativevalue ai isassigned.An order......
  • HDU 5875 Function
    ProblemDescriptionTheshorter,thesimpler.Withthisproblem,youshouldbeconvincedofthistruth.    Youaregivenanarray A of ......