LightOJ 1245 Harmonic Number (II)
2015-10-09 14:45
561 查看
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/G
Harmonic Number (II)
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
题意:给定一个n,让你求Σn/i,i从1->n.
解析:例如10
那么10/1=10;
10/2=5;
则n/i为1的数和为 1*(10-5);
同时对应着n/1的数为36,是以两段对应和为5+1*(10-5);
同理,10/3=3, 和为 5+2*(5-3)
...
当n/i和i产生重合或者交叉时,就可以退出策画了
全部策画流程如下
i 1 2 3 4 5 6 7 8 9 10
n/i 10 5 3 2 2 1 1 1 1 1
如上所示,在策画i=1时,ans+=10, ans+=10-5
Harmonic Number (II)
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
题意:给定一个n,让你求Σn/i,i从1->n.
解析:例如10
那么10/1=10;
10/2=5;
则n/i为1的数和为 1*(10-5);
同时对应着n/1的数为36,是以两段对应和为5+1*(10-5);
同理,10/3=3, 和为 5+2*(5-3)
...
当n/i和i产生重合或者交叉时,就可以退出策画了
全部策画流程如下
i 1 2 3 4 5 6 7 8 9 10
n/i 10 5 3 2 2 1 1 1 1 1
如上所示,在策画i=1时,ans+=10, ans+=10-5
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <cstdlib> #include <limits> #include <queue> #include <stack> #include <vector> #include <map> using namespace std; #define N 10005000 #define INF 0x3f3f3f3f #define PI acos (-1.0) #define EPS 1e-8 #define P (1000000000+7) #define met(a, b) memset (a, b, sizeof (a)) typedef long long LL; int main () { int t, n, nCase = 1; scanf ("%d", &t); while (t--) { scanf ("%d", &n); LL ans = 0, i; for (i=1; i<=sqrt (n); i++) { ans += n / i; if (n / i > n / (i+1)) ans += (n / i - n / (i+1)) * i; } if (n / (i - 1) == i - 1)//判断重合的时候要减去重复的 ans -= n / (i - 1); printf ("Case %d: %lld\n", nCase++, ans); } return 0; }
相关文章推荐
- C语言fillpoly函数详解
- JavaScript子窗口调用父窗口变量和函数的方法
- MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建
- socket断续字节初探
- 如何通过java反编译工具嵌入myeclipse来跟进查看源码
- OpenXml操作Word的一些操作总结.无word组件生成word.
- LinQ操作
- 智能硬件+App移动新生态【11.01深圳】
- smarty 自定义函数
- 更新Cocos2D支持Xcode 7
- 如何查看真机的沙盒(图文教程)
- 更新Cocos2D支持Xcode 7
- 更新Cocos2D支持Xcode 7
- MATLAB中如何输出漂亮的公式
- JQuery EasyUI DataGrid 纵向(转置)表格插件 TransposedView
- NGINX开篇
- General Problem Solving Techniques [Intermediate-1]~D - Fabled Rooks
- oracle 第02章 基本操作
- quick-cocos2d-x数据存储 UserDefault GameState io
- 系统设计之BS与CS结构分析