Given an integer nn, we only want to know the sum of 1/k21/k2 where kk from 11 to nn.
Input
There are multiple cases.
For each test case, there is a single line, containing a single positive integer nn.
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
题解:这种为避免超时,肯定要提前打表,然后需要哪个值就去除,但是我们不知道给的整数多大,可能有很大的数,但是我们也很清楚,当分母很大的时候,和就趋近不变了,然后就分情况考虑输出就可以了。
代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
char a[1000006];
double sum[1000006];
int main(void){
for(int i=1;i<1000006;i++)
{
sum[i]=sum[i-1]+(double)1/i/i;
}
while(~scanf("%s",a))
{
int l=strlen(a);
if(l>=7){
printf("%.5lf\n",sum[1000000]);
}
else{
int n=0;
for(int i=0;i<l;i++){
n=n*10+a[i]-'0';
}
printf("%.5lf\n",sum[n]);
}
}
return 0;
}
本文介绍了一个求和问题的高效算法实现,通过预处理大量数据,避免了运行时的重复计算,显著提高了处理大规模数据集的速度。该算法适用于计算从1到n的1/k^2的累加和,通过对分母较大的项进行特殊处理,确保了计算精度的同时,也避免了不必要的计算资源浪费。
195

被折叠的 条评论
为什么被折叠?



