HDU - 6182 - A Math Problem (枚举 & 打表)

HDU - 6182 - A Math Problem

You are given a positive integer n, please count how many positive integers k satisfy kk≤nkk≤n.
Input
There are no more than 50 test cases.

Each case only contains a positivse integer n in a line.

1≤n≤10181≤n≤1018
Output
For each test case, output an integer indicates the number of positive integers k satisfy kk≤nkk≤n in a line.
Sample Input
1
4
Sample Output
1
2
题目链接

这个题目就是让你求,给定的n,有多少个数k使得k的k次方小于等于n。这个题目的话毕竟是k的k次幂,也是很可怕的指数爆炸,所以,k应该不会太大,所以你写个快速幂,判断一下大于10的18次幂要k等于几就行了,可以直接遍历了。应该是15,到16的时候超了,会出错。

#include <cstdio>
#define ll long long
using namespace std;

ll q_pow(ll a, ll b)
{
    ll ans = 1;
    while(b)
    {
        if(b & 1)   ans *= a;
        a *= a;
        b >>= 1;
    }
    return ans;
}

int main()
{
    ll n;
    while(~scanf("%lld", &n))
    {
        for(ll i = 1; i <= 20; i++)
        {
            ll ans = q_pow(i, i);
            if(ans > n)
            {
                if(i > 15)  printf("15\n");
                else    printf("%lld\n", i - 1);
                break ;
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40788897/article/details/81570058