【問題描述】
使得x^x達到或超過n位數字的最小正整數x是多少?
【輸入格式】
輸入一個正整數n。
【輸出格式】
輸出使得x^x達到n位數字的最小正整數x。
【輸入樣例】
11
【輸出樣例】
10
【數據範圍】
n<=2 000 000 000
題目要求求x^x的位數爲n時最小的x,由於一個正整數的位數n=floor(logx+1)(log取常用對數),所以說x^x的位數是floor(log(x^x)+1),由對數運算可知floor(log(x^x)+1)=floor(x*logx+1)。
二分猜x,帶入上式檢驗,如果x^x的爲位數大於了n,說明x猜大了,應該在小的部分猜,否則在大的區間猜。最後的答案就是x。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const LL maxn=2000000000;
int n;
double check(LL m)
{
if(m==0)return 0;
return floor((double)m*log10(m)/1.0)+1;
}
int main()
{
scanf("%d",&n);
LL A=0,B=maxn,Ans;
for(int i=0;i<70;i++)//二分猜x
{
LL m=A+B>>1;
if(check(m)>=n)B=m,Ans=B;
else A=m;
}
cout<<Ans<<endl;
return 0;
}