NC214899. SkyGarden
描述
输入描述
The only line contains two integers .
输出描述
Output one number -- the sum of the distances between every pair of points in .
Your answer is considered correct if its absolute or relative error does not exceed .
示例1
输入:
1 2
输出:
14.2831853072
说明:
示例2
输入:
2 3
输出:
175.4159265359
Python3(3.9) 解法, 执行用时: 21ms, 内存消耗: 2868K, 提交时间: 2021-01-31 02:19:35
import math n, m = map(int, input().split()) k1 = (1 + n) * n * n / 2 - n * (n + 1) * (2 * n + 1) / 6 # 这里是一个平方和 k2 = (1 + n) * n * (n + 0.5) / 2 - n * (n + 1) * (2 * n + 1) / 6 ans = (1 + n) * n * m if m != 1 else 0 ans += 4 * m * m * k1 t = math.floor(2 * m / math.pi) ans += (2 * math.pi * t * (t + 1) + (4 * m * (m - t) - 2 * m) * 2) * k2 print(ans)
C++(clang++11) 解法, 执行用时: 8ms, 内存消耗: 380K, 提交时间: 2020-12-13 13:00:11
#include<bits/stdc++.h> using namespace std; double pi=acos(-1); int n,m;double ans; int main(){ scanf("%d%d",&n,&m); if(m>1)ans=m*n*(n+1); for(int i=1;i<=n;++i){ double tmp=0; for(int j=1;j<=2*m;++j){ tmp+=min(min(j,2*m-j)*pi/m,2.0)*i; } ans+=2*m*((n-i)*(n-i+1)*m+(n-i)*tmp+tmp/2); } printf("%.10f\n",ans); return 0; }