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;
}

  

 
posted @ 2018-05-03 09:54  蒟蒻JHY  阅读(366)  评论(0编辑  收藏  举报