LightOJ 1245 - Harmonic Number (II)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245
题意:仿照上面那题他想求这么个公式的数。但是递归太慢啦。让你找公式咯。
题解:显然直接longlong存不下。暴力肯定不行啦。这题真的写了很久,死都不懂怎么找的公式啊。然后在wjd的帮助下懂了这题。
我们先列举几个例子
有没有发现他们的共同点,就是除到一定程度,就会变成1。这个临界点是sqrt(n)。那在sqrt(n)前面我们要算的就是这个数对于1,2,3……sqrt(n)的因子个数。
这个因子个数设为x。
n = i * x;
这个时候如果直接求是会TLE的。所以会推出来一个公式。
x = (n / i - n / (i+1) ) * i;
至于这个公式怎么出来的。网上有篇博客是画图推的。QWQ。
这个公式是求什么的呢?其实就是求 x为 1~sqrt(n) 的时候,会有几个这样的式子。
拿10举例,
x = 1 ,i = 6,7,8,9,10。 5个
x = 2 ,i = 4 , 5。 2个
x = 3 ,i = 3。 1个
i在1~sqrt(n)之间当然就可以直接求个数啦。 大胆的n/i。
把所有的x加起来就是答案。因为n可能为平方数,sqrt(n)可能多算了一次所以要减去这一次。
这个真的很难懂啊。QAQ、要多看几遍才行。
#include<iostream>
#include<cmath>
#include<cstdio>
#define ll long long
using namespace std;
int main(){
int T;
cin>>T;
int t = ;
while(T--){
int n;
cin>>n;
ll sum = ;
int tot = sqrt(n);
for(int i = ; i <= tot ;i++){
sum += n / i;
} //枚举 sqrt(n) ~ n x在1~sqrt(n)直接算
for(int i = ; i <= tot ;i++ ){
sum += (n / i - n / (i+)) * i;
} //枚举1~sqrt(n); x在后面就用公式
if( tot == n / tot )
sum -= tot;
printf("Case %d: %lld\n",t,sum);
t++;
}
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) 水题
分析:一段区间的整数除法得到的结果肯定是相等的,然后找就行了,每次是循环一段区间,暴力 #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 ...
- 1245 - Harmonic Number (II)---LightOJ1245
http://lightoj.com/volume_showproblem.php?problem=1245 题目大意:一个数n除以1到n之和 分析:暴力肯定不行,我们可以先求1~sqrt(n)之间的 ...
随机推荐
- (动态改变数据源遇到的问题)ORACLE11g:No Dialect mapping for JDBC type: -9解决方案
在动态改变数据源时 hibernate配置不能使用Oracle官方的方言(org.hibernate.dialect.Oracle10gDialect) 做法写一个方言扩展类,缺什么类型,添加什么类型 ...
- 专题:OpenSSH tunneling
SSH tunneling 相关 参考資料:http://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/ 本地端口转发: 适用场景:发起端可以 ...
- ubuntu 无pthread
由于学习多线程编程,所以用到pthread,但是man的时候却发现没有pthread函数库的手册页,然后安装 $sudo apt-get install glibc-doc 安装以后,发现还是有很多函 ...
- BBS论坛 登录功能
四.登录功能 前端页面html代码: <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- AN之数据集
一.数据集: 首先介绍数据集参数: 英文简称 英文全称 中文全称 单位 换成正常单位 说明1 说明2 Time Time 时间 小时:分钟 Temp Temperature 温度 摄氏度 ...
- rsync+inotify同步备份文件
前言 rsync作用:man rsync可以看到解释为a fast, versatile, remote (and local) file-copying tool,主要进行文件的同步. inotif ...
- Servlet - Tomcat服务器相关
1. 服务器 : 服务器其实就是代码编写的一个程序, 可以根据用户发送的请求, 调用执行对应的逻辑代码 2. Tomcat目录结构说明 : \bin : 存放启动和关闭Tomcat的可执行文件 \co ...
- react使用阿里爸爸的iconfont时,不展示的问题
选择使用Unicode时: 正常使用如下,显示也是正常: <i className="iconfont"></i> 使用map去循环时,需将原本的,改成 ...
- Linux基本使用命令
一.常用命令归纳分类 课外网站 http://man.linuxde.net/ http://www.jb51.net/linux/ http ...
- delphi xe10 蓝牙
//蓝牙 System.Bluetooth //单元中主要包含以下几个类 TBluetoothManager.TBluetoothDeviceList.TBluetoothAdapter.TBluet ...