P1943 LocalMaxima

P1943 LocalMaxima

首先需要推出柿子或者找规律得出:答案就是调和级数。

打表过后发现当 (n) 超过 (10^7) 的时候保留 8 位的答案都是可以近似 (0.57721566490153286060651209) + (logn)

具体证明见本题题解。

代码:

#include<bits/stdc++.h>
const int Block=1e6;
using namespace std;
template <typename T>
inline void read(T &x){
	x=0;char ch=getchar();bool f=false;
	while(!isdigit(ch)){if(ch=='-'){f=true;}ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	x=f?-x:x;
	return ;
}
template <typename T>
inline void write(T x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10^48);
	return ;
}
int n;
double ans;
int main(){
    read(n);
    if(n>10000000) ans=log(n)+0.57721566490153286060651209;
    else for(int i=1;i<=n;i++) ans+=1.0/i*1.0;
    printf("%.8lf",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/Akmaey/p/14668749.html