zoukankan      html  css  js  c++  java
  • 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 = 0;
    ll n;
    int flag = 0;
     
    int haha[100] = {0};
    ll work(ll x,  int i)
    {
     
        if( flag == n)
            return 1;
     
        ll t = x + haha[i];
     
        double tt = pow(double(t),1.0 / 2);
        if(fabs(tt - (ll)(tt)) < 0.000001)
        {
            flag = 0;
            haha[i] = x;
            return 1 + work(x+1,0);
        }
        else
        {
            if(haha[ ( i + 1 ) % n ] == 0)
            {
                haha[i+1] = x;
                flag = 0;
                return 1+work(x + 1, 0);
            }
            else
            {
                flag++;
                return work(x, (i+1)%n);
            }
        }
    }
    int main()
    {
        //freopen("made.txt","w",stdout);
        int T;
        cin>>T;
        while(T--)
        {
            cin>>n;
            haha[0] =  1;
            sum = work(2, 0);
            cout<<sum<<endl;
            memset(haha,0,sizeof(haha));
            flag = 0;
        }
        return 0;
    }
  • 相关阅读:
    2020杭电HDU-6863多校第八场Isomorphic Strings(Hash+学到的新东西)
    CodeForces 1395C-Boboniu and Bit Operations(位运算-暴力)
    洛谷P2585&ZJOI 2006-三色二叉树(树的染色-树形DP)
    洛谷P2016-战略游戏(树的最小点覆盖-树形DP)
    洛谷P2015-二叉苹果树(树形DP)
    洛谷P1352-没有上司的舞会(树形DP)
    CSUSTOJ 4004-你真的会吃零食吗?(打表)
    在线支付--支付宝
    使用rabbitmq rpc 模式
    在kubernetes 集群运行 odoo
  • 原文地址:https://www.cnblogs.com/yqbeyond/p/4435942.html
Copyright © 2011-2022 走看看