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

题意:就是计算所给的函数的值,但是咱们如果按函数那种算法会TLE,so

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std; int main()
{
int t, n, l = 1;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
long long k = sqrt(n);
long long res = 0;
for(int i = 1; i < k+1; i++)
{
res += n /i;
if(n/i > n / (i+1))
res += (n/i - n / (i+1))*i;
if((k*k + i - 1) == n) // 在k*k到k*k+k-1中的数多加了k,so减去
{
res -= k;
}
}
printf("Case %d: %lld\n", l++, res);
}
return 0;
}

  

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

    1245 - Harmonic Number (II)   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 3 ...

  3. 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个数 ...

  4. LightOJ1245 Harmonic Number (II) —— 规律

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

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

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

  6. Harmonic Number (II) 数学找规律

    I was trying to solve problem '1234 - Harmonic Number', I wrote the following code long long H( int  ...

  7. LightOJ1245 Harmonic Number (II)

    题意 \(求\Sigma \lfloor \frac{n}{i} \rfloor\) Input starts with an integer T (≤ 1000), denoting the num ...

  8. 1245 - Harmonic Number (II)---LightOJ1245

    http://lightoj.com/volume_showproblem.php?problem=1245 题目大意:一个数n除以1到n之和 分析:暴力肯定不行,我们可以先求1~sqrt(n)之间的 ...

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

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

随机推荐

  1. <<C++ Primer>> 术语表 (总) (待补充)

    术语表 目录 第 1 章 开始 第 I 部分 C++基础 第 2 章 变量和基本类型 第 3 章 字符串, 向量和数组 第 4 章 表达式 第 5 章 语句 第 6 章 函数 第 7 章 类 第 II ...

  2. 服务器配置好但Idea/Datagrip无法连接远程数据库的解决方案

    服务器没有开放端口3306,在云服务控制台配置安全组即可.

  3. Codeforce1196_D_F

    D RGB Substring 题意 给定一个只含RGB三种字符的字符串,问最少修改多少个字符,能使得修改后的字符串存在一个长度为\(k\)的子串是...RGBRGB...这个循环字符串的子串. 分析 ...

  4. 剑指offer-连续子数组的最大和-数组-python

    题目描述 例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止). 给一个数组,返回它的最大连续子序列的和 思路:动态规划 # -*- coding:u ...

  5. npm学习(四)之如何安装全局包、更新全局安装的包、卸载全局安装的包

    如何安装全局包 有两种方式用来安装 npm 包:本地安装和全局安装.选用哪种方式来安装,取决于你如何使用这个包. 如果你想将其作为一个命令行工具,那么你应该将其安装到全局.这种安装方式后可以让你在任何 ...

  6. DEV第三方控件的GalleryControl控件

    1.获取选中的图片 List<GalleryItem> lstArray = gclImage.Gallery.GetCheckedItems(); 2.滚动到GalleryControl ...

  7. poj 1543 Perfect Cubes (暴搜)

    Perfect Cubes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15302   Accepted: 7936 De ...

  8. 2019-2020-1 20199319《Linux内核原理与分析》第三周作业

    操作系统是如何工作的 基础知识 1.计算机的三个法宝:存储程序计算机.函数调用堆栈机制.中断. 2.堆栈的具体作用:记录函数调用框架.传输函数参数.保存返回值的地址.提供函数内部局部变量的存储空间. ...

  9. 微软推出全新的Windows终端应用程序

    微软正推出一款名为Windows Terminal的新命令行应用程序.它被设计为访问PowerShell,cmd.exe和Windows子系统Linux(WSL)等环境的中心位置.微软正在为想要调整终 ...

  10. Jmeter分布式测试dubbo接口2

    上次我们将dubbo接口与jmeter集成起来,但是jmeter是由java实现的,本身有很多限制,无法实现高并发,我们需要借助分布式来实现大压力测试. 在上次的例子中,我们只是实现了简单的dubbo ...