HDU 5879---cure

Posted kimsimple

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

打表+极限

以上是关于HDU 5879---cure的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5879 Cure

HDU 5879---cure

HDU 5879 Cure

HDU 5879 Cure -2016 ICPC 青岛赛区网络赛

[HDOJ5879]Cure(求极限,打表)

HDU4057 Rescue the Rabbit(AC自动机+状压DP)