HackerRank Week of Code 31Colliding Circles
Posted abclzr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HackerRank Week of Code 31Colliding Circles相关的知识,希望对你有一定的参考价值。
https://www.hackerrank.com/contests/w31/challenges/colliding-circles/problem
设E(n)为序列长度为n时的期望值。
[
egin{aligned}
E(n-1)=&E(n)+frac1{nchoose2}sum_{0leq i<jleq n}2r_ir_j=&E(n)+frac1{nchoose2}left[left(sum r_i
ight)^2-sum r_i^2
ight]\=&E(n)+frac1{nchoose2}left[left(sum r_i
ight)^2-E(n)
ight]\end{aligned}
]
(O(n))递推。
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100003;
double a[N], sum = 0, E[N];
int n, k;
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%lf", &a[i]), sum += a[i], E[n] += a[i] * a[i];
for (int i = 1; i <= k; ++i) {
E[n - 1] = (1.0 - (2.0 / n / (n - 1))) * E[n] + sum * sum * 2 / n / (n - 1);
--n;
}
printf("%.10lf
", E[n] * acos(-1));
return 0;
}
以上是关于HackerRank Week of Code 31Colliding Circles的主要内容,如果未能解决你的问题,请参考以下文章
hackerrank Week of Code 34 Same Occurrence
hackerrank [Week of Code 33] Bonnie and Clyde