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

Submit
Status

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

Source

Problem Setter: Jane Alam Jan

/很好的题解:转载的

先求出前sqrt(n)项和:即n/1+n/2+...+n/sqrt(n)

再求出后面所以项之和.后面每一项的值小于sqrt(n),计算值为1到sqrt(n)的项的个数,乘以其项值即可快速得到答案

例如:10/1+10/2+10/3+...+10/10

sqrt(10) = 3

先求出其前三项的和为10/1+10/2+10/3

在求出值为1的项的个数为(10/1-10/2)个,分别是(10/10,10/9,10/8,10/7,10/6),值为2个项的个数(10/2-10/3)分别是(10/5,10/4),在求出值为3即sqrt(10)的项的个数.

显然,值为sqrt(10)的项计算了2次,减去一次即可得到答案。当n/(int)sqrt(n) == (int)sqrt(n)时,值为sqrt(n)的值会被计算2次。

#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
int t,n,i,j;
int T=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
long long ans=0;
for(i=1;i<=(int)sqrt(n);i++)
{
ans+=n/i;
if(n/i>n/(i+1))
ans+=(long long)((n/i-n/(i+1))*i);
}
i--;
if(n/i==i)
ans-=i;
printf("Case %d: %lld\n",T++,ans);
}
return 0;
}

lightoj--1245--Harmonic Number (II)(数学推导)的更多相关文章

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

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

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

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

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

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

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

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

  5. LightOJ 1245 - Harmonic Number (II)

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

  6. LightOJ 1245 Harmonic Number (II) 水题

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

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

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

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

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

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

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

  10. Harmonic Number (II) 数学找规律

    I was trying to solve problem '1234 - Harmonic Number', I wrote the following code long long H( int  ...

随机推荐

  1. SQL使用总结——集合操作函数

    Oracle中集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINUS.当使用集合操作符时,必须确保不同查询的列个数和数据类型匹配 ...

  2. 多个tomcat配置,解决冲突问题

    一.一般修改 路径: /opt/apache-tomcat/conf/server.xml 1.第一个tomcat使用默认配置 2.第二个tomcat一般配置 二.特殊修改 1.第二个tomcat特殊 ...

  3. Ssh safe

    新建用户,设置密码 useradd eason passwd eason 不允许root直接登陆 修改配置文件 vi /etc/ssh/sshd_config 禁止root登录 查找“#PermitR ...

  4. 创建异步对象XHR的兼容写法、get、post上传数据的方式

    兼容ie7以下,创建异步对象的函数 function creatXHR(){ if(typeof XMLHttpRequest != "undefined"){ return ne ...

  5. IE浏览器 ajax传参数值为中文时出现乱码的解决方案

    找了很多方法,发现就这个方法简单.直接.方便,直接推荐哦! 在汉字的位置加个保护措施:encodeURIComponent(parentid) 举个栗子>>> $.ajax({    ...

  6. 错误:android.util.SuperNotCalledException

    android.util.SuperNotCalledException: Activity {…….YouTubePlayActivity} did not call through to supe ...

  7. matplotlib简介-高质量图形输出

    Matplotlib 是一个用来绘制二维图形的 Python 模块,它克隆了许多 Matlab 中的函数, 用以帮助 Python 用户轻松获得高质量(达到出版水平)的二维图形. 文章来源:http: ...

  8. hibernate增删改查总结

    hibernate操作的都是基于对象的(曾save删delete改update) 进行hql查询是设置参数从零开始(session.setParameter(0,#)) hql查询设置参数可以是其他( ...

  9. python指定日期后加几天判断

    #!/usr/bin/python import datetime import sys arg1_list=list(sys.argv[1].split(',')) arg2_list=list(s ...

  10. obj-c部分对象快捷赋值和取值

    NSNumber: NSNumber *number = @1234; 旧的方式: NSArray *physicsValues = [NSArrayarrayWithObjects: [NSNumb ...