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

该博客探讨了一道编程题目,涉及寻找满足条件 x^x >= 10^(n-1) 的最小整数 x。通过转换为 x*log10(x) >= n-1,博主建议使用二分搜索算法在给定的数据规模下高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述
使得 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值