首页 > 其他 > 详细

【HackerRank Week of Code 31】Colliding Circles

时间:2018-06-28 20:42:20      阅读:256      评论:0      收藏:0      [点我收藏+]

https://www.hackerrank.com/contests/w31/challenges/colliding-circles/problem
设E(n)为序列长度为n时的期望值。
\[ \begin{aligned} E(n-1)=&E(n)+\frac1{n\choose2}\sum_{0\leq i<j\leq n}2r_ir_j\=&E(n)+\frac1{n\choose2}\left[\left(\sum r_i\right)^2-\sum r_i^2\right]\\=&E(n)+\frac1{n\choose2}\left[\left(\sum r_i\right)^2-E(n)\right]\\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\n", E[n] * acos(-1));
    
    return 0;
}

【HackerRank Week of Code 31】Colliding Circles

原文:https://www.cnblogs.com/abclzr/p/9240529.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!