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++)
    {
        sum+=1.0/double(i);
        if(i%50==0)
        a[i/50]=sum;
    }
    cin>>t;
    while(t--)
    {
        cin>>n;
        int b=n/50;
        double ans=a[b];
        for(i=b*50+1;i<=n;i++)
        ans+=1.0/double(i);
        printf("Case %d: %.10lf\n",k++,ans);
    }

    return 0;
}
时间: 2024-08-02 11:00:21

LightOJ 1234 Harmonic Number的相关文章

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

[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

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 1245 Harmonic Number (II)(简单数论)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:求f(n)=n/1+n/2.....n/n,其中n/i保留整数 显然一眼看不出什么规律.而且n有2e31直接暴力肯定要出事情 但是f=n/x这个函数很好关于y = x 对称对称点刚好是sqrt(n) 于是就简单了直接求sum+n/i (i*i<n && i >=1) 然后乘以2,再减去i*i即可. 这个i*i表示的是什么呢,由于对称上半部份的值完

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 1245 Harmonic Number (II) 水题

分析:一段区间的整数除法得到的结果肯定是相等的,然后找就行了,每次是循环一段区间,暴力 #include <cstdio> #include <iostream> #include <ctime> #include <vector> #include <cmath> #include <map> #include <queue> #include <algorithm> #include <cstring

LightOJ - 1245 Harmonic Number (II) (找规律)

题目大意:给出一个n(1 <= n < 2^31)求出H(n)的结果,H(n)的定义为下: 分析:对于一个n,设 t = n / i: 满足 t >= 1的有多少个呢? 有 n / 1 个. 满足 t >= 2的有多少个呢? 有 n / 2 个. -- 满足 t >= k的有多少个呢? 有 n / k 个. 以上结论不难发现,我们再进一步就能发现: 满足 t == 1 的有 n/1 - n/2 个 满足 t == 2 的有 n/2 - n/3 个 -- 满足 t == k 的