当前位置 : 主页 > 编程语言 > c语言 >

2016 ACM/ICPC Asia Regional Qingdao Online B

来源:互联网 收集:自由互联 发布时间:2023-09-06
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 requi

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/n^2) 的极限为 pi^2/6。。。。。当然是趋近很大的时候,所以简单设置一下上界即可=。= (送分题)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 8000005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-7
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
    ll ans = 1;
    a = a % mod;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

double sum[maxn];


int main()
{
    //ios::sync_with_stdio(false);
    string s;
    for (int i = 1; i <= 1000000; i++) {
        sum[i] = sum[i - 1] + 1.0 / i / i;
    }
    while (cin>>s) {
        int len = s.length();
        //printf("%.5f\n", 1.0*pi*pi / 6);
        if (len >= 7)
            printf("%.5f\n", 1.0*pi*pi / 6);
        else {
            int n = 0;
            for (int i = 0; i < len; i++) {
                n = n * 10 + s[i] - '0';
            }
            printf("%.5f\n", 1.00*sum[n]);
        }
    }
}
网友评论