[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的空间,然后从那个值开始计算到当前值
    • 对应的整百就是n100,那么那个值对应的位置就是n100×100对应的整百就是n100,那么那个值对应的位置就是n100×100,
  1. #include<cstdio>
  2. using namespace std;
  3. const int maxn = 1E8 + 10;
  4. double a[maxn / 100 + 10];
  5. void ini(){
  6. double cur = 0;
  7. for(int i = 1 ; i <= 1E8 ;++i ){
  8. cur += double(1)/i;
  9. if(i % 100 == 0) a[i/100] = cur;
  10. }
  11. }
  12. int main(){
  13. ini();
  14. int t,cntcase = 0;
  15. scanf("%d",&t);
  16. while(t--){
  17. int n;
  18. scanf("%d",&n);
  19. double cur = a[n / 100];
  20. for(int i = n / 100 * 100 + 1;i <= n ; ++i){
  21. cur += double(1) / i;
  22. }
  23. printf("Case %d: %.10lf\n",++cntcase ,cur);
  24. }
  25. return 0;
  26. }

来自为知笔记(Wiz)

时间: 2024-12-13 09:39:54

[2016-04-21][light]OJ[1234][Harmonic Number]的相关文章

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

Light oj 1234 - Harmonic Number

题目链接:http://lightoj.com/volume_showproblem.php?problem=1234 给你一个数n,让你求 这个要是直接算的话肯定TLE,要是用1e8的数组预处理存储答案也一定MLE. 所以我用1e6的数组存储每100个数的答案,然后每次给你n的时候顶多算99次. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5

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

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

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)(规律题)

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 =

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&

Light OJ 1341 Aladdin and the Flying Carpet

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised hi

light oj 1066Gathering Food (bfs 稍微有点小坑)

1066 - Gathering Food Winter is approaching! The weather is getting colder and days are becoming shorter. The animals take different measures to adjust themselves during this season. - Some of them "migrate." This means they travel to other plac