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

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. python之OpenCv(二)---保存图像

    1.使用opencv保存图像 cv2.imwrite(存储路径,图像变量[,存盘标识]) 存盘标识: cv2.CV_IMWRITE_JPEG_QUALITY  设置图片格式为.jpeg或者.jpg的图 ...

  2. tcping 与 telnet命令粗略使用

        使用tcping命令,在网上下载tcping文件,放入c盘的system32目录下,即可使用 使用tcping命令用来ping某个端口,能通的话,说明从外部到端口是没有问题的 使用telnet ...

  3. php中加密和解密

    项目要和第三方进行接口对接,所以数据的安全很重要.第一次自己设计并实现,学习记录下 网上查了很多资料,真的很深奥 对称加密: 双方共用一个约定好的密钥进行数据的加密和解密,但是当密匙丢失,数据将有泄露 ...

  4. mac设计师系列 Adobe “全家桶” 15款设计软件 值得收藏!

    文章素材来源:风云社区.简书 文章收录于:风云社区 www.scoee.com,提供1700多款mac软件下载 Adobe Creative Cloud 全线产品均可开放下载(简称Adobe CC 全 ...

  5. CodeForces632E 神奇的多重背包

    https://cn.vjudge.net/problem/333897/origin 万万没想到这题表面上是个多重背包,实际上确实是个多重背包 题意 n种物品每种物品有无限个,每个物品有一个价格,现 ...

  6. docker的使用 -- windows

    1. 下载docker desktop https://www.docker.com/products/docker-desktop 更多操作指令 ps: 值得注意的是,刚下载下来的docker只能在 ...

  7. JavaSE_坚持读源码_ArrayList对象_Java1.7

    底层的数组对象 /** * The array buffer into which the elements of the ArrayList are stored. * The capacity o ...

  8. vue this.$router.push和this.$route.path的区别

    this.$router 实际上就是全局路由对象任何页面都可以调用 push(), go()等方法: this.$route  表示当前正在用于跳转的路由器对象,可以调用其name.path.quer ...

  9. mysql批量替换数据

    如题,项目域名迁移,导致原来商城的商品图片无法查看,地址错误. 怎么办?修改数据库图片路径呗!什么几千行呐,开玩笑.这个任务没人接,只有我干咯! 怎么也得不少时间吧,好吧半天,这是上面的要求. 有聪明 ...

  10. flask异步

    demo def runFlask(port): init() app.config[' app.run(port=port, threaded=True) CORS(app, supports_cr ...