-
Notifications
You must be signed in to change notification settings - Fork 2
/
SOS DP
51 lines (44 loc) · 1.3 KB
/
SOS DP
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
/// Given a fixed array A of 2^N integers, we need to calculate
/// the function F(x) = Sum of all A[i] such that x&i = i, i.e., i is a subset of x.
/// It means i is the subset bitmask of the bitmask of x.
/// Suboptimal Bruteforce Method O(3^n):
// iterate over all the masks
for (int mask = 0; mask < (1<<n); mask++) {
F[mask] = A[0];
// iterate over all the subsets of the mask
for(int i = mask; i > 0; i = (i-1) & mask){
F[mask] += A[i];
}
}
/// Two DP methods O(n*2^n):
/// iterative version
for(int mask = 0; mask < (1<<N); mask++){
dp[mask][0] = A[mask]; //handle base case separately (leaf states)
for(int i = 0;i < N; i++){
if(mask & (1<<i))
dp[mask][i + 1] = dp[mask][i] + dp[mask^(1<<i)][i];
else
dp[mask][i + 1] = dp[mask][i];
}
F[mask] = dp[mask][N];
}
/// memory optimized, super easy to code.
for(int i = 0; i<(1<<N); i++)
F[i] = A[i];
for(int i = 0;i < N; ++i) {
for(int mask = 0; mask < (1<<N); ++mask){
if(mask & (1<<i))
F[mask] += F[mask^(1<<i)];
}
}
/// got ac code .
for(int j=0; j<19; ++j)
{
for(int i=0; i<N; ++i)
{
if(i&(1<<j))
{
Add(dp[l][i],dp[l][i^1<<j]) ;
}
}
}