LightOJ-1234-Harmonic Number-调和级数+欧拉常数 / 直接打表
In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers:

In this problem, you are given n, you have to find Hn.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 108).
Output
For each case, print the case number and the nth harmonic number. Errors less than 10-8 will be ignored.
Sample Input
12
1
2
3
4
5
6
7
8
9
90000000
99999999
100000000
Sample Output
Case 1: 1
Case 2: 1.5
Case 3: 1.8333333333
Case 4: 2.0833333333
Case 5: 2.2833333333
Case 6: 2.450
Case 7: 2.5928571429
Case 8: 2.7178571429
Case 9: 2.8289682540
Case 10: 18.8925358988
Case 11: 18.9978964039
Case 12: 18.9978964139
解法一:
调和级数(即f(n))至今没有一个完全正确的公式,但欧拉给出过一个近似公式:
当n很大时:
f(n)≈ln(n)+C+1(2*n)
欧拉常数值:C≈0.57721566490153286060651209
c++ math库中,log即为ln。
当n很小时:
直接求,此时公式不是很准。
#include<stdio.h>
#include<cmath>
#include<string.h>
typedef long long ll;
using namespace std; const double c=0.57721566490153286060651209;
//f(n)≈ln(n)+C+1/(2*n) double sum[]; void fn()
{
sum[]=1.0;
for(int i=; i<=; i++)
{
sum[i]=sum[i-]+1.0/(i*1.0);
}
}
int main()
{
fn();
int t;
scanf("%d",&t);
int n,tt=;
while(t--)
{
scanf("%d",&n);
if(n<=)
printf("Case %d: %.10lf\n",tt++,sum[n]);
else
{
double x=log(n)+c+1.0/(2.0*n);
printf("Case %d: %.10f\n",tt++,x);
}
}
return ;
}
解法二:
如果公式记不住可以选择打表,10e8全打表必定MLE,而每40个数记录一个结果,即分别记录1/40,1/80,1/120,...,1/10e8,这样对于输入的每个n,最多只需执行39次运算,大大节省了时间,空间上也够了。(可以学习一下这种每40个数记录一个结果的思想,免去了执行很大运算量的操作。)
#include<stdio.h>
#include<cmath>
#include<string.h>
typedef long long ll;
using namespace std; double a[]; void fn()
{
double sum=0.0;
for(int i=; i<; i++)
{
sum+=1.0/i;
if(i%==)
{
a[i/]=sum;
}
}
}
int main()
{
fn();
int t;
scanf("%d",&t);
int n,tt=;
while(t--)
{
scanf("%d",&n);
double ans=a[n/];
for(int i=*(n/)+; i<=n; i++)
{
ans+=1.0/i;
}
printf("Case %d: %.10f\n",tt++,ans);
}
return ;
}
参考博客:https://www.cnblogs.com/shentr/p/5296462.html
LightOJ-1234-Harmonic Number-调和级数+欧拉常数 / 直接打表的更多相关文章
- LightOJ 1234 Harmonic Number 调和级数部分和
题目链接:http://lightoj.com/volume_showproblem.php?problem=1234 Sample Input Sample Output Case : Case : ...
- LightOJ 1234 Harmonic Number(打表 + 技巧)
http://lightoj.com/volume_showproblem.php?problem=1234 Harmonic Number Time Limit:3000MS Memory ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- LightOJ 1234 Harmonic Number
D - Harmonic Number Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu S ...
- LightOJ 1234 Harmonic Number (打表)
Harmonic Number Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submi ...
- C - Harmonic Number(调和级数+欧拉常数)
In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers ...
- LightOJ - 1234 LightOJ - 1245 Harmonic Number(欧拉系数+调和级数)
Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...
- LightOJ 1245 Harmonic Number (II)(找规律)
http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS ...
- LightOJ - 1245 - Harmonic Number (II)(数学)
链接: https://vjudge.net/problem/LightOJ-1245 题意: I was trying to solve problem '1234 - Harmonic Numbe ...
- Harmonic Number (LightOJ 1234)(调和级数 或者 区块储存答案)
题解:隔一段数字存一个答案,在查询时,只要找到距离n最近而且小于n的存答案值,再把剩余的暴力跑一遍就可以. #include <bits/stdc++.h> using namespace ...
随机推荐
- Java设计模式思想(单列模式,工厂模式,策略模式)
a) 单例模式:单例模式核心只需要new一个实例对象的模式,比如数据库连接,在线人数等,一些网站上看到的在线人数统计就是通过单例模式实现的,把一个计时器存放在数据库或者内存中,当有人登陆的时候取出来加 ...
- Vue.js 复选框
demo <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- leetcood学习笔记-167-两数之和 II - 输入有序数组
题目描述: 第一次提交: class Solution(object): def twoSum(self, numbers, target): """ :type num ...
- Spring Boot学习笔记一
Spring Boot简介 前言:本章简单介绍Spring boot的使用. (第二天springboot的学习之路:https://www.cnblogs.com/LBJLAKERS/p/12003 ...
- Jmeter-【JSON Extractor】-响应结果中一级key取值
一.请求返回样式 二.取code值 三.查看结果
- MapReduce分区数据倾斜
什么是数据倾斜? 数据不可避免的出现离群值,并导致数据倾斜,数据倾斜会显著的拖慢MR的执行速度 常见数据倾斜有以下几类 1.数据频率倾斜 某一个区域的数据量要远远大于其他区域 2.数据大小倾斜 ...
- 23 Pro/E二次开发中的问题记录
0 引言 由于项目中涉及到Pro/E的二次开发技术,因此在边用边学的情况下,解决了不少问题,也积攒了不少问题.其中有些问题可能不是调个函数就能搞定的,得了解CAD底层的东西. 1 问题描述 (1)CA ...
- NX二次开发-UFUN多选菜单对话框uc1605
NX11+VS2013 #include <uf.h> #include <uf_ui.h> UF_initialize(); //多选菜单对话框 char sPromptSt ...
- python入门 集合(四)
集合 集合是一个无序的不重复元素序列,可以迭代,也可以修改.集合迭代的时候元素是随机的. 集合通常用来 membership testing, 去重, 也可以用来求交集并集补集. 介绍一下如何创建集合 ...
- SecureCRT是最常用的终端仿真程序,简单的说就是Windows下登录UNIX或Liunx服务器主机的软件,本文主要介绍SecureCRT的使用方法和技巧
SecureCRT是最常用的终端仿真程序,简单的说就是Windows下登录UNIX或Liunx服务器主机的软件,本文主要介绍SecureCRT的使用方法和技巧 VanDyke CRT 和 VanDyk ...