zoukankan      html  css  js  c++  java
  • CodeForces

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

      

     
  • 相关阅读:
    1.淡入淡出效果js原生代码2.缓冲运动
    php 图片加水印插件
    php redis使用 常用方法 | Windows环境下安装Redis | Windows下php安装redis扩展(详解版)
    Windows下安装使用workman简单实例
    极简生成excel方法;excel表导入数据库
    php 生成表单 | url串禁止转义并解决中文字符乱码
    图片上传预览并保存图片 | 多图上传预览并保存图片 | 树状结构(jquery.treeview.js)
    php Rsa签名算法
    php 正则表达式 1.字符串中提取带小数点的数字 2.判断字符串中是否包含关键字 3.统计二维数组中某个字段值出现的次数
    虚拟主机部署tp项目,在.htaccess文件中隐藏index.php
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8983953.html
Copyright © 2011-2022 走看看