Cure
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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.
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
这道题其实就是精度问题,1/k^2,k越大,值就越小,我们打个表会发现,到12万左右值就卡在1.64493,
这里我打到了100000000试了一下,依旧是这个值,所以我们就可以打个12w左右的表。
注意这里范围没有给,应该是字符串输入。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define Pi 4.0*atan(1.0)
#define MOD 1000000007
#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ls rt<<1
#define rs rt<<1|1
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 50010;
using namespace std;
//#define TIME
const int N=100000+5;
int a[N];
inline int read(){
int x(0),f(1);
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
double Sum[120010];
int main()
{
//fin;
//fout;
int j;
double long sum = 0;
Sum[0] = 0;
for(double long i = 1; i <= 120000; ++i){
sum += (double long)1/(i*i);
j = (int)i;
Sum[j] = sum;
}
string str;
int len;
int num;
while(cin >> str){
len = str.size();
if(len >= 7){
printf("1.64493\n");
}
else{
sscanf(str.c_str(), "%d", &num);
if(num > 120000){
printf("1.64493\n");
}
else{
printf("%.5lf\n", Sum[num]);
}
}
}
return 0;
}