Codeforces Round #146 (Div. 2) D. Let's Play Osu! comb数平方和的数学期望

2024-03-18 13:48

本文主要是介绍Codeforces Round #146 (Div. 2) D. Let's Play Osu! comb数平方和的数学期望,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:
D. Let's Play Osu!
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You're playing a game called Osu! Here's a simplified version of it. There aren clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence ofn characters "O" and "X".

Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22 + 32 + 22 = 17. If there are no correct clicks in a play then the score for the play equals to 0.

You know that the probability to click the i-th(1 ≤ i ≤ n) click correctly ispi. In other words, thei-th character in the play sequence haspi probability to be "O",1 - pi to be "X". You task is to calculate the expected score for your play.

Input

The first line contains an integer n (1 ≤ n ≤ 105) — the number of clicks. The second line containsn space-separated real numbers p1, p2, ..., pn(0 ≤ pi ≤ 1).

There will be at most six digits after the decimal point in the given pi.

Output

Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed10 - 6.

Sample test(s)
Input
3
0.5 0.5 0.5
Output
2.750000000000000
Input
4
0.7 0.2 0.1 0.9
Output
2.489200000000000
Input
5
1 1 1 1 1
Output
25.000000000000000
Note

For the first example. There are 8 possible outcomes. Each has a probability of 0.125.

  • "OOO"  → 32 = 9;
  • "OOX"  → 22 = 4;
  • "OXO"  → 12 + 12 = 2;
  • "OXX"  → 12 = 1;
  • "XOO"  → 22 = 4;
  • "XOX"  → 12 = 1;
  • "XXO"  → 12 = 1;
  • "XXX"  → 0.

So the expected score is

 

题意,给出每个位置O出现的概率,求连续O的个数的平方和的数学期望。

思路:(不是我想出来的,大牛教的)设ai为第i段连续O的长度,∑ai^2 = ∑[ ai+ ai*(ai-1) ] = ∑ ai*(ai-1) + ∑ai = ∑ C(ai, 2)*2 + ∑ai

这样就转化为长度大于1的连续段数*2+O的个数

例如:OOXOXOOO,它的值是4+1+9=14

转化后计算则为4*2+6=14 (4段长度大于1的连续段分别为1~2、5~6、5~7、6~7,再加上O的个数6)

dp[i]表示以i结尾的连续O的段数的期望(这里包括长度为1的,上例中,dp[1]=1,dp[2]=2,dp[7]=2,dp[8]=3),状态转移则为dp[i]=dp[i-1]*p[i]+p[i];

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=100005;
double p[maxn], dp[maxn];
int n;
int main(){//freopen("1.txt", "r", stdin);int i;while(scanf("%d", &n)!=EOF){double ans=0;for(i=1; i<=n; i++){scanf("%lf", &p[i]);ans+=p[i];}dp[0]=0;for(i=1; i<=n; i++){dp[i]=dp[i-1]*p[i]+p[i];ans+=(dp[i]-p[i])*2;}printf("%.7f\n", ans);}return 0;
}


 

 

 

这篇关于Codeforces Round #146 (Div. 2) D. Let's Play Osu! comb数平方和的数学期望的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/822539

相关文章

uva 10014 Simple calculations(数学推导)

直接按照题意来推导最后的结果就行了。 开始的时候只做到了第一个推导,第二次没有继续下去。 代码: #include<stdio.h>int main(){int T, n, i;double a, aa, sum, temp, ans;scanf("%d", &T);while(T--){scanf("%d", &n);scanf("%lf", &first);scanf

uva 10025 The ? 1 ? 2 ? ... ? n = k problem(数学)

题意是    ?  1  ?  2  ?  ...  ?  n = k 式子中给k,? 处可以填 + 也可以填 - ,问最小满足条件的n。 e.g k = 12  - 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12 with n = 7。 先给证明,令 S(n) = 1 + 2 + 3 + 4 + 5 + .... + n 暴搜n,搜出当 S(n) >=

uva 11044 Searching for Nessy(小学数学)

题意是给出一个n*m的格子,求出里面有多少个不重合的九宫格。 (rows / 3) * (columns / 3) K.o 代码: #include <stdio.h>int main(){int ncase;scanf("%d", &ncase);while (ncase--){int rows, columns;scanf("%d%d", &rows, &col

【生成模型系列(初级)】嵌入(Embedding)方程——自然语言处理的数学灵魂【通俗理解】

【通俗理解】嵌入(Embedding)方程——自然语言处理的数学灵魂 关键词提炼 #嵌入方程 #自然语言处理 #词向量 #机器学习 #神经网络 #向量空间模型 #Siri #Google翻译 #AlexNet 第一节:嵌入方程的类比与核心概念【尽可能通俗】 嵌入方程可以被看作是自然语言处理中的“翻译机”,它将文本中的单词或短语转换成计算机能够理解的数学形式,即向量。 正如翻译机将一种语言

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

数学建模笔记—— 非线性规划

数学建模笔记—— 非线性规划 非线性规划1. 模型原理1.1 非线性规划的标准型1.2 非线性规划求解的Matlab函数 2. 典型例题3. matlab代码求解3.1 例1 一个简单示例3.2 例2 选址问题1. 第一问 线性规划2. 第二问 非线性规划 非线性规划 非线性规划是一种求解目标函数或约束条件中有一个或几个非线性函数的最优化问题的方法。运筹学的一个重要分支。2