LightOJ1234 Harmonic Number

 1 /*
 2  LightOJ1234 Harmonic Number
 3  http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1234
 4  打表 分块
 5  由于只有加法运算,1e8时间是可以承受的。
 6  然而空间无法承受,于是以50个单位为一块进行分块。
 7  */
 8 #include <cstdio>
 9 #include <algorithm>
10 #include <cstring>
11 #include <cmath>
12 #include <vector>
13 #include <queue>
14 #include <iostream>
15 #include <map>
16 #include <set>
17 //#define test
18 using namespace std;
19 #ifdef old
20 const int Nmax=1e6+7;
21 double f[Nmax];
22 #endif
23 const int Nmax=1e8+5;
24 const double eps=1e-9;
25 double f[Nmax/50+5];
26 //double get_f(int n)
27 //{
28     //if(n<Nmax)
29         //return f[n];
30     //return 1.0/n+get_f(n-1);
31 //}
32 #ifdef old
33 map<int ,double> mp;
34 map<int ,double>::iterator it;
35 double get_f(int n)
36 {
37     if(n<Nmax)
38         return f[n];
39     it=mp.find(n);
40     if(it!=mp.end())
41         return mp[n];
42     it=mp.upper_bound(n);
43     if(it!=mp.begin())
44         it--;
45     //printf("it->n:%d\n,it->first);
46     double ans=it->second;
47     for(int i=it->first+1;i<=n;i++)
48     {
49         ans+=1.0/i;
50     }
51     mp[n]=ans;
52     return ans;
53 }
54 #endif
55 int main()
56 {
57     #ifdef test
58     #endif
59     //freopen("loj1234.in","r",stdin);
60     //freopen("tras.out","w",stdout);
61 #ifdef old
62     f[1]=1.0;
63     for(int i=2;i<Nmax;i++)
64        f[i]=f[i-1]+1.0/i;
65 #endif
66     //for(int i=1;i<=15;i++)
67         //printf("%lf\n",f[i]);
68 #ifdef old
69     mp[Nmax-1]=f[Nmax-1];
70 #endif
71     double tmp=0.0;
72     for(int i=1;i<=1e8;i++)
73     {
74         tmp+=1.0/i;
75         if(i%50==0)
76             f[i/50]=tmp;
77     }
78     int n;
79     int t;
80     scanf("%d",&t);
81     t=0;
82     while(scanf("%d",&n)==1)
83     {
84         t++;
85         printf("Case %d: ",t);
86         double ans=0.0;
87         ans=f[n/50];
88         for(int i=n/50*50+1;i<=n;i++)
89             ans+=1.0/i;
90         printf("%.8lf\n",ans+eps);
91 #ifdef old
92         printf("%.8lf\n",get_f(n)+eps);
93 #endif
94
95     }
96     return 0;
97 }
时间: 2024-08-01 10:41:17

LightOJ1234 Harmonic Number的相关文章

1245 - Harmonic Number (II)(规律题)

1245 - Harmonic Number (II)   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB I was trying to solve problem '1234 - Harmonic Number', I wrote the following code long long H( int n ) {     long long res = 0;     for( int i =

[2016-04-21][light]OJ[1234][Harmonic Number]

时间:2016-04-21 22:18:26 星期四 题目编号:[2016-04-21][light]OJ[1234][Harmonic Number] 题目大意:求∑nk=11kn∈(1,108),精确到10?8求∑k=1n1kn∈(1,108),精确到10?8 分析: 想法是打表,然后输出,但是直接打表会爆内存 解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值 对应的整百就是n

LightOJ 1234 Harmonic Number (打表)

Harmonic Number Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1234 Description In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers: In this p

Light OJ 1234 Harmonic Number 调和级数部分和

题目来源:Light OJ 1234  Harmonic Number 题意: 思路:没思路啊 这个是高数的东西 发散 n足够大时它无穷大 直接公式解 #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <algorithm> #include <iostream> using namespace std; const int

LightOJ 1234 Harmonic Number(打表 + 技巧)

http://lightoj.com/volume_showproblem.php?problem=1234 Harmonic Number Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1234 Description In mathematics, the nth harmonic number is the sum of th

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. InputInput starts with an integer T (≤ 10000), denoting the number of test cases. Each case s

LightOJ - 1245 Harmonic Number (II) 求同值区间的和

题目大意:对下列代码进行优化 long long H( int n ) {    long long res = 0;    for( int i = 1; i <= n; i++ )        res = res + n / i;    return res;} 题目思路:为了避免超时,要想办法进行优化 以9为例: 9/1 = 9 9/2 = 4 9/3 = 3 9/4 = 2 9/5 = 1 9/6 = 1 9/7 = 1 9/8 = 1 9/9 = 1 拿1来看,同为1的区间长度为:9

LightOJ 1234 Harmonic Number

思路:直接暴力会超内存 只存n/50个数就可以了 需要哪个数再算就好了 #include <iostream> #include <math.h> #include <stdio.h> const int N=1e8+10; using namespace std; double a[N/50+10]; int main() { int i,n,t,k=1; double sum=1.0; a[0]=0.0; a[1]=1.0; for(i=2;i<=N;i++)

1245 - Harmonic Number (II)---LightOJ1245

http://lightoj.com/volume_showproblem.php?problem=1245 题目大意:一个数n除以1到n之和 分析:暴力肯定不行,我们可以先求1~sqrt(n)之间的每个数的个数,然后再求n除以1~sqrt(n)之间的数的和 这样算下来就只有2*sqrt(n)的复杂度 最后还要排除多加的,. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algor