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)个数;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define esp 0.00000000001
#define pi 4*atan(1)
const int N=1e3+,M=1e7+,inf=1e9+,mod=1e9+;
int main()
{
int T,cas=;
scanf("%d",&T);
while(T--)
{
ll x;
scanf("%lld",&x);
ll ans=;
for(ll i=;i*i<=x;i++)
{
if(i!=x/i)
ans+=x/i;
ll len=(x/i)-x/(i+);
ans+=i*len;
}
printf("Case %d: %lld\n",cas++,ans);
}
return ;
}

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. 七、Dockerfile案例一(jdk1.8安装)

    七.Dockerfile案例一(jdk1.8安装) 1 获取一个简单的Docker系统镜像,并建立一个容器. 这里我选择下载CentOS镜像 docker pull centos 通过docker t ...

  2. 一、docker临时记录

    docker 临时记录(阿里云centos7.2.1511 ) 查看系统版本号 适用于Redhat/CentOS: [root@iz2zecm4ndtkaue32tynx5z ~]# cat /etc ...

  3. 关于string的replace方法

    今天写代码遇见一个小问题,就是当string  a,b,c,d当腰删除b的时候如果replace(“a”,"");会造成,b,c,d   所以得replace("a&qu ...

  4. base64文件上传的问题

    package com.zhicall.media.util; import java.io.FileInputStream; import java.io.FileOutputStream; imp ...

  5. debug_backtrace final catch

    <?php function backtrace_str(){ $str = ''; $w = 0; $backtrace = debug_backtrace(); foreach($backt ...

  6. python系列二:python3基本数据类型

    #标准数据类型——number(数字)a, b, c = 1, 2.2, "hello"print(a, end = ", ")print(b, end = & ...

  7. python错误笔记

    1.print "hello world!";SyntaxError:Missing parentheses in call to ‘paint’ . Did you mean p ...

  8. getDomain(url)-我的JavaScript函数库-mazey.js

    获取链接地址中域名,如mazey.net,www.mazey.net,m.mazey.net. 参数:url 必需function getDomain(url){    var a = documen ...

  9. 【题解】CF611H New Year and Forgotten Tree

    [题解]CF611H New Year and Forgotten Tree 神题了... 题目描述 给定你一棵树,可是每个节点上的编号看不清了,只能辨别它的长度.现在用问号的个数代表每个节点编号那个 ...

  10. 剑指offer 面试25题

    面试25题:题目:合并两个排序的链表 题:输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 解题思路:递归,并需注意对空链表单独处理. 解题代码: # -* ...