Problem Statement
Let NN be a positive odd number.
There are NN coins, numbered 1,2,…,N1,2,…,N. For each ii (1≤i≤N1≤i≤N), when Coin ii is tossed, it comes up heads with probability pipi and tails with probability 1−pi1−pi.
Taro has tossed all the NN coins. Find the probability of having more heads than tails.
Constraints
- NN is an odd number.
- 1≤N≤29991≤N≤2999
- pipi is a real number and has two decimal places.
- 0<pi<10<pi<1
Input
Input is given from Standard Input in the following format:
NN p1p1 p2p2 …… pNpN
Output
Print the probability of having more heads than tails. The output is considered correct when the absolute error is not greater than 10−910−9.
Sample Input 1 Copy
3 0.30 0.60 0.80
Sample Output 1 Copy
0.612
The probability of each case where we have more heads than tails is as follows:
- The probability of having (Coin1,Coin2,Coin3)=(Head,Head,Head)(Coin1,Coin2,Coin3)=(Head,Head,Head) is 0.3×0.6×0.8=0.1440.3×0.6×0.8=0.144;
- The probability of having (Coin1,Coin2,Coin3)=(Tail,Head,Head)(Coin1,Coin2,Coin3)=(Tail,Head,Head) is 0.7×0.6×0.8=0.3360.7×0.6×0.8=0.336;
- The probability of having (Coin1,Coin2,Coin3)=(Head,Tail,Head)(Coin1,Coin2,Coin3)=(Head,Tail,Head) is 0.3×0.4×0.8=0.0960.3×0.4×0.8=0.096;
- The probability of having (Coin1,Coin2,Coin3)=(Head,Head,Tail)(Coin1,Coin2,Coin3)=(Head,Head,Tail) is 0.3×0.6×0.2=0.0360.3×0.6×0.2=0.036.
Thus, the probability of having more heads than tails is 0.144+0.336+0.096+0.036=0.6120.144+0.336+0.096+0.036=0.612.
Sample Input 2 Copy
1 0.50
Sample Output 2 Copy
0.5
Outputs such as 0.500
, 0.500000001
and 0.499999999
are also considered correct.
Sample Input 3 Copy
5 0.42 0.01 0.42 0.99 0.42
Sample Output 3 Copy
0.3821815872
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <iomanip> #include <deque> #include <bitset> //#include <unordered_set> //#include <unordered_map> #define ll long long #define pii pair<int, int> #define rep(i,a,b) for(int i=a;i<=b;i++) #define dec(i,a,b) for(int i=a;i>=b;i--) using namespace std; int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; const double pi = 3.14159265358979323846; const double eps = 1e-6; const int mod = 1e9 + 7; const int N = 3e3 + 5; //if(x<0 || x>=r || y<0 || y>=c) inline ll read() { ll x = 0; bool f = true; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } ll gcd(ll m, ll n) { return n == 0 ? m : gcd(n, m % n); } ll lcm(ll m, ll n) { return m * n / gcd(m, n); } bool prime(int x) { if (x < 2) return false; for (int i = 2; i * i <= x; ++i) { if (x % i == 0) return false; } return true; } ll qpow(ll m, ll k, ll mod) { ll res = 1, t = m; while (k) { if (k & 1) res = res * t % mod; t = t * t % mod; k >>= 1; } return res; } double dp[N][N]; double a[N],ans; int main() { ll n; cin >> n; rep(i, 1, n) cin >> a[i]; dp[0][0] = 1; rep(i, 1, n) { rep(j, 0, i) { int x = j, y = i - j; if (x) dp[x][y] += dp[x - 1][y] * a[i]; if (y) dp[x][y] += dp[x][y - 1] * (1 - a[i]); } } int l = n, r = 0; while (l > r) { ans += dp[l][r]; l--, r++; } printf("%.10f ", ans); return 0; }