LightOJ - 1245 - Harmonic Number (II)(数学)
链接:
https://vjudge.net/problem/LightOJ-1245
题意:
I was trying to solve problem '1234 - Harmonic Number', I wrote the following code
long long H( int n ) {
long long res = 0;
for( int i = 1; i <= n; i++ )
res = res + n / i;
return res;
}
Yes, my error was that I was using the integer divisions only. However, you are given n, you have to find H(n) as in my code.
思路:
考虑对整数n/i的上下限,下限为最大值为n/(n/i))。不断循环,相等的值直接跳过去就不会超时了。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int MAXN = 1e7+10;
const int MOD = 1e9+7;
LL n;
int main()
{
int t, cnt = 0;
scanf("%d", &t);
while(t--)
{
printf("Case %d:", ++cnt);
scanf("%lld", &n);
LL p = 1;
LL sum = 0;
while(p <= n)
{
LL tmp = n/p;
LL next = n/(n/p);
sum += 1LL*tmp*(next-p+1);
p = next+1;
}
printf(" %lld\n", sum);
}
return 0;
}
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)找规律
题目链接: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 - 1245 Harmonic Number (II) 求同值区间的和
题目大意:对下列代码进行优化 long long H( int n ) { long long res = 0; for( int i = 1; i <= n; i++ ) ...
- 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 ...
- Harmonic Number (II) 数学找规律
I was trying to solve problem '1234 - Harmonic Number', I wrote the following code long long H( int ...
随机推荐
- [转帖]helm模板文件chart编写语法详解
helm模板文件chart编写语法详解 https://blog.51cto.com/qujunorz/2421328 需要学习一下. charts编写介绍 开始 快速创建一个chart模板,helm ...
- python with方法
在实际的编码过程中,有时有一些任务,需要事先做一些设置,事后做一些清理,这时就需要python with出场了,with能够对这样的需求进行一个比较优雅的处理,最常用的例子就是对访问文件的处理. 一般 ...
- flask报错 KeyError: <flask.cli.ScriptInfo object at 0x000001638AC164E0>
(flask_venv) D:\DjangoProject\flask_test>flask db init Traceback (most recent call last): File &q ...
- 最简单的centos上安装Nginx办法
基本几个简单命令就能搞定 由于yum源中没有我们想要的nginx,那么我们就需要创建一个“/etc/yum.repos.d/nginx.repo”的文件,其实就是新增一个yum源. vi /etc/y ...
- diverta 2019 Programming Contest 2
A:签到. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 ...
- java 正则和连接json
前面已经写了不少关于C# 怎么使用正则,有兴趣,可以翻译成java代码. 以图片为例子: import java.util.regex.Matcher; import java.util.regex. ...
- 大数据相关技术原理资料整理(hdfs, spark, hbase, kafka, zookeeper, redis, hive, flink, k8s, OpenTSDB, InfluxDB, yarn)
hdfs: hdfs官方文档 深入理解HDFS的架构和原理 https://blog.csdn.net/kezhong_wxl/article/details/76573901 HDFS原理解析(总体 ...
- 使用的一些支持swift3.0的开源库
#解决键盘弹起遮挡工具 pod 'IQKeyboardManagerSwift', '~>4.0.6' #多种类型弹出框 pod 'SCLAlertView', :git => 'http ...
- day35-python之协程
1.协程 # import time # import queue # # def consumer(name): # # print("--->ready to eat baozi. ...
- urlib库
urllib库是python中最基本的网络请求库,可以模拟浏览器的行为,向指定的服务器发送请求,并可以保存服务器返回的数据. urlopen() urllib.request模块提供了最基本的构造ht ...