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
首先他说输入文件不超过1m,每个样例就是一个数,可见样例会很大,所以最好是打表,排着算一定超时,再者没有给出n的范围,输出要求保留五位小数,如果n特别大后面估计都是一样的,通过打表就可以发现,我们只需要打1000000以内的即可,实际可以更小,因为从某个位置开始的结果都是1.64493,所以分情况来讨论。
代码:
#include <iostream> #include <cstdio> #include <cstring> #define MAX 10000000 #define DMAX 1000000 #define MOD 1000000007 using namespace std; typedef long long ll; char num[MAX + 1]; double sum[DMAX]; int main() { for(long long i = 1;i < 1000000;i ++) { sum[i] = sum[i - 1] + 1.0 / (i * i); } while(~scanf("%s",num)) { double ans = 0; if(strlen(num) > 6) ans = 1.64493; else { int d = 0; for(int i = 0;num[i];i ++) { d = d * 10 + num[i] - '0'; } ans = sum[d]; } printf("%.5f ",ans); } }