http://lightoj.com/volume_showproblem.php?problem=1245

G - Harmonic Number (II)

Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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 = 1; i <= n; i++ )
        res = res + n / i;
    return res;
}

Yes, my error was that I was using the integer divisions only. However, you are given n, you have to find H(n) as in my code.

Input

Input starts with an integer T (≤ 1000), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n < 231).

Output

For each case, print the case number and H(n) calculated by the code.

Sample Input

11

1

2

3

4

5

6

7

8

9

10

2147483647

Sample Output

Case 1: 1

Case 2: 3

Case 3: 5

Case 4: 8

Case 5: 10

Case 6: 14

Case 7: 16

Case 8: 20

Case 9: 23

Case 10: 27

Case 11: 46475828386

根据题中的代码便可知道题意,题意不多说
 
先看两个例子
1.
n = 10    sqrt(10) = 3     10/sqrt(10) = 3
i        1   2   3         4   5   6   7   8   9   10
n/i    10  5   3         2   2   1   1   1   1    1
 
m =  n/i
sum += m;
m = 1的个数10/1-10/2 = 5;
m = 2的个数10/2-10/3 = 2;
m = 3的个数10/3-10/4 = 1;
 
2.
n = 20     sqrt(20) = 4     20/sqrt(20) = 5
i        1   2   3   4       5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20
n/i    20  10 6   5       4   3   2   2   2    2     1     1     1     1     1     1     1     1    1    1
 
m =  n/i
sum += m;
m = 1的个数20/1-20/2 = 10;
m = 2的个数20/2-20/3 = 4;
m = 3的个数20/3-20/4 = 1;
m = 4的个数20/4-20/5 = 1;
...
m = i的个数20/i - 20/(i + 1)(1<= i <= sqrt(n))
 
这样我们可以得出:sqrt(n)之前的数我们可以直接用for循环来求
sqrt(n)之后的sum += (n/i - n/(i + 1)) * i;
当sqrt(n) = n / sqrt(n)时(如第一个例子10,sum就多加了一个3),sum多加了一个sqrt(n),减去即可;
 
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std;
const int N = ;
typedef long long ll; int main()
{
int t, n, p = ;
ll sum;
scanf("%d", &t);
while(t--)
{
sum = ;
p++;
scanf("%d", &n);
int m = sqrt(n);
for(int i = ; i <= m ; i++)
sum += n / i;
for(int i = ; i <= m; i++)
sum += (n / i - n / (i + )) * i;
if(m == n / m)
sum -= m;
printf("Case %d: %lld\n", p, sum);
}
return ;
}
 

LightOJ 1245 Harmonic Number (II)(找规律)的更多相关文章

  1. LightOj 1245 --- Harmonic Number (II)找规律

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1245 题意就是求 n/i (1<=i<=n) 的取整的和这就是到找规律的题 ...

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

    1245 - Harmonic Number (II)   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 3 ...

  3. G - Harmonic Number (II) 找规律--> 给定一个数n,求n除以1~n这n个数的和。n达到2^31 - 1;

    /** 题目:G - Harmonic Number (II) 链接:https://vjudge.net/contest/154246#problem/G 题意:给定一个数n,求n除以1~n这n个数 ...

  4. LightOJ - 1245 - Harmonic Number (II)(数学)

    链接: https://vjudge.net/problem/LightOJ-1245 题意: I was trying to solve problem '1234 - Harmonic Numbe ...

  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保留整数 显 ...

  6. LightOJ 1245 - Harmonic Number (II)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:仿照上面那题他想求这么个公式的数.但是递归太慢啦.让你找公式咯. ...

  7. LightOJ 1245 Harmonic Number (II) 水题

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

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

    题目大意:对下列代码进行优化 long long H( int n ) {    long long res = 0;    for( int i = 1; i <= n; i++ )      ...

  9. LightOJ - 1234 LightOJ - 1245 Harmonic Number(欧拉系数+调和级数)

    Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...

随机推荐

  1. 淘宝主搜索离线集群完成Hadoop 2

    淘宝搜索离线dump集群(hadoop&hbase)2013进行了几次重大升级,本文中将这些升级的详细过程.升级中所遇到的问题以及这些问题的解决方案分享给大家.至此,淘宝主搜索离线集群完全进入 ...

  2. 极大似然估计&最大后验概率估计

    https://guangchun.wordpress.com/2011/10/13/ml-bayes-map/ http://www.mi.fu-berlin.de/wiki/pub/ABI/Gen ...

  3. 【英语】Bingo口语笔记(23) - 万圣节系列

    jack-o-lantern 杰克灯(南瓜灯) spooky 幽灵般的

  4. 【Python】Python重新学习

    <python基础教程(第二版)> http://www.cnblogs.com/fnng/category/454439.html 分片(后面取的是前一位) eg: >>&g ...

  5. Windows Server 2003 激活码及激活方法

    Windows Server 2003 简体中文企业版,真正免激活. CD-KEY:JB88F-WT2Q3-DPXTT-Y8GHG-7YYQY 安装序列号:JCGMJ-TC669-KCBG7-HB8X ...

  6. Java中传值与传引用

    不管Java参数类型是什么,一律传递参数的副本. <Thinking In Java>:“When you're passing primitives into a method,you ...

  7. 【转】这些编程语言程序员工资最高!Java才第四

    原文网址:http://tech.hexun.com/2016-07-18/185009761.html 在众多行业中,程序员属于高薪职业.无论是在国外还是国内,程序员的薪金水平普遍高于其他行业的工作 ...

  8. 使用模板时 error LNK2019: 无法解析的外部符号

    类模板是c++编译器指令 说明了如何生成类和成员函数 除非编译器实现了新的关键字export关键字 否则将模板成员函数放置在一个独立的实现文件中 将无法运行 因为模板不是函数 他们不能单独编译 模板必 ...

  9. devexpress GridControl 行指示列图标绘制

    Row Indicator Panel The row indicator panel represents a region displayed at the left edge of the Vi ...

  10. Memcached 内存级缓存

    Memcached在大型网站中应用    memcached是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像.视 频.文件以及 ...