Harmonic Number (II)

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

思路:类似分块的思想最多有2sqrt(n)个数;

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define esp 0.00000000001
  5. #define pi 4*atan(1)
  6. const int N=1e3+,M=1e7+,inf=1e9+,mod=1e9+;
  7. int main()
  8. {
  9. int T,cas=;
  10. scanf("%d",&T);
  11. while(T--)
  12. {
  13. ll x;
  14. scanf("%lld",&x);
  15. ll ans=;
  16. for(ll i=;i*i<=x;i++)
  17. {
  18. if(i!=x/i)
  19. ans+=x/i;
  20. ll len=(x/i)-x/(i+);
  21. ans+=i*len;
  22. }
  23. printf("Case %d: %lld\n",cas++,ans);
  24. }
  25. return ;
  26. }

LightOJ 1245 数学的更多相关文章

  1. LightOJ 1245 数学题,找规律

    1.LightOJ 1245   Harmonic Number (II)   数学题 2.总结:看了题解,很严谨,但又确实恶心的题 题意:求n/1+n/2+....+n/n,n<=2^31. ...

  2. LightOJ 1245 Harmonic Number (II)(找规律)

    http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS    ...

  3. lightoj 1245

    lightoj 1245 Harmonic Number (II) 题意:给定一个 n ,求 n/1 + n/2 + …… + n/n 的值(这里的 "/" 是计算机的整数除法,向 ...

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

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

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

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

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

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

  7. lightoj 1245 Harmonic Number (II)(简单数论)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:求f(n)=n/1+n/2.....n/n,其中n/i保留整数 显 ...

  8. LightOJ 1245 - Harmonic Number (II)

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

  9. G - Harmonic Number (II) LightOJ - 1245

    算是一个找规律的题目吧. 枚举前sqrt(n)个数,数i出现的次数为n/i-n/(i+1),对答案的贡献为(n/i-n/(i+1))*i. 对于sqrt后边的数,可以直接由n/i获得,并且一定只出现一 ...

随机推荐

  1. 洛谷P1122 最大子树和

    P1122 最大子树和 题目提供者该用户不存在 标签动态规划树形结构 难度普及/提高- 通过/提交54/100 提交该题 讨论 题解 记录 题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在 ...

  2. easy 正则表达式验证 封装

    基于EasyUI ValidateBox 的表单验证,可以直接写正则表达式. 在页面加载时执行下面代码 if ($.fn.form) { $.extend($.fn.validatebox.defau ...

  3. Java中,由this关键字引发的问题

    很久之前一直有一个疑问,最近重新翻了遍JVM的书,才算是终于有所顿悟.问题如下: 被自己遗忘的问题 package org.hanyan.test.testClass; public class T3 ...

  4. Spring 缓存注解@Cacheable 在缓存时候 ,出现了第一次进入调用 方法 ,第二次不调用的异常

    代码: @Override @Cacheable(value = CACHE_NAME, key = "'CartItemkey_'+#uId") public List<S ...

  5. 在执行save操作时候出现的诡异!

    情景:有一份excel数据需要导入到数据库中,想了想就写了一个导入excel的小功能. .............................准备使用时候,就开搞了,擦! 什么鬼??? 全程无报错 ...

  6. element-UI的操作步骤steps每一项添加事件,比如click,hover

    简单来说,只添加click 和css :hover就好了 一.组件里 <el-steps :space="200" :active="1" finish- ...

  7. spring security原理

    spring security通过一系列过滤器实现其功能,入口过滤器如下(web.xml): <filter> <filter-name>springSecurityFilte ...

  8. Jmeter关联技术

    JMeter:关联步骤 <1>录制成功,回放失败了: <2>录制两个业务相同的脚本,比对差别,找到动态数据,AptDiff_1.6.zip工具 <3>找到相应请求: ...

  9. Retrofit2.2说明-简单使用

    很久前就想学习下Retrofit了,不过总是没有时间,正好最近新项目要用到网络请求,正好研究了下Retrofit2.2的简单使用方法,大致记录如下: Retrofit与okhttp共同出自于Squar ...

  10. More on Class Loading and Initialization

    上一篇博客中对于类的加载和初始化进行了详细的说明,但上一篇博客代码中的main()所在的类为导出类, 对其中一些问题的理解可能会引起误导和不明确,所以补充这篇博客进一步说明.以下面的代码为例进行说明: ...