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. 适用于Firemonkey的Json解析对象XsuperObject使用方法介绍

    XSuperObject是适用于FileMonkey平台的JSON解析组件,能够在Android,IOS,MACOS,WINDOWS等多个平台使用 点击下载 Sample JSON { "n ...

  2. short s1 = 1; s1 = s1 + 1;和 s1 += 1;

    对于short s1 = 1; s1 = s1 + 1; 由于s1+1运算时会自动提升表达式的类型,所以结果是int型,再赋值给short类型s1时,编译器将报告需要强制转换类型的错误. 对于shor ...

  3. HDU 1564 (博弈) Play a game

    这道题的答案猜也很好猜出来,但是想太难想了..题解一贴,然后闪人.. 请戳这

  4. UVa 12206 (字符串哈希) Stammering Aliens

    体验了一把字符串Hash的做法,感觉Hash这种人品算法好神奇. 也许这道题的正解是后缀数组,但Hash做法的优势就是编码复杂度大大降低. #include <cstdio> #inclu ...

  5. 在view中常见的四种方法的使用场合

    四种方法,使view创建好里面就有东西:[1.init  2.initWithFrame使用代码创建的时候.(从文件创建的时候不一定调用:1.init  2.initWithFrame这两个方法) 3 ...

  6. php的session.serialize_handler

    php里面的session.serialize_handler用来设置php的session的序列化方式,默认值为php,及使用php的序列化与反序列化. 还有一个值为session.serializ ...

  7. QR二维码(转)

    二维码又称QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型:比如:字符,数字, ...

  8. 删除github.com上repository(仓库)的方法

    第一步:打开http://github.com,看到右侧仓库列表.第二步:假设要删除“HiTop”这个参考,点击对应仓库进入详细页面之后,在右侧会看到“Settings”入口. 第三步:进入设置页面之 ...

  9. PHP String函数分类

    1.查找字符位置函数: strpos  ($str,search,[int]):    查找search在$str中的第一次位置从int开始: stripos ($str,search,[int]): ...

  10. MySQL与Oracle 差异比较之四条件循环语句

    循环语句 编号 类别 ORACLE MYSQL 注释 1 IF语句使用不同 IF iv_weekly_day = 'MON' THEN       ii_weekly_day := 'MON';ELS ...