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

      

     
  • 相关阅读:
    移除jboss响应中的中间件信息
    Cypress web自动化1-windows环境npm安装Cypress
    pytest文档39-参数化(parametrize)结合allure.title()生成不同标题报告
    pytest文档38-allure.step()添加测试用例步骤
    python笔记45-经典面试题:判断字符串括号是否闭合{}[]()
    Linux学习28-linux一行命令杀掉指定名称进程(killall 、kill 、pkill)
    pytest文档37-自定义用例顺序(pytest-ordering)
    pytest文档36-断言失败后还能继续执行pytest-assume
    pytest文档35-Hooks函数之统计测试结果(pytest_terminal_summary)
    pytest文档34-Hooks函数改变用例执行顺序(pytest_collection_modifyitems)
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8983953.html
Copyright © 2011-2022 走看看