您的位置:首页 > 其它

洛谷OJ - P2759 - 奇怪的函数(二分答案)

2017-05-16 21:34 288 查看
题目描述
使得 x^x 达到或超过 n 位数字的最小正整数 x 是多少?
输入一个正整数 n
输出使得 x^x 达到 n 位数字的最小正整数 x
样例输入
11


样例输出
10


题目思路
将题目翻译成公式即为:x^x >= 10^(n-1) 对两边取对数得到 x*log10(x) >= n-1 那么我们只要枚举 x 得到最小的x即可,由于数据量的问题,我们采用二分法快速找到最小的x。

题目代码
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;
int n, x;
int l, r, mid;

bool check(int a){
return a*log10(a) >= n-1;
}

int main(){

while(scanf("%d",&n) != EOF){
l = 1; r = 1000000000;
while(l < r){
mid = (l+r) >> 1;

if(check(mid))
r = mid;
else
l = mid + 1;
}

printf("%d\n",r);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: