HDU5752 Sqrt Bo

  题目链接如下:http://acm.hdu.edu.cn/showproblem.php?pid=5752

  题意如下:

  

Problem Description
Let's define the function f(n)=n√.

Bo wanted to know the minimum number y which satisfies fy(n)=1.

note:f1(n)=f(n),fy(n)=f(fy1(n))

It is a pity that Bo can only use 1 unit of time to calculate this function each time.

And Bo is impatient, he cannot stand waiting for longer than 5 units of time.

So Bo wants to know if he can solve this problem in 5 units of time.
 
Input
This problem has multi test cases(no more than 120).

Each test case contains a non-negative integer n(n<10100).
 
Output
For each test case print a integer - the answer y or a string "TAT" - Bo can't solve this problem.
 
 
分析:由于他说执行开方操作小于等于5次, 因此我们可以得出五次以内开方最终结果为1的范围是[1, 2^32), 然后暴力模拟即可。代码如下:
/*************************************************************************
    > File Name: HDU5752.cpp
    > Author:
    > Mail:
    > Created Time: 2016年07月28日 星期四 19时40分42秒
 ************************************************************************/

#include <cstdio>
#include <cmath>
#include <cstring>

using namespace std;
typedef long long LL;
char input[1000];
int len;

LL trans(char a[])
{
    LL res = 0;
    for(int i=0; i<len; i++)
    {
        res = res*10 + input[i] - '0';
    }
    return res;
}

int main()
{
    while(scanf("%s", input) != EOF)
    {
        len = strlen(input);
        if(len > 10 || (len==1 && input[0]=='0')) printf("TAT
");
        else
        {
            LL num = trans(input);
            int res = 0;
            while(num != 1)
            {
                num = sqrt(num);
                res++;
            }
            if(res <= 5)
                printf("%d
", res);
            else
                printf("TAT
");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xingxing1024/p/5716088.html