LightOJ - 1245 Harmonic Number (II) 求同值区间的和
题目大意:对下列代码进行优化
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) 求同值区间的和的更多相关文章
- LightOJ 1245 Harmonic Number (II)(找规律)
http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS ...
- LightOJ - 1245 - Harmonic Number (II)(数学)
链接: https://vjudge.net/problem/LightOJ-1245 题意: I was trying to solve problem '1234 - Harmonic Numbe ...
- LightOj 1245 --- Harmonic Number (II)找规律
题目链接:http://lightoj.com/volume_showproblem.php?problem=1245 题意就是求 n/i (1<=i<=n) 的取整的和这就是到找规律的题 ...
- lightoj 1245 Harmonic Number (II)(简单数论)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:求f(n)=n/1+n/2.....n/n,其中n/i保留整数 显 ...
- LightOJ 1245 - Harmonic Number (II)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:仿照上面那题他想求这么个公式的数.但是递归太慢啦.让你找公式咯. ...
- LightOJ 1245 Harmonic Number (II) 水题
分析:一段区间的整数除法得到的结果肯定是相等的,然后找就行了,每次是循环一段区间,暴力 #include <cstdio> #include <iostream> #inclu ...
- LightOJ - 1234 LightOJ - 1245 Harmonic Number(欧拉系数+调和级数)
Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...
- 1245 - Harmonic Number (II)(规律题)
1245 - Harmonic Number (II) PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 3 ...
- 1245 - Harmonic Number (II)---LightOJ1245
http://lightoj.com/volume_showproblem.php?problem=1245 题目大意:一个数n除以1到n之和 分析:暴力肯定不行,我们可以先求1~sqrt(n)之间的 ...
随机推荐
- php-resque 轻量级队列
一:简介 github地址:https://github.com/chrisboulton/php-resque 这个轻量级队列是由 Ruby 开发的 Resque 启发而来的. 注意:1. php- ...
- Zabbix Web API Get方法整合
#!/usr/bin/python3 import requests,json,re,time,datetime url = 'http://xxx.com/api_jsonrpc.php' ...
- Linux下FTP虚拟账号环境部署简述
vsftp的用户有三种类型:匿名用户.系统用户.虚拟用户.1)匿名登录:在登录FTP时使用默认的用户名,一般是ftp或anonymous.2)本地用户登录:使用系统用户登录,在/etc/passwd中 ...
- lucene创建索引的几种方式(一)
什么是索引: 根据你输入的值去找,这个值就是索引 第一种创建索引的方式: 根据文件来生成索引,如后缀为.txt等的文件 步骤: 第一步:FSDirectory.open(Paths.get(url)) ...
- qml性能优化(来源于群友分享);
Qt quick性能优化 使用时间驱动 避免定时轮询: 使用信号槽形式: 使用多线程 C++; QML WorkerScript元件: 使用Qt Quick Compiler 只需要再PRO文件中添加 ...
- RHCSA考试真题
2018年 RHCSA考试真题... ------------ 考前需要做的基础 破解root密码 KVM虚拟机与VM虚拟机 主机名:station.domain1.example.comIP地址:1 ...
- JAVA核心技术I---JAVA基础知识(映射Map)
一:映射Map分类 二:Hashtable(同步,慢,数据量小) –K-V对,K和V都不允许为null –同步,多线程安全 –无序的 –适合小数据量 –主要方法:clear, contains/con ...
- python 内置函数,匿名函数,sorted,filter,map,递归,二分法,冒泡算法 eval
############################总结#################################1. lambda 匿名函数 语法——lambda 参数:返回值 __na ...
- JAVA-8大基本类型与包装类的例子(基础必备)
package com.net.xinfang.reflect; /*** * 8种基本类型(byte,int,short,long,float,double,char,boolean) * 布尔型只 ...
- idea创建父子工程
第一步:创建一个新的父工程father:file—–>new—->project ,注意要选maven,Create from archetype不要勾选.next填写GroupId .A ...