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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值