【[Offer收割]编程练习赛 14 B】投掷硬币
【题目链接】:http://hihocoder.com/problemset/problem/1506
【题意】
中文题
【题解】
这种题是概率DP….
设f[i][j]表示i个硬币里面有j个正面朝上的概率;
则第i个有两种可能;
证明朝上或反面朝上;
f[i][j]=f[i-1][j]*(1-p[i])+f[i-1][j-1]*p[i];
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1100;
int n,m;
double p[N],f[N][N];
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false);
cin >> n >> m;
rep1(i,1,n)
cin >> p[i];
double allzheng=1,allfan=1;
rep1(i,1,n)
{
allzheng*=p[i];
allfan*=(1-p[i]);
f[i][i] = allzheng,f[i][0]=allfan;
}
rep1(i,2,n)
rep1(j,1,min(i,m))
f[i][j] = f[i-1][j]*(1-p[i])+f[i-1][j-1]*p[i];
cout << fixed << setprecision(9) << f[n][m] << endl;
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}