LightOJ - 1245

Harmonic Number (II)

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

/**
          题意:求n/1 + n/2 + n/3 + ........n/n
          做法:先求tt = sqrt(n); 比如 n = 10
          sqrt(10) = 3,10/1 ~ 10/10 之间有 10/6 ~ 10/10 之间都是1 个数为(10/1 - 10/2)个
          之间为2的个数为 (10/4 ~ 10/5) 有(10/2 ~ 10/3) 个
          为3的个数有 10 /3 有(10/3) 个
          然后加判断 如果 n/(int)sqrt(n) == (int)sqrt(n)  则sum -= n/(int)sqrt(n)
**/
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
          freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
          int T;
          int Case = 1;
          scanf("%d",&T);
          while(T--)
          {
                    long long n;
                    scanf("%lld",&n);
                    long long res = 0;
                    int tt = (int)sqrt(n);
                    for(int i=1;i<=tt;i++)
                    {
                              res += (n/i);
                    }
                    for(int j=1;j<=tt;j++)
                    {
                              res += (n/j - n/(j+1)) * j;
                    }
                    if( n/(int)sqrt(n) == (int)sqrt(n))
                    res -= (n/(int)sqrt(n));
                    printf("Case %d: %lld\n",Case++,res);
          }
          return 0;
}
时间: 2024-08-08 20:56:32

LightOJ - 1245的相关文章

LightOJ 1245 数学题,找规律

1.LightOJ 1245   Harmonic Number (II)   数学题 2.总结:看了题解,很严谨,但又确实恶心的题 题意:求n/1+n/2+....+n/n,n<=2^31. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a

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

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:求f(n)=n/1+n/2.....n/n,其中n/i保留整数 显然一眼看不出什么规律.而且n有2e31直接暴力肯定要出事情 但是f=n/x这个函数很好关于y = x 对称对称点刚好是sqrt(n) 于是就简单了直接求sum+n/i (i*i<n && i >=1) 然后乘以2,再减去i*i即可. 这个i*i表示的是什么呢,由于对称上半部份的值完

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

题目大意:对下列代码进行优化 long long H( int n ) {    long long res = 0;    for( int i = 1; i <= n; i++ )        res = res + n / i;    return res;} 题目思路:为了避免超时,要想办法进行优化 以9为例: 9/1 = 9 9/2 = 4 9/3 = 3 9/4 = 2 9/5 = 1 9/6 = 1 9/7 = 1 9/8 = 1 9/9 = 1 拿1来看,同为1的区间长度为:9

LightOJ 1245 Harmonic Number (II) 水题

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

LightOJ 1245 数学

Harmonic Number (II) 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 e

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

题目大意:给出一个n(1 <= n < 2^31)求出H(n)的结果,H(n)的定义为下: 分析:对于一个n,设 t = n / i: 满足 t >= 1的有多少个呢? 有 n / 1 个. 满足 t >= 2的有多少个呢? 有 n / 2 个. -- 满足 t >= k的有多少个呢? 有 n / k 个. 以上结论不难发现,我们再进一步就能发现: 满足 t == 1 的有 n/1 - n/2 个 满足 t == 2 的有 n/2 - n/3 个 -- 满足 t == k 的

kuangbin 带你飞 数学基础

模版整理: 晒素数 void init() { cas = 0; for (int i = 0 ; i < MAXD ; i++) is_prime[i] = true; is_prime[0] = is_prime[1] = false; for (int i = 2 ; i < MAXD ; i++) { if (is_prime[i]) { prime[cas++] = i; for (int j = i + i ; j < MAXD ; j += i) is_prime[j] =

1245 - Harmonic Number (II)---LightOJ1245

http://lightoj.com/volume_showproblem.php?problem=1245 题目大意:一个数n除以1到n之和 分析:暴力肯定不行,我们可以先求1~sqrt(n)之间的每个数的个数,然后再求n除以1~sqrt(n)之间的数的和 这样算下来就只有2*sqrt(n)的复杂度 最后还要排除多加的,. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algor

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

1245 - Harmonic Number (II)   PDF (English) Statistics Forum 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 =