HDU 5879---cure

 

 

Cure

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 293    Accepted Submission(s): 96

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
 

Source

 
 

大数据量,想到打表预处理+数学|规律

 

规律:k大到一定程度,保留五位小数就不变了

 

坑:n没有给出范围,意思就是默认无限大。
RE原因,输入过大

 

 

#include "string"
#include "cstdio"
#include "iostream"
using namespace std;
#define MAX 120005
#define LL long long
double a[MAX]={0.0,1.0};
void init()
{
    for(int i=2;i<MAX;i++)
    {
        a[i]=a[i-1]+1.0/i/i;
    }
}
int main()
{
    init();
    int n,len;
    string s;
    while(cin>>s)
    {
        len=s.length();
        if(len>=7){
            printf("1.64493\n");
        }else
        {
            n=0;
            for(int i=0;i<len;i++){
                n=n*10+s[i]-'0';
                if(n>120000)
                {
                    n=120000;
                    break;
                }
            }
            printf("%.5f\n",a[n]);
        }
    }
    return 0;
}

 

打表+极限

posted @ 2017-06-29 22:58  kimsimple  阅读(148)  评论(0编辑  收藏  举报