HDU6182 A Math Problem【暴力+快速幂】

 

A Math Problem

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4151    Accepted Submission(s): 1309

 

 

Problem Description

You are given a positive integer n, please count how many positive integers k satisfy kkn.

 

 

Input

There are no more than 50 test cases.

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

1≤n≤1018

 

 

Output

For each test case, output an integer indicates the number of positive integers k satisfy kkn in a line.

 

 

Sample Input

 

14

 

 

Sample Output

 

12

 

 

Source

2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)

 

 

 

问题链接HDU6182 A Math Problem

问题简述:(略)

问题分析

  这个问题的关键是计算k^k的值的范围,k=16大概已经超过10^18,所以只需要k在1-15范围内暴力一下就可以了。

  还有一个方法是,使用快速幂计算函数,计算速度稍微快一些。

程序说明

  计算过程中还是要用long long类型进行计算方能防止溢出导致精度丢失。

  程序中的N值应该是多少,可以事先编写一个程序计算一下k^k就知道了。

  2个程序中的条件i<N也许可以没有,但是那样的话程序逻辑不够严密。

  这里给出2个解法程序。

题记:(略)

参考链接:(略)

 

AC的C++语言程序如下:

/* HDU6182 A Math Problem */

#include <iostream>

using namespace std;

typedef unsigned long long ULL;

const int N = 15;

int main()
{
    ULL n, kk;

    while(cin >> n) {
        int i;
        for(i = 1; i <= N; i++) {
            kk = i;
            for(int j = 1;  j < i; j++)
                kk *= i;
            if(kk > n)
                break;
        }

        cout << i - 1 << endl;
    }

    return 0;
}

 

 

 

 

AC的C++语言程序如下:

/* HDU6182 A Math Problem */

#include <iostream>

using namespace std;

typedef unsigned long long ULL;

const int N = 15;

// 快速幂
ULL mypow(ULL x, ULL n)
{
    ULL result = 1;
    for(; n; n>>=1) {
        if(n & 1)
            result *= x;
        x *= x;
    }

    return result;
}

int main()
{
    ULL n;

    while(cin >> n) {
        int i;
        for(i = 1; i <= N; i++)
            if(mypow(i, i) > n)
                break;

        cout << i - 1 << endl;
    }

    return 0;
}

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值