lightoj--1245--Harmonic Number (II)(数学推导)
| Time Limit: 3000MS | Memory Limit: 32768KB | 64bit IO Format: %lld & %llu |
Description
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
11
1
2
3
4
5
6
7
8
9
10
2147483647
Sample Output
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
Source
/很好的题解:转载的
先求出前sqrt(n)项和:即n/1+n/2+...+n/sqrt(n)
再求出后面所以项之和.后面每一项的值小于sqrt(n),计算值为1到sqrt(n)的项的个数,乘以其项值即可快速得到答案
例如:10/1+10/2+10/3+...+10/10
sqrt(10) = 3
先求出其前三项的和为10/1+10/2+10/3
在求出值为1的项的个数为(10/1-10/2)个,分别是(10/10,10/9,10/8,10/7,10/6),值为2个项的个数(10/2-10/3)分别是(10/5,10/4),在求出值为3即sqrt(10)的项的个数.
显然,值为sqrt(10)的项计算了2次,减去一次即可得到答案。当n/(int)sqrt(n) == (int)sqrt(n)时,值为sqrt(n)的值会被计算2次。
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
int t,n,i,j;
int T=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
long long ans=0;
for(i=1;i<=(int)sqrt(n);i++)
{
ans+=n/i;
if(n/i>n/(i+1))
ans+=(long long)((n/i-n/(i+1))*i);
}
i--;
if(n/i==i)
ans-=i;
printf("Case %d: %lld\n",T++,ans);
}
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)(数学)
链接: 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 - 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 ...
随机推荐
- B - Guess a number!
Problem description A TV show called "Guess a number!" is gathering popularity. The whole ...
- java selenium手动最大化chrome浏览器的方法
package my_automation; import java.awt.Dimension; import org.openqa.selenium.Capabilities; import or ...
- Spring《八》AOP/代理类定义
Spring通知 Interception Around通知 MethodInterceptor类(方法执行前后通知) Before通知 MethodBeforeAdvice类(方法执行前通知) Af ...
- 让.bashrc文件在终端自动生效
修改了.bashrc文件,想在打开终端时默认路径变成桌面路径.代码如下 cd ~/desktop export PATH="/Users/nola/local:$PATH" 但是每 ...
- WebStorm2018.2 破解 激活
1.进入http://idea.lanyus.com/,如图: 2.下载http://idea.lanyus.com/jar/JetbrainsCrack-3.1-release-enc.jar . ...
- Docker-compose Setup for Self-hosting Development & Deployment Tools
Last week I wrote about my self-hosted Sentry install in 3 Docker containers. This week I want to br ...
- dll文件:关于MFC程序不能定位输入点
问题:无法定位程序输入点到动态链接库上...... 过程:找完整个工程文件夹: APS为资源文件: resource.h为定义文件: 完全复制 除主文件以外的所有文件,程序可用: 在主程序框Cpp文件 ...
- JS 从36个数字里面随机抽取8个
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 网络是通的 yum用不了
Loaded plugins: fastestmirror, langpacks One of the configured repositories failed (Unknown),and yum ...
- C++ STL-stack使用详解
stack 类是容器适配器,它给予程序员栈的功能--特别是 FILO (先进后出)数据结构. 该类模板表现为底层容器的包装器--只提供特定函数集合.栈从被称作栈顶的容器尾部推弹元素. 一:头文件 #i ...