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 a. aij (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
2
0 0.5
0.5 0
0.500000 0.500000
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
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的更多相关文章
- 「算法笔记」状压 DP
一.关于状压 dp 为了规避不确定性,我们将需要枚举的东西放入状态.当不确定性太多的时候,我们就需要将它们压进较少的维数内. 常见的状态: 天生二进制(开关.选与不选.是否出现--) 爆搜出状态,给它 ...
- F题
Problem F Codeforces 16E 这道题是一道数位Dp将鱼的死活列为0两种状态然后找DP关系 •题意:有n(n<=18)条鱼,接下来的n-1天,每天会有一对鱼(a,b)相遇,每天 ...
- 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 ...
- Codeforces Beta Round #16 E. Fish (状压dp)(概率dp)
Codeforces Beta Round #16 (Div. 2 Only) E. Fish 题目链接:## 点击打开链接 题意: 有 \(n\) 条鱼,每两条鱼相遇都会有其中一只吃掉对方,现在给你 ...
- codeforces Educational Codeforces Round 16-E(DP)
题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...
- Codeforces 247D Mike and Fish
Mike and Fish 我们可以把这个模型转换一下就变成有两类点,一类是X轴, 一类是Y轴, 每个点相当于对应的点之间建一条边, 如果这条边变红两点同时+1, 变蓝两点同时-1. 我们能发现这个图 ...
- 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 ...
- CodeForces 522C Chicken or Fish?
Chicken or Fish? 题意比较难理解. 需要注意的是 就算某个人抱怨了 但是的t[i]也是他最后选择的结果. 题解: 首先考虑没有r[i] = 1的情况. 直接记录t[i]=0的数目,最后 ...
- Codeforces 547D - Mike and Fish(欧拉回路)
Codeforces 题目传送门 & 洛谷题目传送门 首先考虑将题目中的条件转化为图论的语言.看到"行""列",我们很自然地想到二分图中行.列转点,点转 ...
随机推荐
- python使用@property @x.setter @x.deleter
@property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的. 1>只有@property表示只读. 2>同时 ...
- 在Foxmail中添加阿里云企业邮箱账号
1.安装完成Foxmail之后,新建账号 输入阿里云邮箱地址和密码,点击创建 接受服务器类型你可以选择POP3或者IMAP,在这里我选择的是POP3 点击创建,大功告成. 为什么要写这篇文章呢? 因为 ...
- leetcode 【 Copy List with Random Pointer 】 python 实现
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- SSH的基本操作
一. 登陆SSH远程控制服务器 我们以192.168.100.42服务器为例. SSH乱码: LANG="zh_CN.GB18030" export LANG=zh_CN.gb23 ...
- jmeter非GUI模式如何压测并生成测试报告
在启动Jmeter时,我们会看到这样一句提示: 不要使用GUI模式(界面模式)进行负载测试,GUI模式只能用于创建测试和调试.进行负载测试时,需要时用非GUI模式. 那么为什么进行负载测试时一定要用非 ...
- 聊聊、Java 命令 第三篇
这篇随笔主要写启动 jar 时,如果需要依赖其他的 jar 包该怎么处理,我会以 rabbitMQ 客服端启动为例. package com.rockcode.www.rabbitmq; import ...
- sqlserver2008透明书库加密
/*Title:TDE加密Author:浪客Environment:Windows Server 2008 Enterprise + SQL Server 2008 EnterpriseDescrip ...
- Box 类
public class Box extends JComponent implements Accessible使用 BoxLayout 对象作为其布局管理器的一个轻量级容器.Box 提供几个对使用 ...
- HTML表单与文件
<!DOCTYPE html> <html> <head lang="en"> <title></title> < ...
- 游戏(game)
游戏(game) 题目描述 这个游戏是这样的,你有一个初始序列S ,你每次可以选择一段任意长度的连续区间,把他们+1 再膜k,给定目标序列,你需要尝试用尽量少的操作次数将初始序列变为目标序列.作为一名 ...