奇怪的函数 题解

Description:

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

Input:

一个正整数 n

Output:

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

Example Input:

11

Example Output

10

Data Range:

n <= 2000000000 (2e9)


Difficulty: ★★☆☆☆

Thoughts:

看到如此之大的数据范围,显然强行计算x^x的位数是不现实的,不仅需要高精度,而且效率极低。
题目的核心在于如何计算x^x的位数,可以利用对数的方法,log10(x)就是x的位数。
也就是 log10(x ^ x) >= n - 1
如此判定方法就是简单了,代码形式就是 x * log(x) / log(10)(换底公式) >= n - 1
求最小,很明显是二分搜索,二分套一个判断函数就完成了此题。


AC代码:

// Skq_Liao

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (register int i = (a), i##_end_ = (b); i < i##_end_; ++i)
#define ROF(i, a, b) for (register int i = (a), i##_end_ = (b); i > i##_end_; --i)
#define debug(...) fprintf(stderr, __VA_ARGS__)

const int MAXN = 1000000000;

bool CountFigure(long long x, int n)
{
	return x * (log(x) / log(10)) >= n - 1;
}

void BinarySearch(int l, int r, int n)
{
	int ans;
	while(l <= r)
	{
		int mid = (l + r) >> 1;
		bool cur = CountFigure(1ll * mid, n);
		if(cur)
		{
			r = mid - 1;
			ans = mid;
		}
		else
			l = mid + 1;
	}
	printf("%d\n", ans);
	return ;
}

int main()
{
#define Bxy
#ifdef Bxy
	freopen("test.in", "r", stdin);
	freopen("test.out", "w", stdout);
#endif
	int n;
	scanf("%d", &n); 
	BinarySearch(0, MAXN, n);
	return 0;
}

总结:

此题的核心就是判断位数,碰到一次后就没有任何难度了。

Skq_Liao 2017/06/29 18 : 05 于209

posted @ 2017-06-29 18:10  箜瑟_qi  阅读(349)  评论(0)    收藏  举报