【Luogu】 P1943 LocalMaxima

题目链接

点击打开链接

题目解法

考虑数 x x x 比前面所有数都大的期望,即在数列 x + 1 , . . . , n x+1,...,n x+1,...,n 中排名最小的期望
( n − x ) ! ( n − x + 1 ) ! = 1 n − x + 1 \frac{(n-x)!}{(n-x+1)!}=\frac{1}{n-x+1} (nx+1)!(nx)!=nx+11
考虑 a n s = ∑ 1 n − i + 1 = ∑ i = 1 n 1 i ans=\sum{\frac{1}{n-i+1}}=\sum_{i=1}^{n}{\frac{1}{i}} ans=ni+11=i=1ni1
n n n 小的时候可以暴力计算
n n n 大时,考虑到答案是调和级数,大概为 l n ( n ) + G a m m a ln(n)+Gamma ln(n)+Gamma G a m m a Gamma Gamma 为欧拉常数,这里可以取 0.57721566490153285 0.57721566490153285 0.57721566490153285

#include <bits/stdc++.h>
using namespace std;
const double Gamma=0.57721566490153285;
inline int read(){
	int FF=0,RR=1;
	char ch=getchar();
	for(;!isdigit(ch);ch=getchar()) if(ch=='-') RR=-1;
	for(;isdigit(ch);ch=getchar()) FF=(FF<<1)+(FF<<3)+ch-48;
	return FF*RR;
}
int main(){
	int n=read();
	double ans=0;
	if(n<=1e8) for(int i=1;i<=n;i++) ans+=1.0/i; 
	else ans=log(n)+Gamma;
	printf("%.8lf",ans);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值