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. 在windows系统下,配置vue项目一键启动文件

    我的项目由客户端.后台管理.数据库和服务器三部分组件,每次启动项目都要一个一个启动,挺麻烦的,现在写一个.bat文件来批处理命令. 这个是我的启动文件内容. 第一行运行的我wampServer服务器, ...

  2. 如何通过 SSH/Telnet 用 root 权限登录群晖

    出于系统安全原因,对 Synology NAS 的 root 访问有限.如果您获取 root 权限,请在命令行界面中用任何属于Local Administrators群组的帐户证书登录 DSM(如Pu ...

  3. Centos7 Python2 升级到Python3

    1. 从Python官网到获取Python3的包, 切换到目录/usr/local/src wget https://www.python.org/ftp/python/3.7.2/Python-3. ...

  4. EntityManager的Clear方法的使用

    在日常开发中,如果使用hibernate的话,常常会被hibernate的事务搞得焦头烂额.今天解决了之前项目中一直存在的问题,记录一下. 问题描述 有一张表TemplateCopy,如下 publi ...

  5. ECMAScript基本对象——String 对象

    对象用于处理文本(字符串). 1.创建 var txt = new String("string"); var txt = "string"; 2.方法 cha ...

  6. (C语言)学生成绩排序-期末考倒数第二题结构体数组排序

    假设学生的基本信息包括学号.姓名.三门课程成绩以及个人平均成绩,定义一个能够表示学生信息的结构类型.输入n(n<50)个学生的成绩信息,按照学生的个人平均分从高到低输出他们的信息.如果平均分相同 ...

  7. Docker最全教程——从理论到实战(十六)

    前言 与其他语言相比,Go非常值得推荐和学习,真香!为什么?主要是可以直接编译成机器代码(性能优越,体积非常小,可达10来M,见实践教程图片)而且设计良好,上手门槛低.本篇主要侧重于讲解了Go语言的优 ...

  8. 在source insight project中删除文件夹或者文件的通用方法

    1.删除文件夹或者文件在硬盘上的数据 2.project->synchronize files...看到如下图,然后勾选remove missing files from project

  9. 【已解决】使用 yarn 安装时,报错node_modules\node sass:Command failed.

    npm install -g mirror-config-china --registry=http://registry.npm.taobao.org npm install node-sass y ...

  10. C++——动态内存分配1

    9.动态内存分配  new 类型名T(初值列表) 其功能是在程序执行期间申请用于存放T类型对象的内存空间,并依初值列表赋以初值,结果值:成功则T类型的指针,指向新分配的内存:失败则为0(null).若 ...