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

  

 

CodeForces - 16E Fish的更多相关文章

  1. 「算法笔记」状压 DP

    一.关于状压 dp 为了规避不确定性,我们将需要枚举的东西放入状态.当不确定性太多的时候,我们就需要将它们压进较少的维数内. 常见的状态: 天生二进制(开关.选与不选.是否出现--) 爆搜出状态,给它 ...

  2. F题

    Problem F Codeforces 16E 这道题是一道数位Dp将鱼的死活列为0两种状态然后找DP关系 •题意:有n(n<=18)条鱼,接下来的n-1天,每天会有一对鱼(a,b)相遇,每天 ...

  3. Codeforces Round #180 (Div. 2) D. Fish Weight 贪心

    D. Fish Weight 题目连接: http://www.codeforces.com/contest/298/problem/D Description It is known that th ...

  4. Codeforces Beta Round #16 E. Fish (状压dp)(概率dp)

    Codeforces Beta Round #16 (Div. 2 Only) E. Fish 题目链接:## 点击打开链接 题意: 有 \(n\) 条鱼,每两条鱼相遇都会有其中一只吃掉对方,现在给你 ...

  5. codeforces Educational Codeforces Round 16-E(DP)

    题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...

  6. Codeforces 247D Mike and Fish

    Mike and Fish 我们可以把这个模型转换一下就变成有两类点,一类是X轴, 一类是Y轴, 每个点相当于对应的点之间建一条边, 如果这条边变红两点同时+1, 变蓝两点同时-1. 我们能发现这个图 ...

  7. CodeForces - 547D: Mike and Fish (转化为欧拉回路)(优化dfs稠密图)(定向问题)

    As everyone knows, bears love fish. But Mike is a strange bear; He hates fish! The even more strange ...

  8. CodeForces 522C Chicken or Fish?

    Chicken or Fish? 题意比较难理解. 需要注意的是 就算某个人抱怨了 但是的t[i]也是他最后选择的结果. 题解: 首先考虑没有r[i] = 1的情况. 直接记录t[i]=0的数目,最后 ...

  9. Codeforces 547D - Mike and Fish(欧拉回路)

    Codeforces 题目传送门 & 洛谷题目传送门 首先考虑将题目中的条件转化为图论的语言.看到"行""列",我们很自然地想到二分图中行.列转点,点转 ...

随机推荐

  1. 03015_JSTL技术

    1.JSTL概述 (1)JSP(JSP Standard Tap Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能.jstl出现的目的同el一样也是要替代js ...

  2. loj2292 「THUSC 2016」成绩单

    ref 我是傻逼,我啥也不会,这是我抄的. #include <iostream> #include <cstring> #include <cstdio> usi ...

  3. Python-S9——Day110-Git继续

    1 当日内容概要 2 内容回顾 3 Git版本控制之多人协同开发 4 Git版本控制之fork 5 版本控制之其他 6 Redis之字典基本操作 7 Django中操作Redis 8 Django缓存 ...

  4. Leetcode 643.子数组最大平均数I

    子数组最大平均数I 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. 示例 1: 输入: [1,12,-5,-6,50,3], k = 4 输出: 12.75 解释: ...

  5. [oldboy-django][6其他]备份数据库和导入数据库

    # 备份数据库 - 简单备份 mysqldump -uroot -pec494904 ecmangent-mobile > /tmp/backfile.sql 表结构+数据 - --opt my ...

  6. JavaWeb笔记(二)Servlet

    Tomcat目录简介 bin--可执行文件 conf--配置文件 lib--依赖jar包 logs--日志文件 temp--临时文件 webapps--默认项目部署路径 work--存放运行时的数据 ...

  7. HDU——2089 不要62

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. MySql数据类型和Java数据类型对应一览

    类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述             VARCHAR L+N VARCHAR java.lang.String 12   CHAR N ...

  9. leetcode 27 水

    class Solution { public: int removeElement(vector<int>& nums, int val) { int length=nums.s ...

  10. jspspy database help

    .