LightOJ1245 Harmonic Number (II) —— 规律
题目链接:https://vjudge.net/problem/LightOJ-1245
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 |
题意:
对于一个数n,求出 sigma(n/k), 1<=k<=n。
题解:
1.由于n<=2^31,直接暴力不不可能的。
2.手写一下可发现一个规律, 当 n/i = k时, i的范围为 (n/(k+1), n/k],即有n/k- n/(k+1) 个 i 使得n/i = k。
3.有了上述结论之后,就可以降低暴力程度了,设 m = sqrt(n)。先从1枚举到m,求出n/i的和,这样 [1,m] 这个区间的求值就解决了,还剩 [m+1, n] 这个区间:从m递减枚举到1,求出k*(n/k - n/(k+1))的和,这样就求出了[ n/m, n]的值,当 n/m==m时,即开始下标为m,则区间为[m,n],所以在m的位置重复计算了,需要减去m;当n/m!=m,即n/m>m时,其实下标为m+1,所以区间为 [m+1,n]。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e6+; int main()
{
int T, n, kase = ;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
int m = sqrt(n); LL ans = ;
for(int i = ; i<=m; i++) ans += n/i;
for(int i = ; i<=m; i++) ans += 1LL*i*(n/i - n/(i+));
if(n/m==m) ans -= m;
printf("Case %d: %lld\n", ++kase, ans);
}
}
LightOJ1245 Harmonic Number (II) —— 规律的更多相关文章
- LightOJ1245 Harmonic Number (II)
题意 \(求\Sigma \lfloor \frac{n}{i} \rfloor\) Input starts with an integer T (≤ 1000), denoting the num ...
- G - Harmonic Number (II) 找规律--> 给定一个数n,求n除以1~n这n个数的和。n达到2^31 - 1;
/** 题目:G - Harmonic Number (II) 链接:https://vjudge.net/contest/154246#problem/G 题意:给定一个数n,求n除以1~n这n个数 ...
- LightOJ 1245 Harmonic Number (II)(找规律)
http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS ...
- 1245 - Harmonic Number (II)(规律题)
1245 - Harmonic Number (II) PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 3 ...
- Harmonic Number (II)
Harmonic Number (II) PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB I ...
- Harmonic Number (II) 数学找规律
I was trying to solve problem '1234 - Harmonic Number', I wrote the following code long long H( int ...
- 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) 的取整的和这就是到找规律的题 ...
- 1245 - Harmonic Number (II)---LightOJ1245
http://lightoj.com/volume_showproblem.php?problem=1245 题目大意:一个数n除以1到n之和 分析:暴力肯定不行,我们可以先求1~sqrt(n)之间的 ...
随机推荐
- "从客户端中检测到有潜在危险的 Request.Form 值"的解决方案汇总
#事故现场 在一个asp.net 的项目中,前端通过ajax将富文本中的文字内容post到服务端的一个ashx中,在ashx中尝试读取参数值时, 结果报错:“从客户端中检测到有潜在危险的 Reques ...
- 洛谷—— P2251 质量检测
https://www.luogu.org/problemnew/show/P2251 题目背景 无 题目描述 为了检测生产流水线上总共N件产品的质量,我们首先给每一件产品打一个分数A表示其品质,然后 ...
- SQL SERVER 内存
http://www.cnblogs.com/CareySon/archive/2012/08/16/HowSQLServerManageMemory.html
- Xcode搭建真机调试环境 图文实例
本文介绍的Xcode搭建真机调试环境 图文实例,图文并茂,使我们学习起来更方便些,我们先来看内容. AD: 2013云计算架构师峰会超低价抢票中 Xcode搭建真机调试环境 是本文要介绍的内容,不多说 ...
- Andriod PopupWindow 键盘冲突
调起键盘的时候,弹出PopupWindow,但是键盘没有隐藏. private void init() { View contentView = LayoutInflater.from(mContex ...
- C#中通过反射获取类中非公有成员
public class NGlbGlobeXComm { public static T GetPrivateField<T>(object instance, string field ...
- Projective Texture的原理与实现 【转】
Projective Texture是比较常见的一种技术,实现起来代码也就区区的不过百行,了解其原理及技术细节是我们的重点,知其然,知其所以然. 粗略的说就是想象场景 ...
- web前端面试系列 - 算法( 数组去重 )
1. 思路:设置一个临时数组temp,然后遍历要去重的数组arr,如果arr中的元素能够在temp中找到,则跳过此元素,否则将此元素存入temp,最后返回temp. 实现一 function uniq ...
- Leetcode_num2_Maximum Depth of Binary Tree
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- (转)Understanding C parsers generated by GNU Bison
原文链接:https://www.cs.uic.edu/~spopuri/cparser.html Satya Kiran PopuriGraduate StudentUniversity of Il ...