题目大意:对下列代码进行优化

long long H( int n ) {
    long long res = 0;
    for( int i = 1; i <= n; i++ )
        res = res + n / i;
    return res;
}

题目思路:为了避免超时,要想办法进行优化

以9为例:

9/1 = 9

9/2 = 4

9/3 = 3

9/4 = 2

9/5 = 1

9/6 = 1

9/7 = 1

9/8 = 1

9/9 = 1

拿1来看,同为1的区间长度为:9/(9/5)+1-5,

得出通式:值相同的区间长度为:n/(n/i)+1-i。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define MAXSIZE 1000005
#define LL long long using namespace std; int main()
{
int T,cns=;
LL n;
scanf("%d",&T);
while(T--)
{
scanf("%lld",&n);
LL i=;
LL ans=;
while(i<=n)
{
ans=ans+(n/(n/i)-i+)*(n/i);
i=n/(n/i)+;
}
printf("Case %d: %lld\n",cns++,ans);
}
return ;
}

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)(数学)

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

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

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

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

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

  5. LightOJ 1245 - Harmonic Number (II)

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

  6. LightOJ 1245 Harmonic Number (II) 水题

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

  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. 1245 - Harmonic Number (II)---LightOJ1245

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

随机推荐

  1. mysql安装和操作

    1.install: 下载地址:https://dev.mysql.com/downloads/ 2.下载zip包解压: 3.自己在该文件夹下创建 my.ini,并编辑内容: [mysql] # 设置 ...

  2. Unity 网络编程(Socket)应用

    服务器端的整体思路: 1.初始化IP地址和端口号以及套接字等字段: 2.绑定IP启动服务器,开始监听消息  socketServer.Listen(10): 3.开启一个后台线程接受客户端的连接 so ...

  3. ansible的主机的默认配置部分

    Ansible默认安装好后有一个配置文件/etc/ansible/ansible.cfg,该配置文件中定义了ansible的主机的默认配置部分,如默认是否需要输入密码.是否开启sudo认证.actio ...

  4. 考虑浏览器兼容的文件上传(IE8不支持FormData)

    方法一:使用FormData(因IE8不支持FormData, IE10才支持,因此此方法不兼容IE10以下的IE浏览器) 也可参考文章 http://www.jianshu.com/p/46e6e0 ...

  5. 富文本框vue-quill-editor的使用

    vue-quill官网:https://www.npmjs.com/package/vue-quill-editor quill官网:https://quilljs.com/docs/quicksta ...

  6. docker swarm(当前官网示例)

    介绍 Docker Swarm 是 Docker 公司推出的官方容器集群平台,基于 Go 语言实现 作为容器集群管理器,Swarm 最大的优势之一就是 100% 支持标准的 Docker API.各种 ...

  7. C++回顾day01---<const常量重点>

    一:定义常整型数 const int a;(或者int const a;)  不涉及指针 不可改变值(也不可通过指针修改) 二:定义一个指向常整型数的指针 const int* c;   可改指针指向 ...

  8. zookeeper下的基本操作

    安装好zk之后 启动服务端:在bin目录下 zkServer.sh restart 启动客户端 zkCli.sh 检查是否启动 直接输入jps命令,显示下面则表示启动成功 [root@iZbp12gg ...

  9. 从Paxos到Zookeeper分布式一致性原理与实践 读书笔记之(一) 分布式架构

    1.1 从集中式到分布式 1 集中式特点 结构简单,无需考虑对多个节点的部署和节点之间的协作. 2  分布式特点 分不性:在时间可空间上随意分布,机器的分布情况随时变动 对等性:计算机之间没有主从之分 ...

  10. Hadoop记录-hive操作示范