题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5879
Problem Description
Given an integer n, we only want to know the sum of 1/k2 where k from 1 to n.
这一题当n大到一定程度的时候值就不在变化了,我也就枚举到15万,加的时候倒着来
AC代码:
# include <stdio.h>
using namespace std;
typedef long long int ll;
double ans[150011];
int main(){
ll i, j, k, n;
ans[150010]=(double)1.0/150010/150010;
for(i=150009; i>=1; i--){
ans[i]=ans[i+1]+(double)1.0/(i)/(i);
}
while(scanf("%lld", &n)!=EOF){
if(n>150009||n<=0){
printf("%.5lf\n", ans[1]);
}
else{
printf("%.5lf\n", ans[1]-ans[n+1]);
}
}
return 0;
}