链接:

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. GCC 基础知识

    目录 GCC 基础知识 一.GCC编译选项解析 二.多模块.多个文件一起编译 三.静态库与动态库 四.查看帮助文档 GCC 基础知识 一.GCC编译选项解析 1. 常用编译选项 命令格式:gcc [选 ...

  2. 44 容器(三)——ArrayList索引相关方法

    方法都比较简单,这里列出来即可: add(index,ele) //忘制定下标插入元素 add(ele) addAll(Collection <C> c) 泛型必须与调用add的泛型保持一 ...

  3. Microsoft的考验――查找第二大的数

    #include<stdio.h> int main() { int n,m,t,max,max1; scanf("%d",&n); while(n--) { ...

  4. Charles4.2.8抓包(http+https)

    Charles 和 Fiddler 一样都是http抓包工具. 之前用 Fiddler 抓个别 ios 手机 https 报文时总卡在哪里返不回任何数据,后来怀疑是 Fiddler 问题,就考虑使用  ...

  5. Matlab图像处理基础知识

    Matlab图像处理基础知识 Matlab的图片以矩阵的形式存储,矩阵的行列值为图片的行列的色彩值. 1图像表达方式: 像素索引 图像被视为离散单元.如使用I(2,2)可以获取第二行第二列的像素值 空 ...

  6. (五) Docker 安装 Nginx

    参考并感谢 官方文档 https://hub.docker.com/_/nginx 下载nginx镜像(不带tag标签则表示下载latest版本) docker pull nginx 启动 nginx ...

  7. NEST routing timeout scroll

    /// <summary> /// PUT /employee/employee/9e5e50da-7740-488e-bee2-b24951435691?routing=test_rou ...

  8. Linux虚拟机设置静态ip

    二.设置静态ip dhclient 动态分配ip 修改 ifcfg-ens33网卡配置文件  静态分配ip dhclient -r (释放动态分配的ip地址) vi /etc/sysconfig/ne ...

  9. js --装饰者模式

    定义 装饰者模式能够在补改变对象自身的基础上,在程序运行期间给对象动态的添加职责. 当看到装饰者模式的定义的时候,我们想到的js 的三大特性之一--继承,不也能够实现不改变对象自身的基础上,添加动态的 ...

  10. CSS知识整理

    1. 权重问题(CSS优先级): 继承或 * :0,0,0,0 标签:0,0,0,1 每个类,伪类:0,0,1,0 每个ID:0,1,0,0 每个行内式:1,0,0,0 !important:无穷大 ...