惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
N
Netflix TechBlog - Medium
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
宝玉的分享
宝玉的分享
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
量子位
Blog — PlanetScale
Blog — PlanetScale
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
阮一峰的网络日志
阮一峰的网络日志
D
Docker
罗磊的独立博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
IT之家
IT之家
MyScale Blog
MyScale Blog
Microsoft Azure Blog
Microsoft Azure Blog

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
HDU 5879.Cure(2016 ACM/ICPC Asia Regional Qingdao Online)...
2016-09-17 · via OhYee 博客

这是一篇最后编辑于 8 年前 的文章,其内容可能与目前实际情况差异较大,请注意甄别

题目

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

题解

Σ1/n2
由于该式在 n→∞ 时,结果为 π<sup>2</sup>/6 ( ≈1.64493 )
由于只需要输出 5 位小数,因此当 n 足够大时,只需要输出一个确定值( 1.64493 )即可

本地调试测试可知,当数据大于 200000 时,就可以放心输出 1.64493
因此可以先打表计算 200005 内的值,当读入的数大于 200005 时,直接输出即可

由于 n 的大小不确定,因此应该高精度读入(注意前导 0 )
然后类似高精度的比较大小即可

代码

/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリ0隶�
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

typedef long long LL;

const int INF = 0x7FFFFFFF;
const double eps = 1e-10;

const int maxn = 200005;

LL n;
double ans[maxn];
char s[1000000];

char cmp[] = "200000";

int read_string(char s[]) {
    char c;
    int i = 0;
    while(!((c = getchar()) >= '1' && c <= '9'))
        if(c == EOF)
            return -1;
    while(c >= '0'&&c <= '9') {
        s[i++] = c;
        c = getchar();
    }
    s[i] = '\0';
    return i;
}

bool cmp_biger(char *a,char *b) {
    int len1 = strlen(a);
    int len2 = strlen(b);
    if(len1 > len2)
        return true;
    else if(len1 == len2) {
        len1 = 0;
        while(a[len1] == b[len1] && len1 <= len2)
            len1++;
        if(len1 <= len2 && a[len1] > b[len1])
            return true;
        else
            return false;
    } else
        return false;
}

bool Do() {
    if(read_string(s) == -1)
        return false;

    //cout << cmp << " " << s << endl << "    ";

    if(cmp_biger(cmp,s)) {
        int len = strlen(s);
        n = 0;
        for(int i = 0;i < len;i++)
            n = n * 10 + s[i] - '0';
        cout << fixed << setprecision(5) << ans[n] << endl;
    } else {
        cout << "1.64493" << endl;
    }
    return true;
}


int main() {
    cin.tie(0);
    cin.sync_with_stdio(false);


    ans[1] = 1.0;
    for(int i = 2;i < maxn;i++)
        ans[i] = ans[i - 1] + 1 / (double)i / (double)i;


    while(Do());

    return 0;
}