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 kk≤n.
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 kk≤n in a line.
Sample Input
14
Sample Output
12
Source
问题简述:(略)
问题分析:
这个问题的关键是计算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;
}