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. nginx解决服务器宕机、解决跨域问题、配置防盗链、防止DDOS流量攻击

    解决服务器宕机 配置nginx.cfg配置文件,在映射拦截地址中加入代理地址响应方案 location / { proxy_connect_timeout 1; proxy_send_timeout ...

  2. source、sh、./执行脚本对变量的影响

    shell脚本中的变量: local一般用于局部变量声明,多在在函数内部使用. shell脚本中定义的变量是global的,其作用域从被定义的地方开始,到shell结束或被显示删除的地方为止. she ...

  3. docker下载镜像太慢的解决方案

    参考链接:https://blog.csdn.net/weixin_43569697/article/details/89279225 docker下载镜像卡死或太慢找了网上很多方法,使用镜像中国也是 ...

  4. SQL语句中设置字段值取反操作

    1.对布尔值取反,使用 ~. 如 update set status=~status where id=2; status的值为true || false. 2.对0.1 数值取反,使用abs() 取 ...

  5. Linux 一些使用工具

    ssh 链接使用工具xshell 下载链接 http://www.onlinedown.net/soft/36383.htm 映射硬盘工具 sftpdriver 安装输入服务器之后链接 并且输入注册码 ...

  6. windows上快速新建1g的文件

    在E盘新建1g文件 fsutil file createnew d:\b.txt 1073741824

  7. WSO2 ESB XML定义语法(3)

    6.Property Mediator 通过Synapse调解的每条消息都可以具有一组关联的属性.Synapse引擎和底层传输在处理的每条消息上设置了许多属性,用户可以操纵这些属性来修改消息流的运行时 ...

  8. 基于Web的网络商城项目设计与实现【SSM+Bootstrap+Vue】

    [Spring+SpringMVC+MyBatis+Bootstrap+Vue] 演示:线路1  线路2 1.系统功能介绍 网上商城系统 是一个功能完善的在线购物系统 - ,主要为在线销售和在线购物服 ...

  9. 开发过程中遇到的代理Proxy配置问题

    proxy代理问题 在公司不能访问外网的时候,使用IDEA开发.. 需要配置IDEA Proxy Maven Git IDEA开发工具Proxy配置,使用Spring Boot快读构建工具 Maven ...

  10. C语言 栈的初始化,入栈,出栈,判断栈是否为空,清空栈等函数

    #include <stdio.h>#include <stdlib.h> typedef struct Node{ int data ; struct Node * pNex ...