-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFish_Bitmask_DP.cpp
More file actions
74 lines (58 loc) · 1.42 KB
/
Fish_Bitmask_DP.cpp
File metadata and controls
74 lines (58 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Problem: https://codeforces.com/problemset/problem/16/E
Solution: https://www.youtube.com/watch?v=d7kvyp6dfz8
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define boost ios_base ::sync_with_stdio(0); cin.tie(0);
int n;
double p[20][20];
double dp[(1 << 19)];
int count_set_bits(int x){
int cnt = 0;
while(x){
x = x & (x-1);
cnt++;
}
return cnt;
}
double pMove(int prevBitMask, int j){
int k = count_set_bits(prevBitMask);
int den = (k * (k-1))/2;
double num = 0;
for(int fish = 0; fish < n; fish++){
if(prevBitMask & (1 << fish))
num += p[fish][j];
}
return num/(1.0 * den);
}
double solve(int bitMask){
int alive = count_set_bits(bitMask);
if(alive == n)
return 1.0;
if(dp[bitMask] > -0.5)
return dp[bitMask];
double ans = 0.0;
for(int fish = 0; fish < n; fish++){
if(!(bitMask & (1 << fish))){
int prevBitMask = bitMask | (1 << fish);
ans += solve(prevBitMask) * pMove(prevBitMask, fish);
}
}
return dp[bitMask] = ans;
}
int main()
{
boost;
cin >> n;
memset(dp, -1, sizeof(dp));
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cin >> p[i][j];
for(int i = 0; i < n; i++)
cout << fixed << setprecision(10) << solve(1 << i) << " ";
return 0;
}