LightOJ 1245 - Harmonic Number (II) (找规律)

简介:


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

 

题目大意:
就是根据这个代码:
long long H( int n ) {
    long long res = 0;
    for( int i = 1; i <= n; i++ )
        res = res + n / i;
    return res;
}
我们要求的就是H(n);

解题思路:

首先我们观察一下数据范围,2^31次方有点大,暴力会超时,所以我们看看有没有啥规律,假设 tmp 是 n/i 的值,当n == 10的时候(取具体值)
当 tmp = 1 时,个数 是10/1 - 10/2 == 5个

当 tmp = 2 时,个数 是10/2 - 10/3 == 2个

当 tmp = 3 时,个数 是10/3 - 10/4 == 1个
…………
当 tmp = 10时,个数是10/10 - 10/11 == 1个
所以我们发现有个规律了,当tmp == i 的时候,我们要求的个数就是 10/i - 10/(i+1),然后我们前1 — sqrt(n)个数的数值还是比较大的,但是数据范围变小了
暴力可以求出来,剩下的 sqrt(n)+1 — n个数中 数据范围还是比较大,但是 n/i 的数据范围介于 1 - sqrt(n)之间,所以用我们找出的规律可以求出来,我们只需要
两个for循环就搞定了,时间复杂度 O(sqrt(n)),完全可以,剩下的就是编写程序了
上代码:
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;
int main()
{
    int T;
    LL n;
    cin>>T;
    for(int cas=1; cas<=T; cas++)
    {
        cin>>n;
        int m = sqrt(n);
        LL ret = 0;
        for(int i=1; i<=m; i++)
            ret += n/i;
        for(int i=1; i<=m; i++)
            ret += (n/i - n/(i+1))*i;
        if(m == n/m)
            ret -= m;
        cout<<"Case "<<cas<<": "<<ret<<endl;
    }
    return 0;
}




目录
相关文章
|
Java C++
HDU-1005,Number Sequence(有规律的数学题)
HDU-1005,Number Sequence(有规律的数学题)
|
7月前
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
73 1
|
18天前
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】
|
1月前
|
存储 算法
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
【LeetCode力扣】单调栈解决Next Greater Number(下一个更大值)问题
15 0
|
7月前
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
25 0
|
算法
LeetCode 414. Third Maximum Number
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
71 0
LeetCode 414. Third Maximum Number
|
存储
LeetCode 313. Super Ugly Number
编写一段程序来查找第 n 个超级丑数。 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数。
76 0
LeetCode 313. Super Ugly Number
|
算法
LeetCode 306. Additive Number
累加数是一个字符串,组成它的数字可以形成累加序列。 一个有效的累加序列必须至少包含 3 个数。除了最开始的两个数以外,字符串中的其他数都等于它之前两个数相加的和。 给定一个只包含数字 '0'-'9' 的字符串,编写一个算法来判断给定输入是否是累加数。 说明: 累加序列里的数不会以 0 开头,所以不会出现 1, 2, 03 或者 1, 02, 3 的情况。
99 0
LeetCode 306. Additive Number
|
算法
LeetCode 268. Missing Number
给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。
69 0
LeetCode 268. Missing Number
LeetCode 264. Ugly Number II
编写一个程序,找出第 n 个丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。
57 0
LeetCode 264. Ugly Number II