A Math Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 318 Accepted Submission(s): 152
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
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
1 4
Sample Output
1 2
Source
Recommend
题意:
给出n,求满足k^k<=n的k值的个数,其中k为正整数。
分析:
1.因为k值范围比较小,可以暴力枚举,从1开始枚举找出所有满足条件的k值,或者从15倒着枚举。(特别注意边界!用计算机自带的计算器可计算出k最大为15,此时为k^k为18位数)坑啊,wa了好几次,怪自己考虑不周全。
2.快速幂。
代码:
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
LL quickmul(int k){
LL ans=1;
long long n=k;
while(k){
if(k&1) ans*=n;
k>>=1;
n*=n;
}
return ans;
}
int main(){
LL n;
while(cin>>n){
int i;
for(i=15;i>0;i--)///注意边界,否则会爆long long
if(quickmul(i)<=n)
break;
cout<<i<<endl;
}
}