链接:

https://vjudge.net/problem/LightOJ-1245

题意:

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.

思路:

考虑对整数n/i的上下限,下限为最大值为n/(n/i))。不断循环,相等的值直接跳过去就不会超时了。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map> using namespace std;
typedef long long LL;
const int INF = 1e9; const int MAXN = 1e7+10;
const int MOD = 1e9+7; LL n; int main()
{
int t, cnt = 0;
scanf("%d", &t);
while(t--)
{
printf("Case %d:", ++cnt);
scanf("%lld", &n);
LL p = 1;
LL sum = 0;
while(p <= n)
{
LL tmp = n/p;
LL next = n/(n/p);
sum += 1LL*tmp*(next-p+1);
p = next+1;
}
printf(" %lld\n", sum);
} return 0;
}

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

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

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

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:求f(n)=n/1+n/2.....n/n,其中n/i保留整数 显 ...

  4. LightOJ 1245 - Harmonic Number (II)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:仿照上面那题他想求这么个公式的数.但是递归太慢啦.让你找公式咯. ...

  5. LightOJ 1245 Harmonic Number (II) 水题

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

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

    题目大意:对下列代码进行优化 long long H( int n ) {    long long res = 0;    for( int i = 1; i <= n; i++ )      ...

  7. LightOJ - 1234 LightOJ - 1245 Harmonic Number(欧拉系数+调和级数)

    Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...

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

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

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

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

随机推荐

  1. Apache Kafka主题 - 架构和分区

    1.卡夫卡话题 在这篇Kafka文章中,我们将学习Kafka主题与Kafka Architecture的整体概念.Kafka中的体系结构包括复制,故障转移以及并行处理.此外,我们还将看到创建Kafka ...

  2. PAT(B) 1035 插入与归并(Java)

    题目链接:1035 插入与归并 (25 point(s)) 参考博客:PAT乙级--1035(插入排序和归并)java实现熊仙森 题目描述 根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据, ...

  3. 使用 Angular RouteReuseStrategy 缓存(路由)组件

    使用 Angular RouteReuseStrategy 缓存组件 Cache components with Angular RouteReuseStrategy RouteReuseStrate ...

  4. html2canvas以及domtoimage的使用踩坑总结 动态获取的二维码失效如何生成海报

    //判断手机为安卓还是ios 安卓html2canvas方法 ios系统dom-to-image方法 $(".code").click(function() { var u = n ...

  5. Codeforces Round #570 Div. 3

    A:暴力从小到大枚举判断. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 10 ...

  6. (转)项目迁移_.NET项目迁移到.NET Core操作指南

    原文地址:https://www.cnblogs.com/heyuquan/p/dotnet-migration-to-dotnetcore.html 这篇文章,汇集了大量优秀作者写的关于" ...

  7. SQL递归获取树型路径中文名称

    项目中遇到一个树型结构表要根据任意传入节点获取它从根节点一直到自身节点的全部路径的中文名称,并且用'>'与分隔. 我使用在sqlServer中写了一个解析函数方便开发调用. USE [RP_ER ...

  8. ③ Python3.0 数字类型

    Python3 的六个标准数据类型中:不可变数据(3 个):Number(数字).String(字符串).Tuple(元组):可变数据(3 个):List(列表).Dictionary(字典).Set ...

  9. 作为消费者访问提供者提供的功能(eureka的铺垫案例)

    1. 实体类.提供者的创建如本随笔者的Euraka适合初学者的简单小demo中有所展示 2. 创建子工程作为消费者 (1) 添加依赖:切记引入实体类的依赖 <dependencies> & ...

  10. linux设置网卡速率

    ethtool # ethtool ethX //查询ethX网口基本设置 # ethtool –h //显示ethtool的命令帮助(help) # ethtool –i ethX //查询ethX ...