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

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. 前端模块化,AMD与CMD的区别

    最近在研究cmd和amd,在网上看到一篇不错的文章,整理下看看. 在JavaScript发展初期就是为了实现简单的页面交互逻辑,寥寥数语即可:如今CPU.浏览器性能得到了极大的提升,很多页面逻辑迁移到 ...

  2. python字节(bytes)

    在 3.x 中,字符串和二进制数据完全区分开.文本总是 Unicode,由 str 类型表示,二进制数据则由 bytes 类型表示.Python 3 不会以任意隐式的方式混用 str 和 bytes, ...

  3. 关键字(4):grant授权/revoke回收权限

    单表授权 grant select , insert, update, delete on 表名 to 被授权用户名; grant select , insert, update, delete, r ...

  4. nginx request_time 和upstream_response_time

    1.request_time 官网描述:request processing time in seconds with a milliseconds resolution; time elapsed ...

  5. Java的内省机制

    我现在的理解就是,Java的内省机制就是针对JavaBean的,可以获取到类的属性名称,以及属性的Getter和Setter方法,应该是在写框架的时候才会用到内省机制,还有一个地方可以用到内省机制,就 ...

  6. javaweb简单登陆例子

    JSP+Servlet+JavaBean简单程序例子——用户名密码登陆,摘自<Tomcat&JavaWeb 技术手册>,亲测可用. IDE环境:MyEclipse10 1.建立We ...

  7. 阅读:重新介绍 JavaScript(JS教程)

    这篇文章是记录自己阅读重新介绍 JavaScript(JS 教程)的记录和个人体会 在线调试代码工具:https://codepen.io/pen 引言 分歧根源:名字Javascript和Java有 ...

  8. java 中对象比较大小

    java 中对象比较大小 java 中对象比较大小有两种方法 1:实现Comparable 接口 的 public int compareTo(T o) 方法: 2:实现Comparator 接口 的 ...

  9. forEach、for in、for of 三者对比

    forEach forEach专门用来循环数组,可以直接取到元素,同时也可以取到index值 存在局限性,不能continue跳过或者break终止循环,没有返回值,不能return let arr ...

  10. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)

    Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...