洛谷 P2759 奇怪的函数

题目描述

使得 x^x 达到或超过 n 位数字的最小正整数 x 是多少?

输入输出格式

输入格式:

 

一个正整数 n

 

输出格式:

 

使得 x^x 达到 n 位数字的最小正整数 x

 

输入输出样例

输入样例#1:
11
输出样例#1:
10

说明

n<=2000000000

思路:根据换底公式 可以推得,当x*log10(x)==n-1时x^x恰好为n位数,所以二分查找即可,在特判一下为1的情况。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,mid;
int main(){
    //freopen("Lemon_Soda.in","r",stdin);
    //freopen("Lemon_Soda.out","w",stdout);
    scanf("%d",&n);
    int l=0,r=1000000000,prel,prer;
    if(n==1){
        cout<<1;
        return 0;
    }
    while(l<r){
        prel=l;prer=r;
        int mid=(l+r)/2;
        if(mid*log10(mid)<n-1)    l=mid;
        else if(mid*log10(mid)>n-1)    r=mid;
        if(l==prel&&r==prer)    break;
    }
    cout<<prer;
}
细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
原文地址:https://www.cnblogs.com/cangT-Tlan/p/7507019.html