题意:给定n,p;表示n个点中任意两点连边的概率为p,求生成的图是个连通块的概率。n<=20
解法:反向思考,ans[i]为i个节点为连通块的概率,求ans[n]时候,求不为一个连通块的概率,然后用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[n]);
}
return 0;
}