2016 ACMICPC亚洲区域青岛在线B题的解题思路是什么?

2026-04-11 21:571阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计529个文字,预计阅读时间需要3分钟。

2016 ACM/ICPC亚洲区域青岛在线B题的解题思路是什么?

python简化后的代码

def sum_of_reciprocal_squares(n): return sum(1 / i**2 for i in range(1, n + 1))

读取输入并处理多个测试案例import sysinput=sys.stdin.readdata=input().split()

for line in data: if line.strip(): n=int(line.strip()) result=sum_of_reciprocal_squares(n) print(result)

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

2016 ACM/ICPC亚洲区域青岛在线B题的解题思路是什么?

Σ(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]); } } }

本文共计529个文字,预计阅读时间需要3分钟。

2016 ACM/ICPC亚洲区域青岛在线B题的解题思路是什么?

python简化后的代码

def sum_of_reciprocal_squares(n): return sum(1 / i**2 for i in range(1, n + 1))

读取输入并处理多个测试案例import sysinput=sys.stdin.readdata=input().split()

for line in data: if line.strip(): n=int(line.strip()) result=sum_of_reciprocal_squares(n) print(result)

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

2016 ACM/ICPC亚洲区域青岛在线B题的解题思路是什么?

Σ(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]); } } }