题目链接:https://vjudge.net/problem/LightOJ-1245

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

题意:

对于一个数n,求出 sigma(n/k), 1<=k<=n。

题解:

1.由于n<=2^31,直接暴力不不可能的。

2.手写一下可发现一个规律, 当 n/i = k时, i的范围为 (n/(k+1), n/k],即有n/k- n/(k+1) 个 i 使得n/i = k。

3.有了上述结论之后,就可以降低暴力程度了,设 m = sqrt(n)。先从1枚举到m,求出n/i的和,这样 [1,m] 这个区间的求值就解决了,还剩 [m+1, n] 这个区间:从m递减枚举到1,求出k*(n/k - n/(k+1))的和,这样就求出了[ n/m, n]的值,当 n/m==m时,即开始下标为m,则区间为[m,n],所以在m的位置重复计算了,需要减去m;当n/m!=m,即n/m>m时,其实下标为m+1,所以区间为 [m+1,n]。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e6+; int main()
{
int T, n, kase = ;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
int m = sqrt(n); LL ans = ;
for(int i = ; i<=m; i++) ans += n/i;
for(int i = ; i<=m; i++) ans += 1LL*i*(n/i - n/(i+));
if(n/m==m) ans -= m;
printf("Case %d: %lld\n", ++kase, ans);
}
}

LightOJ1245 Harmonic Number (II) —— 规律的更多相关文章

  1. LightOJ1245 Harmonic Number (II)

    题意 \(求\Sigma \lfloor \frac{n}{i} \rfloor\) Input starts with an integer T (≤ 1000), denoting the num ...

  2. G - Harmonic Number (II) 找规律--> 给定一个数n,求n除以1~n这n个数的和。n达到2^31 - 1;

    /** 题目:G - Harmonic Number (II) 链接:https://vjudge.net/contest/154246#problem/G 题意:给定一个数n,求n除以1~n这n个数 ...

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

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

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

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

  5. Harmonic Number (II)

    Harmonic Number (II)   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB I ...

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

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

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

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

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

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

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

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

随机推荐

  1. Codeforces 196C Paint Tree(贪心+极角排序)

    题目链接 Paint Tree 给你一棵n个点的树和n个直角坐标系上的点,现在要把树上的n个点映射到直角坐标系的n个点中,要求是除了在顶点处不能有线段的相交. 我们先选一个在直角坐标系中的最左下角的点 ...

  2. va_list 简介

    原文:http://blog.sina.com.cn/s/blog_590be5290100qhxr.html va_list是一个宏,由va_start和va_end界定. typedef char ...

  3. poj 2104 K-th Number(主席树

    Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 59058   Accepted: 20529 Case Time Limi ...

  4. AGC006

    AtCoder Grand Contest 006 <br > 心血来潮,开了一套AGC..... 然后发现各种不会做.........感觉智商被AGC摁在地上摩擦...... <b ...

  5. springboot快速集成swagger

    今天技术总监说:小明,我们本次3.0改造,使用swagger2.0作为前后端分离的接口规范,它可以一键生成前后端的API,一劳永逸--小明:??? Spring Boot 框架是目前非常流行的微服务框 ...

  6. Cef应用程序结构

    Application Structure 应用程序结构 Every CEF3 application has the same general structure. Provide an entry ...

  7. 手动安装windows的磁盘清理工具

    All you really need to do is copy some files that are already located on your server into specific s ...

  8. POJ3592 Instantaneous Transference 强连通+最长路

    题目链接: id=3592">poj3592 题意: 给出一幅n X m的二维地图,每一个格子可能是矿区,障碍,或者传送点 用不同的字符表示: 有一辆矿车从地图的左上角(0,0)出发, ...

  9. NFC模组,开发NFC功能 仅仅要几条指令的事情

    特点:实现NFC透明传输.内置NFC协议栈,支持UART串口直接读写,用于门禁能够同一时候兼容手机和卡片开门,还能实现动态密钥,读到的NFC数据自己主动串口输出,会串口就能开发NFC,不须要研究LLC ...

  10. 第一个AngularJS Sample

    代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3. ...