CodeForces - 16E Fish

Discription

n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability aij, and the second will eat up the first with the probability aji = 1 - aij. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake.

Input

The first line contains integer n (1 ≤ n ≤ 18) — the amount of fish in the lake. Then there follow n lines with n real numbers each — matrix aaij (0 ≤ aij ≤ 1) — the probability that fish with index i eats up fish with index j. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: aij = 1 - aji. All real numbers are given with not more than 6 characters after the decimal point.

Output

Output n space-separated real numbers accurate to not less than 6 decimal places. Number with index i should be equal to the probability that fish with index i will survive to be the last in the lake.

Examples

Input
2
0 0.5
0.5 0
Output
0.500000 0.500000 
Input
5
0 1 1 1 1
0 0 0.5 0.5 0.5
0 0.5 0 0.5 0.5
0 0.5 0.5 0 0.5
0 0.5 0.5 0.5 0
Output
1.000000 0.000000 0.000000 0.000000 0.000000 


不难想到设f[S]为到达状态S下的概率,模拟鱼相遇的过程就可以做到 O(2^N * N^2) 的复杂度,足够通过本题。
但是这还不是最优的方法,因为一个状态 S 的后继只有 O(N)种,并且在这个题中不同的到达后继的方式是很好合并的,根据每一对鱼之间相遇的概率的独立性,我们可以O(2^N * N)预处理出每个鱼i在集合S中被吃掉的概率 f[S][i],并通过这个直接从S -> S^(2^i) ,总的复杂度就是 O(2^N * N)。

(假装我是CF上的rank1 23333)


#include<bits/stdc++.h>
#define ll long long
using namespace std;
#define D double
const int maxn=400005;
D a[23][23],f[maxn],BE[maxn][23];
int ci[35],n,m,T,BT[maxn],dy[maxn];

inline void prework(){
	for(int i=1,now,lef;i<ci[n];i++){
		now=i&-i,lef=i^now,now=dy[now];
		for(int j=0;j<n;j++) BE[i][j]=BE[lef][j]+a[now][j];
	}
}

inline void solve(){
	f[ci[n]-1]=1;
	for(int i=ci[n]-1;i;i--) if(BT[i]>1){
		T=BT[i]*(BT[i]-1)>>1;
		for(int j=0;j<n;j++) if(ci[j]&i) f[i^ci[j]]+=f[i]*BE[i][j]/(D)T;
	}
}

int main(){
	ci[0]=1; for(int i=1;i<=20;i++) ci[i]=ci[i-1]<<1;
	
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	    for(int j=0;j<n;j++) scanf("%lf",&a[i][j]);
	    
	BT[0]=0; for(int i=1;i<ci[n];i++) BT[i]=BT[i^(i&-i)]+1;
	for(int i=0;i<n;i++) dy[ci[i]]=i;
	
	prework(),solve();
	
	for(int i=0;i<n;i++) printf("%.10lf ",f[ci[i]]);
	return 0;
}

  

 

转载于:https://www.cnblogs.com/JYYHH/p/8983953.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值