1245 - Harmonic Number (II)(规律题)
Time Limit: 3 second(s) | Memory Limit: 32 MB |
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.
Input
Input starts with an integer T (≤ 1000),
denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤
n < 231).
Output
For each case, print the case number and H(n) calculated
by the code.
Sample Input |
Output for Sample Input |
11 1 2 3 4 5 6 7 8 9 10 2147483647 |
Case 1: 1 Case 2: 3 Case 3: 5 Case 4: 8 Case 5: 10 Case 6: 14 Case 7: 16 Case 8: 20 Case 9: 23 Case 10: 27 Case 11: 46475828386 |
题解,自己有思路,却写不出来,看了答案,倒是挺简单的;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f; int main(){
int T;
int n,cnt=;
LL res;
scanf("%d",&T);
while(T--){
res=;
scanf("%d",&n);
int m=sqrt(n);
for(int i=;i<=m;i++)res+=n/i;
for(int i=;i<=m;i++)
res+=(n/i-n/(i+))*i;
if(m==n/m)res-=m;
printf("Case %d: %lld\n",++cnt,res);
}
return ;
}
1245 - Harmonic Number (II)(规律题)的更多相关文章
- LightOJ 1245 Harmonic Number (II) 水题
分析:一段区间的整数除法得到的结果肯定是相等的,然后找就行了,每次是循环一段区间,暴力 #include <cstdio> #include <iostream> #inclu ...
- LightOJ1245 Harmonic Number (II) —— 规律
题目链接:https://vjudge.net/problem/LightOJ-1245 1245 - Harmonic Number (II) PDF (English) Statistics ...
- 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 题意:仿照上面那题他想求这么个公式的数.但是递归太慢啦.让你找公式咯. ...
- 1245 - Harmonic Number (II)---LightOJ1245
http://lightoj.com/volume_showproblem.php?problem=1245 题目大意:一个数n除以1到n之和 分析:暴力肯定不行,我们可以先求1~sqrt(n)之间的 ...
- light oj -1245 - Harmonic Number (II)
先举个例子,假如给你的数是100的话,将100/2=50;是不是就是100除100-51之间的数取整为1: 100/3=33;100除50到34之间的数为2,那么这样下去到sqrt(100);就可以求 ...
随机推荐
- 1.2UISwitch 1.3 自定义UIswitch 1.4pickerView
1.2 UISwitch创建和使用开关 问题你想给你的用户打开一个选项或关闭的能力.解使用UISwitch类. 讨论该UISwitch类提供像在图1-7为自动大写,自动校正,等等所示的开/ ...
- C#获取时间的函数
//获取日期+时间DateTime.Now.ToString(); // 2012-9-4 20:02:10DateTime.Now.ToLocalTime().ToString ...
- ##DAY5 UIControl及其子类
##DAY5 UIControl及其子类 #pragma mark ———————UIControl——————————— UIControl初识: 1)UIControl是有控制功能的视图(比如UI ...
- linux下静态链接库的用法
最近在Linux下编程发现一个诡异的现象,就是在链接一个静态库的时候总是报错,类似下面这样的错误: (.text+0x13): undefined reference to `func' 关于unde ...
- Oracle "Job定时"
今天需要做个定时器,定时到别的库导入数据用到了Job,第一次使用记录下来,如果有第一次操作的可以借鉴一下 1.首先,使用Toad新建job,进入配置页面
- python的map()函数
map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例如,对于list [1, 2 ...
- 韦根(Wiegand)数据传输格式
韦根数据传输使用TTL电平,有两条数据线,分别称为DATA0和DATA1.无数据传输时,两条线都是高电平,当传输“1”时,DATA0为高,DATA1为低:当传输“0”时,DATA0为低,DATA1为高 ...
- Json.Net系列教程 2.Net类型与JSON的映射关系
原文 Json.Net系列教程 2.Net类型与JSON的映射关系 首先谢谢大家的支持和关注.本章主要介绍.Net类型与JSON是如何映射的.我们知道JSON中类型基本上有三种:值类型,数组和对象.而 ...
- perl5 第五章 文件读写
第五章 文件读写 by flamephoenix 一.打开.关闭文件二.读文件三.写文件四.判断文件状态五.命令行参数六.打开管道 一.打开.关闭文件 语法为open (filevar, file ...
- HDOJ 1226 超级密码(bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1226 思路分析:题目要求寻找一串长度不大于500的C进制的密码,且该密码需要为十进制数N的整数倍. & ...