本文参考自: 原文地址
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<=10^18
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 4Sample Output
1 2
题解:
我都不忍心说这题有什么算法,以你们最朴素的程序知识,枚举一个k,掰掰手指头就知道k<=15,然后根据题意算出k^k满足k^k<=n就行了。
如果非要将这题搞得复杂一点,也有如下几种解题方法:
1.打表,a[i]=i^i(i<=15),若n>=a[i-1] && n<a[i] 则k=i;
2.快速幂,个人感觉没有必要,除非秀算法,如果k是偶数 k^k=(k^(k/2))*(k^(k/2))
否则 k^k=(k^(k/2))*(k^(k/2))*k
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
unsigned long long m,n,k;
while(cin>>n)
{
for(k=1;k<=15;++k)
{
m=k;
for(int i=1;i<k;++i)
{
m*=k;
}
if(m>n) break;
}
cout<<k-1<<endl;
}
return 0;
}