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. 编写可维护的JS 01

    1.编程风格 缩进层级 使用制表符进行缩进 2个/4个空格缩进 语句结尾 不省略分号 行的长度 不超过80个字符 换行 在运算符后面换行 空行 在以下场景中添加: 方法之间 在方法中局部变量与第一条语 ...

  2. Node.js初学

    Node.js 初学~ 其技术上最大的卖点是非阻塞的I/O和基于事件的异步处理机制. 后端没有什么深入研究,一直对其不是很了解. 透过一个例子看 非阻塞 与 通常的 阻塞 var text = rea ...

  3. Jquery ajax调用后台aspx后台文件方法(不是ashx)

    在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. (1)通过aspx.cs的静态方法+WebMethod进行处理 简单的介绍下 ...

  4. DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建

    这个系列通过一些简单例子循序渐进,介绍DevExpress ASP.NET控件的使用.先来介绍一下XPO的使用,安装的DevExpress版本为DXperienceUniversal-12.2.4,使 ...

  5. Java 网络编程(二) 两类传输协议:TCP UDP

    链接地址:http://www.cnblogs.com/mengdd/archive/2013/03/09/2951841.html 两类传输协议:TCP,UDP TCP TCP是Transfer C ...

  6. BZOJ 1257 余数之和sum(分块优化)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46954 题意:f(n, k)=k mod 1 + k mod 2 ...

  7. java 成员访问修饰符

    作用域 当前类 当前包(package) 子类 其他包(package) public ok ok ok ok protected ok ok ok no default ok ok no no pr ...

  8. 梳理下phpmyadmin改root密码后登录不上的问题。

    一, 登陆phpmyadmin,然后点击左侧进入mysql数据库,在顶部点击“mysql”进入sql输入界面.输入以下命令: update user set password=password('12 ...

  9. js获取中英文长度

    function getLength(str) {    var len = str.length;    var reLen = 0;    for (var i = 0; i < len; ...

  10. POCO C++ lib开发环境构建

    Welcome Thank you for downloading the POCO C++ Libraries and welcome to the growing community of POC ...