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. 51nod1376 最长递增子序列的数量

    O(n2)显然超时.网上找的题解都是用奇怪的姿势写看不懂TAT.然后自己YY.要求a[i]之前最大的是多少且最大的有多少个.那么线段树维护两个值,一个是当前区间的最大值一个是当前区间最大值的数量那么我 ...

  2. 精选37条强大的常用linux shell命令组合

    任务                             命令组合 1 删除0字节文件 find . -type f -size 0 -exec rm -rf {} \;find . type f ...

  3. Asp.Net判断字符是否为汉字的方法大全

    判断一个字符是不是汉字通常有三种方法: 第一种用 ASCII 码判断,缺点:把全角逗号“,”当汉字处理 第二种用汉字的 UNICODE 编码范围判 断, 第三种用正则表达式判断 1.用ASCII码判断 ...

  4. chrome浏览器下禁制 textarea改变大小; Jquery的textareaCounter插件控制textarea输入的字符数量

    给  textarea 添加一个css 样式即可 resize: none;   用Jquery的插件控制textarea输入的字符数量 一:引用Jquery脚本,并引入 textareaCounte ...

  5. 韦东山驱动视频笔记——3.字符设备驱动程序之poll机制

    linux内核版本:linux-2.6.30.4 目的:我们在中断方式的按键应用程序中,如果没有按键按下,read就会永远在那等待,所以如果在这个程序里还想做其他事就不可能了.因此我们这次改进它,让它 ...

  6. strust2 配置chainAction结果类型的配置

    <result name="chainAction" type="chain"> <param name="actionName&q ...

  7. openssl安装

    www.openssl.orgconfigure the environment:<pre lang="bash" escaped="true">t ...

  8. Heritrix源码分析(十五) 各种问题总结(转)

    开博客以及建立Heritrix 群有一段时间了(这里谢谢大家的关注),这篇博客将整理这段时间所遇到的问题.同时由于自己从今年5月份开始就不怎么接触Heritrix,很多东西开始遗忘(不过里面思想没忘) ...

  9. mysql 数据库自增id 的总结

    有一个表StuInfo,里面只有两列 StuID,StuName其中StuID是int型,主键,自增列.现在我要插入数据,让他自动的向上增长,insert into StuInfo(StuID,Stu ...

  10. 数据结构——Java实现二叉树

    相关概念 存储结构: 顺序存储结构:二叉树的顺序存储结构适用于完全二叉树,对完全二叉树进行顺序编号,通过二叉树的性质五(第1个结点为根结点,第i个结点的左孩子为第2i个结点,右孩子为第2i+1个结点) ...