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

      

     
  • 相关阅读:
    洛谷P1057传球游戏(逆向递推递归+记忆化)
    洛谷P1433吃奶酪(正向暴力递归,回溯更新)
    洛谷P1434滑雪(逆向图的遍历搜索递归+记忆化,最长路问题)
    洛谷P1192台阶问题(逆向递推递归dfs+记忆化)
    洛谷p1025数的划分(正向暴力递归,数学排列与组合问题)
    洛谷P1141 01迷宫(图的遍历搜素递归dfs或bfs,连痛块回溯更新问题,记忆化或者并查集根结点)
    Git 版本更新--Windows
    plop-templates自动新建项目文件
    前端-随机生成测试数据-mockjs
    cookie、seseionStorage、localStorage的区别
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8983953.html
Copyright © 2011-2022 走看看