洛谷T2062 随机数生成器

作者:

在

,

$$ \text{题意:} \\ \text{求}f\left( n \right) =1+\frac{1}{n}\sum_{1\le i\le n}{f\left( i \right)} \\ \text{设}F\left( n \right) =\sum_{1\le i\le n}{f\left( i \right)} \\ f\left( n \right) =1+\frac{1}{n}F\left( n \right) \\ nf\left( n \right) =n+F\left( n \right) \\ \left( n-1 \right) f\left( n-1 \right) =n-1+F\left( n-1 \right) \\ \text{相减} \\ nf\left( n \right) -\left( n-1 \right) f\left( n-1 \right) =n-\left( n-1 \right) +F\left( n \right) -F\left( n-1 \right) \\ nf\left( n \right) -nf\left( n-1 \right) +f\left( n-1 \right) =1+f\left( n \right) \\ \left( 1-n \right) f\left( n-1 \right) =1+\left( 1-n \right) f\left( n \right) \\ f\left( n \right) +\frac{1}{1-n}=f\left( n-1 \right) \\ f\left( n \right) =\frac{1}{n-1}+f\left( n-1 \right) \\ \text{规定}f\left( 1 \right) =1 \\ \text{则}f\left( n \right) =1+\sum_{1\le i<n}{\frac{1}{i}} \\ n\text{较小时暴力计算,}n\text{较大时直接近似} \\ $$

#pragma GCC target("avx,sse,sse2")
#pragma GCC optimize("Ofast")
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <iomanip>
#include <cmath>
typedef long long int int_t;
typedef long double real_t;

using std::cin;
using std::endl;
using std::cout;

const real_t GAMMA = 0.57721566490153286060651209;

int main(){
    real_t f = 1;
    int_t n;
    cin >> n;
    n -= 1;
    if(n == 0){
        f = 0;
    }else if(n <= 5e7){
        for(int_t i = 1;i <= n;i ++){
            f += (real_t) 1 / i;
        }
    }else{
        f += logl(n) + GAMMA;
    }
    std::cout.setf(std::ios::fixed);
    cout << std::setprecision(5) << f << endl;
    
	return 0;
}

 

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。