首页 > 代码库 > HDU 5879 Cure
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
2016 ACM/ICPC Asia Regional Qingdao Online
解析:一定要注意这句话“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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。