lightoj 1245 - Harmonic Number (II) 【数学 计数】
2015-11-08 14:57
447 查看
1245 - Harmonic Number (II)
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.
Each case starts with a line containing an integer n (1 ≤ n < 231).
PROBLEM SETTER: JANE ALAM JAN
题意:给你一个n,让你按照给出的程序求解结果。
太弱了,比赛时没找到规律。。。
令m = sqrt(n)。
ans = sigma( (n / i - n / (i+1)) * i) + n / i)。【1 <= i <= m】
ans -= n / m == m ? n / m : 0。
AC代码:
![]() | PDF (English) | Statistics | Forum |
Time Limit: 3 second(s) | Memory Limit: 32 MB |
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 |
PROBLEM SETTER: JANE ALAM JAN
题意:给你一个n,让你按照给出的程序求解结果。
太弱了,比赛时没找到规律。。。
令m = sqrt(n)。
ans = sigma( (n / i - n / (i+1)) * i) + n / i)。【1 <= i <= m】
ans -= n / m == m ? n / m : 0。
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <vector> #define INF 0x3f3f3f3f #define eps 1e-8 #define MAXN (100000+10) #define MAXM (50000000) #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rf(a) scanf("%lf", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 using namespace std; int main() { int t; Ri(t); int kcase = 1; W(t) { LL n; Rl(n); LL ans = 0; LL m = sqrt(n); for(LL i = 1; i <= m; i++) ans += (n / i - n / (i+1)) * i + n / i; if(m == n / m) ans -= n / m; printf("Case %d: %lld\n", kcase++, ans); } return 0; }
相关文章推荐
- 第三章 分支结构程序
- objective-c系列-NSString
- 内存溢出(OOM)and内存泄露---及其解决
- Ubuntu14.04+hadoop2.5.2完全分布式集群搭建
- MySQL常用命令
- matrix_world_final_2012
- Light oj1354:IP Checking
- view getwidth() 和getMeasuredWidth()区别
- java中线程安全的讲解
- 程序猿书单--转载
- 转自一个CG大神的文章
- D3.js数据可视化(三)——地图可视化
- 二叉树的各种遍历算法的递归和非递归实现
- S3C2440 NAND Flash的使用
- C++实现动态绑定代码分享
- 一个关于二维指针的问题
- 数据结构实验之栈八:栈的基本操作
- Property's synthesized getter follows Cocoa naming convention for returning
- nyoj62笨小熊
- python之模块calendar(汇集了日历相关的操作)