Codeforces Beta Round #16 (Div. 2 Only), problem: (E) Fish 概率DP

#include<cstdio>
#include<cstring>
const int LMT=(1<<18)+10;
double dp[LMT],a[19][19],__one[LMT];
void get_one(void)
{
	int i,x;
	for(i=1;i<LMT;i++)
	{
		x=i;
		while(x)
		{
			if(x&1)__one[i]++;
			x>>=1;
		}
	}
}
int main(void)
{
	int n,lim,i,ii,jj;
	get_one();
	while(~scanf("%d",&n))
	{
		memset(dp,0,sizeof(dp));
	     lim=(1<<n)-1;
	     dp[lim]=1.0;
		 for(ii=0;ii<n;ii++)
			 for(jj=0;jj<n;jj++)
				 scanf("%lf",&a[ii][jj]);
		 for(i=lim-1;i>0;i--)
			 if(__one[i]>0)
			 {
				 for(jj=0;jj<n;++jj)
					 if(!(i&(1<<jj)))
						 for(ii=0;ii<n;++ii)
							 if(i&(1<<ii))
								 dp[i]+=a[ii][jj]*dp[i|(1<<jj)];
								 dp[i]/=(__one[i]+1)*__one[i]*0.5;
			 }
			 for(i=0;i<n;i++)
				 printf("%.6lf ",dp[1<<i]);
			 printf("\n");
	}
	return 0;
}

你可能感兴趣的:(Codeforces Beta Round #16 (Div. 2 Only), problem: (E) Fish 概率DP)