CodeForces 16E

状态压缩 动态规划

DP[111.....1]=1表示所有鱼都在的几率为1

0代表已经挂了的,1代表没挂;

#include "stdio.h"
#define max 1<<18
double a[18][18],dp[max];
int bitcount(unsigned x)
{
    int b;
    for (b = 0; x != 0; x &= (x-1))
        b++;
    return b;
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++)
            scanf("%lf",&a[i][j]);
    }
    //printf("%lf\n",a[n-1][n-1]);
    int nn=(1<<n)-1;
    dp[nn]=1;
    for(int i=nn;i>0;i--){
        int bit=bitcount(i);
        if(bit==1)continue;
        double p=2*dp[i]/bit/(bit-1);
        for(int j=0;j<n;j++){
            if((1<<j)&i){
                for(int k=0;k<n;k++)
                    if(i&(1<<k)){
                        dp[i^(1<<k)] += p*a[j][k];
                    }
            }
        }
    }
    for(int i=0;i<n;i++){
        printf("%.6f ",dp[(1<<i)]);
    }
    return 0;
}

CodeForces 16E,布布扣,bubuko.com

时间: 2024-10-06 11:24:08

CodeForces 16E的相关文章

Problem F CodeForces 16E

Description 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 

Codeforces 16E Fish

题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<queue> #include<string> #include<stdlib.h> #include<a

Codeforces 16E Fish 概率DP

这一场好水啊..这题算是比较简单的概率DP了吧,外加一点状压. dp[sta] = sigma (dp[sta|(1<<i)]*val[j][i]/( (ans+1)*ans/2 ) ),(i为sta已被吃掉的,j为存活的,ans为存活的个数). 之所以要除( (ans+1)*ans/2 ),是因为在ans+1条鱼中一共有这些对,且这些对等概率. #include <algorithm> #include <iostream> #include <cstring&

CSU-ACM暑假集训基础组训练赛(2) 解题报告

Problem A Codeforces 451A •题意:给你n+m根筷子,将他们分别水平.垂直放置,形成n*m个节点.两个人轮流选择一个点,将穿过这个点的两根筷子拿走,谁可以逼迫对方率先无法继续操作,谁就获胜,问获胜的是先手还是后手. •找规律即可.n,m中较小的那个数是奇数,先手胜,否则后手胜. 1 #include <cstdio> 2 int main(){ 3 int a,b; 4 scanf("%d%d",&a,&b); 5 a = a <

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store