【Luogu P5515】[MtOI2019]灵梦的计算器

链接:

洛谷

博客园

题目大意:

求出 \(\lfloor n^a+n^b\rfloor\) 相等时,\(n\) 的上下界的差值。

正文:

如果不取整,函数 \(f(x)=x^a+x^b\) 的图象是这样的:

我们要找的是最大的 \(x_1-x_2\)

现在我们知道 \(f(x_1)-f(x_2)\rightarrow 1\),即它趋向于 \(1\)。我们知道斜率的公式是 \(\frac{y_1-y_2}{x_1-x_2}=\frac{f(x_1)-f(x_2)}{x_1-x_2}\),我们可以通过斜率 \(k\) 倒推 \(x_1-x_2=\frac{f(x_1)-f(x_2)}{k}\)

而求斜率 \(k\),可以求导。设 \(u(x)=x^a,v(x)=x^b\),则 \(f(x)=u(x)+v(x)\)

\[\begin{aligned}k=f'(x)&=u'(x)+v'(x)\\ &=(x^a)'+(x^b)'\\ &=ax^{a-1}+bx^{b-1}\end{aligned}\]

然后答案 \(x_1-x_2=\frac{0.\dot{9}}{k}\)

代码:

inline ll READ()
{
	ll x = 0, f = 1;
	char c = getchar();
	while (c != '-' && (c < '0' || c > '9')) c = getchar();
	if (c == '-') f = -f, c = getchar();
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
	return x * f;
}

namespace Mker
{
//  Powered By Kawashiro_Nitori
//  Made In Gensokyo, Nihon
	#define uint unsigned int
	uint sd;int op;
	inline void init() {scanf("%u %d", &sd, &op);}
	inline uint uint_rand()
	{
		sd ^= sd << 13;
		sd ^= sd >> 7;
		sd ^= sd << 11;
		return sd;
	}
	inline double get_n()
	{
		double x = (double) (uint_rand() % 100000) / 100000;
		return x + 4;
	}
	inline double get_k()
	{
		double x = (double) (uint_rand() % 100000) / 100000;
		return (x + 1) * 5;
	}
	inline void read(double &n,double &a, double &b)
	{
		n = get_n(); a = get_k();
		if (op) b = a;
		else b = get_k(); 
	}
}

int t; 
double n, a, b, k, ans;

int main()
{
	t = READ();
	Mker::init();
	for (; t--; )
	{
		Mker::read(n, a, b);
		k = a * pow(n, a - 1) + b * pow(n, b - 1);
		ans += 0.9999999 / k;
	}
	printf ("%.5lf", ans);
	return 0;
}
posted @ 2021-05-16 16:12  Jayun  阅读(79)  评论(0编辑  收藏  举报