HDU 5879 Cure
Posted 月夜下
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 5879 Cure相关的知识,希望对你有一定的参考价值。
Cure
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1333 Accepted Submission(s): 440
Problem Description
Given an integer n, we only want to know the sum of 1/k2 where k from 1 to n.
Input
There are multiple cases.
For each test case, there is a single line, containing a single positive integer n.
The input file is at most 1M.
For each test case, there is a single line, containing a single positive integer n.
The input file is at most 1M.
Output
The required sum, rounded to the fifth digits after the decimal point.
Sample Input
1
2
4
8
15
Sample Output
1.00000
1.25000
1.42361
1.52742
1.58044
Source
解析:一定要注意这句话“The input file is at most 1M.”坑点所在。输入的长度可能达到1e6。这个极限为PI*PI/6,保留5位小数就是1.64493。n达到1000000左右以后,结果就不会再变了。
#include <cstdio> #include <cstring> const int MAXN = 1e6+5; double sum[MAXN]; char s[MAXN]; void init() { sum[0] = 0; for(int i = 1; i <= 1000000; ++i){ sum[i] = sum[i-1]+1.0/(i*1.0*i); } } int main() { init(); while(~scanf("%s", s)){ int len = strlen(s); if(len >= 7){ printf("1.64493\n"); continue; } int n = 0; for(int i = 0; i < len; ++i) n = n*10+s[i]-‘0‘; printf("%.5f\n", sum[n]); } return 0; }
以上是关于HDU 5879 Cure的主要内容,如果未能解决你的问题,请参考以下文章