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

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. echarts柱状图 渐变色

    效果图:  var xAxisData = []; var data = []; for (var i = 9; i < 16; i++) { xAxisData.push('5月' + i + ...

  2. AOP和IOC

    AOP切面编程,作用:事务,日志,统一调用,统一实现,拦截,分发: 切点:告诉编译器切面作用于哪个package中的class IOC:控制反转,相对于new 对象来说的,依赖注入:AuthorWar ...

  3. (基础) 平方和与立方和 hdu2007

    平方和与立方和 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2007 Time Limit: 2000/1000 MS (Java/Others)     ...

  4. go操作redis和mysql示例

    一:redis示例 使用redis的包是: github.com/garyburd/redigo/redis 1:编写第一个示例: 链接,设置,获取 redis_basic.go package ma ...

  5. Pyhton之subprocess模块和configparser模块

    一.subprocess模式 # import os # while True: # cmd=input('>>').strip() # if not cmd:continue # if ...

  6. org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].Standard

    Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lan ...

  7. 1042. Shuffling Machine (20)

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  8. 20165232 2017-2018-2《Java程序设计》结对编程一 第一周总结

    20165232 2017-2018-2<Java程序设计>结对编程一 第一周总结 结对对象 20165219王彦博 20165232何彦达 需求分析 实现一个程序,要求: 1 支持整数运 ...

  9. Mysql:性能优化

    性能优化 优化MySQL数据库是数据库管理员和数据库开发人员的必备技能.MySQL优化,一方面是找出系统的瓶颈,提高MySQL数据库的整体性能:一方面需要合理的结构设计和参数调整,以提高用户操作响应的 ...

  10. windows批量修改文件后缀名

    有时候需要批量修改一些文件的后缀名,下面介绍批量修改的方法. 1.在文件夹内新建一个.txt文本文档. 2.在文本文档内写:ren *    *.mp3 (意思是把没有后缀名的全部改成.mp3的格式, ...