1245 - Harmonic Number (II)
Time Limit: 3 second(s) Memory Limit: 32 MB

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

Output for Sample Input

11

1

2

3

4

5

6

7

8

9

10

2147483647

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),减去即可;
无语,这里输出%I64d竟然wa。。。
代码:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f; int main(){
int T;
int n,cnt=;
LL res;
scanf("%d",&T);
while(T--){
res=;
scanf("%d",&n);
int m=sqrt(n);
for(int i=;i<=m;i++)res+=n/i;
for(int i=;i<=m;i++)
res+=(n/i-n/(i+))*i;
if(m==n/m)res-=m;
printf("Case %d: %lld\n",++cnt,res);
}
return ;
}

1245 - Harmonic Number (II)(规律题)的更多相关文章

  1. LightOJ 1245 Harmonic Number (II) 水题

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

  2. LightOJ1245 Harmonic Number (II) —— 规律

    题目链接:https://vjudge.net/problem/LightOJ-1245 1245 - Harmonic Number (II)    PDF (English) Statistics ...

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

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

  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://lightoj.com/volume_showproblem.php?problem=1245 题意就是求 n/i (1<=i<=n) 的取整的和这就是到找规律的题 ...

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

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

  7. LightOJ 1245 - Harmonic Number (II)

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

  8. 1245 - Harmonic Number (II)---LightOJ1245

    http://lightoj.com/volume_showproblem.php?problem=1245 题目大意:一个数n除以1到n之和 分析:暴力肯定不行,我们可以先求1~sqrt(n)之间的 ...

  9. light oj -1245 - Harmonic Number (II)

    先举个例子,假如给你的数是100的话,将100/2=50;是不是就是100除100-51之间的数取整为1: 100/3=33;100除50到34之间的数为2,那么这样下去到sqrt(100);就可以求 ...

随机推荐

  1. 1.2UISwitch 1.3 自定义UIswitch 1.4pickerView

    1.2 UISwitch创建和使用开关 问题你想给你的用户打开一个选项或关闭的能力.解使用UISwitch类. 讨论该UISwitch类提供像在图1-7为自动大写,自动校正,等等所示的开/ ...

  2. C#获取时间的函数

    //获取日期+时间DateTime.Now.ToString();            // 2012-9-4 20:02:10DateTime.Now.ToLocalTime().ToString ...

  3. ##DAY5 UIControl及其子类

    ##DAY5 UIControl及其子类 #pragma mark ———————UIControl——————————— UIControl初识: 1)UIControl是有控制功能的视图(比如UI ...

  4. linux下静态链接库的用法

    最近在Linux下编程发现一个诡异的现象,就是在链接一个静态库的时候总是报错,类似下面这样的错误: (.text+0x13): undefined reference to `func' 关于unde ...

  5. Oracle "Job定时"

    今天需要做个定时器,定时到别的库导入数据用到了Job,第一次使用记录下来,如果有第一次操作的可以借鉴一下 1.首先,使用Toad新建job,进入配置页面

  6. python的map()函数

    map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例如,对于list [1, 2 ...

  7. 韦根(Wiegand)数据传输格式

    韦根数据传输使用TTL电平,有两条数据线,分别称为DATA0和DATA1.无数据传输时,两条线都是高电平,当传输“1”时,DATA0为高,DATA1为低:当传输“0”时,DATA0为低,DATA1为高 ...

  8. Json.Net系列教程 2.Net类型与JSON的映射关系

    原文 Json.Net系列教程 2.Net类型与JSON的映射关系 首先谢谢大家的支持和关注.本章主要介绍.Net类型与JSON是如何映射的.我们知道JSON中类型基本上有三种:值类型,数组和对象.而 ...

  9. perl5 第五章 文件读写

    第五章 文件读写 by flamephoenix 一.打开.关闭文件二.读文件三.写文件四.判断文件状态五.命令行参数六.打开管道 一.打开.关闭文件   语法为open (filevar, file ...

  10. HDOJ 1226 超级密码(bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1226 思路分析:题目要求寻找一串长度不大于500的C进制的密码,且该密码需要为十进制数N的整数倍. & ...