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

思路: 找规律这件事,emmm.....

   注意sqrt(n)这个数,数之间的差与后面数的个数。。。。。

   写几个完整的例子,努力寻找规律!

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#include <deque>
#include <iostream>
using namespace std;
typedef long long LL;
const LL N = ; map<int, bool> check;
int prime[]; long long H( int n ) {
long long res = ;
for( int i = ; i <= n; i++ )
res = res + n / i;
return res;
} int main()
{
LL i, p, j, n, t, cnt = ;
LL sum; scanf("%lld", &t);
while(t--) {
sum = ;
scanf("%lld", &n);
for(i = ; i <= (LL)sqrt(n); i++) {
// cout << "i: " << i << endl;
sum += (n / i - n / (i + )) * i;
sum += n / i;
}
if(n / (LL)sqrt(n) == (LL)sqrt(n)) {
// sum -= (n / (LL)(sqrt(n)) - n / (LL)(sqrt(n) + 1)) * (i - 1);
sum -= (LL)sqrt(n);
}
printf("Case %lld: %lld\n", cnt ++, sum);
}
return ; //2 147 483 648
}

Harmonic Number (II) 数学找规律的更多相关文章

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

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

  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. LightOJ1245 Harmonic Number (II) —— 规律

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

  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. # E. Mahmoud and Ehab and the xor-MST dp/数学+找规律+xor

    E. Mahmoud and Ehab and the xor-MST dp/数学/找规律 题意 给出一个完全图的阶数n(1e18),点由0---n-1编号,边的权则为编号间的异或,问最小生成树是多少 ...

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

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

  8. HDU 1391 number steps(找规律,数学)

    Starting from point (0,0) on a plane, we have written all non-negative integers 0, 1, 2,... as shown ...

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

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

随机推荐

  1. LeetCode30 Hard 查找所有子串

    本文始发于个人公众号:TechFlow,原创不易,求个关注 链接 Substring with Concatenation of All Words 难度 Hard 描述 给定一个字符串s作为母串,和 ...

  2. Linux 命令之 linux 四剑客

    Linux命令-- 四剑客 一:Linux命令 之 AWK 符号:^ 开头 $ 结尾 awk 是一种处理文本的语言,一个强大的文本分析命令! 1:提取文件中的每行的第二个 提取前文本中内容为  命令: ...

  3. MVC开发之Razor的使用

    一.运用布局和视图起始文件 布局的使用,可以简化视图,允许用户创建能够运用于多个视图的通用HTML,并使得维护更加容易.下面是一个简单的例子: 域模型类: public class Product { ...

  4. mysql 零基础 开始过程

    2016-11-07 算是差不多是两个月的学习,我也马上要结课了.今天才勉强把mysql装上,之前还有因为用户权限的问题,以及用户为创建的问题.应该早点就把该准备的开发平台准备好,而不是在最后快考试了 ...

  5. kali linux中mariadb加上密码

    kali自带mysql.2019.4 中带得是:MariaDB.据说跟Mysql差不多.简单用了一下发现root用户可以不要密码进入Mysql! 这极不习惯,不输入密码感觉好像少了点什么.这肯定是权限 ...

  6. win10系统家庭版升级到专业版

    win10家庭版升级专业版密钥:VK7JG-NPHTM-C97JM-9MPGT-3V66T4N7JM-CV98F-WY9XX-9D8CF-369TT FMPND-XFTD4-67FJC-HDR8C-3 ...

  7. PAT (Advanced Level) Practice 1008 Elevator (20 分) (模拟)

    The highest building in our city has only one elevator. A request list is made up with N positive nu ...

  8. 如何在php7.2/php7.3中安装mcrypt扩展?

    mcrypt 是php里面重要的加密支持扩展库,linux环境下该库在默认情况下不开启.window环境下,PHP>=5.3,默认开启mcrypt扩展.mcrypt扩展是mcrypt加密库的接口 ...

  9. PP: Taking the human out of the loop: A review of bayesian optimization

    Problem: Design problem parameters consist of the search space of your model. Scientists design expe ...

  10. Charles抓包问题

    Charles抓包问题 抓包失败解决方法之一:在proxy下的Recording Setting找到解决方法 找到里面的include 把勾去掉,点击OK,然后就恢复正常可以抓包了.