poj3557(概率dp)
2014-11-26 22:32
162 查看
题意:给定n,p;表示n个点中任意两点连边的概率为p,求生成的图是个连通块的概率。n<=20
解法:反向思考,ans[i]为i个节点为连通块的概率,求ans
时候,求不为一个连通块的概率,然后用1减。求非连通时,枚举与1号节点为一个连通块的点的个数即可。
公式:ans[i]=1.0- sigma C[i-1][j-1]*ans[j]*pow(1.0-p,j*(i-j)) --- j from 1 to i-1;
代码:
解法:反向思考,ans[i]为i个节点为连通块的概率,求ans
时候,求不为一个连通块的概率,然后用1减。求非连通时,枚举与1号节点为一个连通块的点的个数即可。
公式:ans[i]=1.0- sigma C[i-1][j-1]*ans[j]*pow(1.0-p,j*(i-j)) --- j from 1 to i-1;
代码:
/****************************************************** * @author:xiefubao *******************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <vector> #include <algorithm> #include <cmath> #include <map> #include <set> #include <stack> #include <string.h> //freopen ("in.txt" , "r" , stdin); using namespace std; #define eps 1e-8 #define zero(_) (abs(_)<=eps) const double pi=acos(-1.0); typedef long long LL; const int Max=22; const LL INF=0x3FFFFFFF; double ans[23]; int n; double p; LL C[30][30]; void init() { for(int i=0; i<Max; i++) for(int j=0; j<=i; j++) C[i][j]= j==0? 1 : C[i-1][j-1]+C[i-1][j]; } int main() { init(); while(scanf("%d%lf",&n,&p)==2) { memset(ans,0,sizeof ans); ans[1]=1.0; for(int i=2;i<=n;i++) { double help=0; for(int j=1;j<i;j++) help+=C[i-1][j-1]*ans[j]*pow(1.0-p,double(j*(i-j))); ans[i]=1.0-help; } printf("%.3lf\n",ans ); } return 0; }
相关文章推荐
- poj3557 Map Generator(概率dp)及其扩展
- poj3557 一个很巧妙的概率dp
- POJ 3071 Football (概率DP)
- zujt ZJUT 1423 地下迷宫 高斯消元法 概率DP
- LightOJ1274---Beating the Dataset (概率dp)
- hdu4405--Aeroplane chess(概率dp第七弹:飞行棋游戏--2012年网络赛)
- HDU 4336 Card Collector (概率-期望DP)【模板】
- 例10-12 *uva1637(概率dp)
- POJ 3744 Scout YYF I(概率Dp)
- 概率dp 求期望
- [HEOI2017]分手是祝愿 期望概率dp 差分
- POJ 2151 Check the difficulty of problems 概率DP
- bzoj4318 OSU! 概率DP
- HDU 4800 Josephina and RPG【概率DP】
- POJ 2096 Collecting Bugs 概率DP
- POJ 2151 Check the difficulty of problems (概率DP)
- hdu4870(高斯消元解概率DP)
- HDU-3853 LOOPS(概率DP求期望)
- BZOJ 1415: [Noi2005]聪聪和可可 [DP 概率]
- [LOJ#2290][THUWC 2017][概率][状压][DP][陈老师神题]随机二分图