SlideShare a Scribd company logo
Bitwise DP
(Acka)
Bitwise Operator
BitwiseDP 2
Bitwise Operator
• A | B => A or B
• A & B => A and B
• A ^ B => A xor B
• A = 1001, B = 0101
• A | B = 1101, A & B = 0001, A ^ B = 1100
• ~A => ones complement of A =>
• A << x => left shift A as x
• A >> x => right shift A as x
• A = 00110101
• ~A = 11001010, A << 2 = 11010100, A >> 2 = 00001101
BitwiseDP 3
• S M .
• add x: S x . x
• remove x: S x . x
• check x: S x 1, 0
• toggle x: S x x , x
• all: S {1, 2, ..., 20} .
• empty: S .
• 1 ≤ x ≤	20
• 1 ≤ M ≤ 3,000,000
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
4
• bool exist [21]
• exist[i]: i true, false
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
5
• .
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
6
• .
• stat = 50 = 0….000000000000000110010(2)
• stat: {2, 5, 6}
• i-1 : i /
• .
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
7
• add x
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
8
• add 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
9
…01010001(2)
…01011001(2)
• add 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
10
…01010001(2)
+ …00001000(2)
…01011001(2)
• add 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
11
…01011001(2)
+ …00001000(2)
• add 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
12
…01011001(2)
+ …00001000(2)
…01100001(2)
• add 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
13
…0101?001(2)
or …00001000(2)
• add 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
14
…0101?001(2)
or …00001000(2)
…0101?001(2)
• add 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
15
…0101?001(2)
or …00001000(2)
…0101?001(2)
• add 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
16
…0101?001(2)
or …00001000(2)
…0101?001(2)
1|0 = 1
0|0 = 0
0 or
• add 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
17
…0101?001(2)
or …00001000(2)
…0101?001(2)
• add 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
18
…0101?001(2)
or …00001000(2)
…01011001(2)
1|1 = 1
0|1 = 1
1 or 1
• add x = stat | (1 << (x – 1))
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
19
…0101?001(2)
or …00001000(2)
…01011001(2)
• remove 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
20
…0101?001(2)
• remove 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
21
…0101?001(2)
& …11110111(2)
…0101?001(2)
• remove 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
22
…0101?001(2)
& …11110111(2)
…0101?001(2)
• remove 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
23
…0101?001(2)
& …11110111(2)
…0101?001(2)
1&1 = 1
0&1 = 0
1 and
• remove 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
24
…0101?001(2)
& …11110111(2)
…0101?001(2)
• remove 4
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
25
…0101?001(2)
& …11110111(2)
…01010001(2)
0&0 = 0
1&0 = 0
0 and 0
• remove x = stat & ~(1 << (x – 1))
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
26
…0101?001(2)
& …11110111(2)
…01010001(2)
• bit[x] = 2x-1
• add: stat |= bit[x]
• remove: stat &= ~bit[x]
• check: stat & bit[x]
• toggle: stat ^= bit[x]
• all: stat = bit[21] - 1
• empty: stat = 0
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
27
• C/C++:
https://gist.github.com/Acka1357/61f2990267345c21319bbf4fb79fe5a2
• Java:
https://gist.github.com/Acka1357/2c243480be1edca69968a7ad300279cf
BitwiseDP
Problem: https://www.acmicpc.net/problem/11723
28
•
• N 0 9
• (0 .)
• 1 ≤ N ≤	100
BitwiseDP
Problem: https://www.acmicpc.net/problem/1562
29
•
• D[i][j]: i j
• D[i][j] = D[i – 1][j – 1] + D[i – 1][j + 1]
BitwiseDP
Problem: https://www.acmicpc.net/problem/1562
30
•
• D[i][j]: i j
• D[i][j] = D[i – 1][j – 1] + D[i – 1][j + 1]
• + 0 ~ 9
BitwiseDP
Problem: https://www.acmicpc.net/problem/1562
31
• D[i][j][use0][use1][use2]…[use9]:
i j
use0, use1, use2 .. use9
(use0, use1 … use9 = 0 or 1)
BitwiseDP
Problem: https://www.acmicpc.net/problem/1562
32
• D[i][j][use0][use1][use2]…[use9]:
i j
use0, use1, use2 .. use9
(use0, use1 … use9 = 0 or 1)
• [use0][use1][use2]…[use9]: 2 * 2 * 2 * … * 2
=> 210
=> 0000000000(2) ~ 1111111111(2)
BitwiseDP
Problem: https://www.acmicpc.net/problem/1562
33
• D[i][j][used]: i j
used
• used: 0000000000(2) ~ 1111111111(2) => 210
• used k(0~9) bit: k
BitwiseDP
Problem: https://www.acmicpc.net/problem/1562
34
• D[i][j][used]: i j
used
• used: 0000000000(2) ~ 1111111111(2) => 210
• used k(0~9) bit: k
BitwiseDP
Problem: https://www.acmicpc.net/problem/1562
used: 101 = 0001100101(2)
5 0
8
35
• D[i][j][used]: i j
used
• used .
• D[i][j - 1][used] -> D[i + 1][j][used | 2j]
• D[i][j + 1][used] -> D[i + 1][j][used | 2j]
BitwiseDP
Problem: https://www.acmicpc.net/problem/1562
36
• : ∑ 𝐷 𝑁 𝑖 [1023]-
./0
• N (used == 1111111111(2))
BitwiseDP
Problem: https://www.acmicpc.net/problem/1562
37
• : O(N * 10 * 210)
• * *
• 1 ≤ N ≤	100
BitwiseDP
Problem: https://www.acmicpc.net/problem/1562
38
• C/C++:
https://gist.github.com/Acka1357/ba689997d73246d77a8ccc33f876073f
• Java:
https://gist.github.com/Acka1357/1d65d7af446ce436b48b80a36386e108
BitwiseDP
Problem: https://www.acmicpc.net/problem/1562
39
• 1 N .
• N
• . ( )
•
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 40
•
• *
* …
• N * (N – 1) * (N – 2) * … * 1 => O(N!)
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 41
•
• *
* …
• N * (N – 1) * (N – 2) * … * 1 => O(N!)
• 2 ≤ N ≤ 16
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 42
•
• *
* …
• N * (N – 1) * (N – 2) * … * 1 => O(N!)
• 2 ≤ N ≤ 16
• 16! => 20,922,789,888,000
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 43
• 1 -> 2 -> 3 -> 4 -> ?
• 1 -> 3 -> 2 -> 4 -> ?
• : 1, 2, 3, 4 4
• : 4
1
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 44
• 1 -> 2 -> 3 -> 4 -> ?
• 1 -> 3 -> 2 -> 4 -> ?
•
• .
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 45
•
•
• /
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 46
•
•
• /
• (1 ) * (2 ) * (3
) * … * (N )
• 2N
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 47
• d[cur][visited]: i
visited
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 48
• d[cur][visited]: i
visited
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP
visited: 101 = 01100101(2)
49
• d[cur][visited]: i
visited
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP
7 1
8
50
visited: 101 = 01100101(2)
• d[cur][visited]: i
visited
• d[cur][visited] + cost[cur][next] -> d[next][visited + 2next]
• 2next : next
• next
• visited & 2next == 0
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 51
• d[cur][visited]
visited cur
d[cur][visited] .
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP
10011010
10011010
10011010
10011010
00011010
10001010
10010010
10011000
min
52
cur visited
2,4,5
2,4,8
2,5,8
4,5,8
cur visited
8
5
4
2
• d[cur][visited]
visited cur
d[cur][visited] .
• visited .
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP
10011010
10011010
10011010
10011010
00011010
10001010
10010010
10011000
min
53
cur visited
2,4,5
2,4,8
2,5,8
4,5,8
cur visited
8
5
4
2
• d[cur][visited]: i
visited
• d[cur][visited] = minpast ∈	visited (d[past][visited – 2cur]
+ cost[past][cur])
• 2cur : cur
• past cur , visited
• visited & 2past == 2past
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 54
•
• ( ) .
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 55
•
• ( ) .
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 56
P1 P2 P3 P4 P5 P6
•
• ( ) .
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 57
P1
P2
P3
P4
P5
P6
• : O(N * N * 2N)
• : O(N * 2N)
• / : O(N)
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 58
• C/C++:
https://gist.github.com/Acka1357/be750aba1f1950f50c93dce7d038752c
https://gist.github.com/Acka1357/2815446354910d6b4ac84164a776b6ee
• Java:
https://gist.github.com/Acka1357/25a34b9ea7e49ad148fe4cfe2fe6143b
Problem: https://www.acmicpc.net/problem/2098
BitwiseDP 59
• N X M
• 2 X 1, 1 X 2
•
• 1 ≤ N, M ≤ 14
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 60
• , .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 61
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
… …
… …
• , 0 .
• (0, 1) (0, 10)
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 62
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
… …
… …
• (0, 1)
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 63
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
… …
… …
• 0 1 .
• 0 , M-1
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 64
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
… …
… …
• status = 20 + 21
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 65
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
… …
… …
0으로부터 한 줄의 상태:
0000000011(2)
• (0, 10)
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 66
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
… …
… …
• status = 20 ( )
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 67
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
… …
… …
0으로부터 한 줄의 상태:
0000000001(2)
• 1 , 0 ,
• 0 .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 68
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
… …
… …
0으로부터 한 줄의 상태:
0000000001(2)
• 0 10 ,
• 10 .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 69
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
… …
… …
1로부터 ‘한 줄’의 상태:
1000000000(2)
• i-1 0 -> i
• i (i + 1) (i + M) .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 70
0 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
… …
• i , (i – 1)
• (i + M) .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 71
0 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
… …
• i i M
(i – 1) 1, (i + M) 0
.
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 72
0 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
… …
• 12
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 73
0 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
… …
12부터 M칸의 상태:
1111001001(2)
• 12
• 11 , 22 .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 74
0 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
… …
12부터 M칸의 상태:
1111001001(2)
• 12
• 12 , 13 .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 75
0 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
… …
12부터 M칸의 상태:
1111001001(2)
• 13 M
• 12 M .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 76
0 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
… …
12부터 M칸의 상태:
1111001001(2)
• status >> 1 (or status /= 2)
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 77
0 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
… …
13부터 M칸의 상태:
0111100100(2)
• 13
• 13 , 23 .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 78
0 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
… …
12부터 M칸의 상태:
1111001001(2)
• 13
• (13, 14) (13, 23) .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 79
0 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
… …
13부터 M칸의 상태:
0111100100(2)
• (13, 14) (13, 14) .
• 0 1 0 .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 80
0 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
… …
13부터 M칸의 상태:
0111100100(2)
• (13, 14) ,
• 14 M .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 81
0 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
… …
13부터 M칸의 상태:
0111100100(2)
• (status >> 1) + 20
• 14 M 14 0
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 82
0 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
… …
14부터 M칸의 상태:
0011110011(2)
• (13, 23) (13, 23) .
• 23 .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 83
0 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
… …
13부터 M칸의 상태:
0111100100(2)
• (13, 23) ,
• 14 M .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 84
0 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
… …
13부터 M칸의 상태:
0111100100(2)
• (status >> 1) + 2M-1
• 14 M 23 M-1 .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 85
0 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
… …
14부터 M칸의 상태:
1011110010(2)
• D[i][status]: i – 1 ,
i M status
• 0 ≤	status < 2M
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 86
• i (if (status & 1) == 1)
• d[i + 1][status >> 1] += d[i][status]
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 87
• i (if (status & 1) == 1)
• d[i + 1][status >> 1] += d[i][status]
• i
•
•
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 88
• (if (status & 3) == 0)
• d[i + 1][(status >> 1) + 1] += d[i][status]
• (i (status & 1) == 0)
• d[i + 1][(status >> 1) + 2M-1] += d[i][status]
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 89
• (if (status & 3) == 0)
• d[i + 1][(status >> 1) + 1] += d[i][status]
• (i (status & 1) == 0)
• d[i + 1][(status >> 1) + 2M-1] += d[i][status]
• , i .
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 90
• d[i + 1][(status >> 1)] += d[i][status]
• if (status & 1) == 1
• d[i + 1][(status >> 1) + 1] += d[i][status]
• if (status & 3) == 0, and (i % M) != M – 1
• d[i + 1][(status >> 1) + 2M-1] += d[i][status]
• if (status & 1) == 0
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 91
• : d[N * M][0]
• N * M (N * M – 1) ,
• N * M
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 92
• : O(N * M * 2M)
• : O(N * M)
• : O(2M)
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 93
• C/C++:
https://gist.github.com/Acka1357/473f67f678c484b19e5d5daabde7522c
• Java:
https://gist.github.com/Acka1357/3406495ff3597161c4245d9989dc3fa7
Problem: https://www.acmicpc.net/problem/1648
BitwiseDP 94
• N X M
• 2 X 1, 1 X 2
• .
• .
•
• 1 ≤ N, M ≤ 14
Problem: https://www.acmicpc.net/problem/1657
BitwiseDP 95
• ==
• , .
Problem: https://www.acmicpc.net/problem/1657
BitwiseDP 96
• ==
• , .
• , .
Problem: https://www.acmicpc.net/problem/1657
BitwiseDP 97
• D[i][status]: i – 1 ( ) ,
i M status
• 0 ≤	status < 2M
Problem: https://www.acmicpc.net/problem/1657
BitwiseDP 98
• i (if (status & 1) == 1)
• d[i + 1][status >> 1] <- d[i][status]
Problem: https://www.acmicpc.net/problem/1657
BitwiseDP 99
• i (if (status & 1) == 1)
• d[i + 1][status >> 1] <- d[i][status]
• i
• d[i + 1][status >> 1] <- d[i][status]
• d[i + 1][(status >> 1) + 1] += d[i][status] ( )
• d[i + 1][(status >> 1) + 2M-1] += d[i][status] ( )
Problem: https://www.acmicpc.net/problem/1657
BitwiseDP 100
• d[i + 1][(status >> 1)] <- d[i][status]
• always possible
• d[i + 1][(status >> 1) + 1] += d[i][status]
• if (status & 3) == 0, and (i % M) != M – 1
• d[i + 1][(status >> 1) + 2M-1] += d[i][status]
• if (status & 1) == 0
Problem: https://www.acmicpc.net/problem/1657
BitwiseDP 101
• : d[N * M][0]
• N * M (N * M – 1) ( ) ,
• N * M
Problem: https://www.acmicpc.net/problem/1657
BitwiseDP 102
• : O(N * M * 2M)
• : O(N * M)
• : O(2M)
Problem: https://www.acmicpc.net/problem/1657
BitwiseDP 103
• C/C++:
https://gist.github.com/Acka1357/99cd03e1d98611f02840ab9708052c99
• Java:
https://gist.github.com/Acka1357/6d5dbdcc1528957a2896a703f6fa796c
Problem: https://www.acmicpc.net/problem/1657
BitwiseDP 104

More Related Content

PDF
TC74VCX244FT PSpice Model (Free SPICE Model)
PDF
Artificial Neural Networks (ANNs) - XOR - Step-By-Step
PDF
TC74AC244FT PSpice Model (Free SPICE Model)
PDF
TC74AC244F PSpice Model (Free SPICE Model)
PDF
TC74AC244P PSpice Model (Free SPICE Model)
PDF
TC74AC244FW PSpice Model (Free SPICE Model)
PDF
Gilat_ch01.pdf
PDF
The Ring programming language version 1.3 book - Part 21 of 88
TC74VCX244FT PSpice Model (Free SPICE Model)
Artificial Neural Networks (ANNs) - XOR - Step-By-Step
TC74AC244FT PSpice Model (Free SPICE Model)
TC74AC244F PSpice Model (Free SPICE Model)
TC74AC244P PSpice Model (Free SPICE Model)
TC74AC244FW PSpice Model (Free SPICE Model)
Gilat_ch01.pdf
The Ring programming language version 1.3 book - Part 21 of 88

What's hot (7)

PDF
Gilat_ch03.pdf
PDF
The Ring programming language version 1.7 book - Part 33 of 196
PDF
Gilat_ch02.pdf
PDF
Prelude to halide_public
PDF
Playing with camera preview buffers on BlackBerry 10
DOCX
2019-ME-37(NMC).docx
PDF
apidays LIVE Australia 2020 - Strangling the monolith with a reactive GraphQL...
Gilat_ch03.pdf
The Ring programming language version 1.7 book - Part 33 of 196
Gilat_ch02.pdf
Prelude to halide_public
Playing with camera preview buffers on BlackBerry 10
2019-ME-37(NMC).docx
apidays LIVE Australia 2020 - Strangling the monolith with a reactive GraphQL...
Ad

Similar to 알고리즘 연합캠프 세미나 1-B (Bitwise DP) (20)

PDF
Baekjoon Online Judge 3015번 풀이
PDF
Baekjoon Online Judge 10986번 풀이
PDF
Python Homework Sample
PPTX
Algorithm Homework Help
PPTX
Code conversion
PPTX
Bit-Manipulation for competitive programming
PPTX
CS304PC:Computer Organization and Architecture Session 21 Division Algorithm....
PDF
Course project solutions 2019
PDF
Bit-wise Operation Slides
DOC
Network lab manual
PDF
Dynamic programming
PPT
Chap11alg
PPT
Chap11alg
PDF
Assignment_URI_Code_Solution_Roll_2010052 (1).pdf
PPT
Exercises+Lab.ppt - minimum node cover - exam
PDF
Bitwise
PPTX
Algorithm Assignment Help
PPTX
CS304PC:Computer Organization and Architecture session 18 floating point rese...
DOC
Spoj Hints
PDF
2014 ACM-ICPC Daejeon 인터넷 예선 해설
Baekjoon Online Judge 3015번 풀이
Baekjoon Online Judge 10986번 풀이
Python Homework Sample
Algorithm Homework Help
Code conversion
Bit-Manipulation for competitive programming
CS304PC:Computer Organization and Architecture Session 21 Division Algorithm....
Course project solutions 2019
Bit-wise Operation Slides
Network lab manual
Dynamic programming
Chap11alg
Chap11alg
Assignment_URI_Code_Solution_Roll_2010052 (1).pdf
Exercises+Lab.ppt - minimum node cover - exam
Bitwise
Algorithm Assignment Help
CS304PC:Computer Organization and Architecture session 18 floating point rese...
Spoj Hints
2014 ACM-ICPC Daejeon 인터넷 예선 해설
Ad

More from HYUNJEONG KIM (9)

PDF
알고리즘 연합캠프 세미나 3-B (LCA)
PDF
알고리즘 연합캠프 세미나 3-C (C++11 and ETC)
PDF
알고리즘 연합캠프 세미나 2-C (Segment Tree)
PDF
알고리즘 연합캠프 세미나 1-A (Multi Dimension Segment/Fenwick Tree)
PDF
알고리즘 연합캠프 세미나 1-C (알고리즘 설계와 모델링 및 수학)
PDF
알고리즘 연합캠프 세미나 1-D (Knapsack, Tree DP)
PDF
shake! 2017 본선문제 풀이
PDF
shake! 2017 예선 문제 풀이
PDF
shake! 2016 예선 문제 풀이
알고리즘 연합캠프 세미나 3-B (LCA)
알고리즘 연합캠프 세미나 3-C (C++11 and ETC)
알고리즘 연합캠프 세미나 2-C (Segment Tree)
알고리즘 연합캠프 세미나 1-A (Multi Dimension Segment/Fenwick Tree)
알고리즘 연합캠프 세미나 1-C (알고리즘 설계와 모델링 및 수학)
알고리즘 연합캠프 세미나 1-D (Knapsack, Tree DP)
shake! 2017 본선문제 풀이
shake! 2017 예선 문제 풀이
shake! 2016 예선 문제 풀이

Recently uploaded (20)

PPTX
OOP with Java - Java Introduction (Basics)
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
web development for engineering and engineering
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
additive manufacturing of ss316l using mig welding
PPTX
UNIT 4 Total Quality Management .pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Geodesy 1.pptx...............................................
PDF
PPT on Performance Review to get promotions
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
OOP with Java - Java Introduction (Basics)
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
web development for engineering and engineering
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
additive manufacturing of ss316l using mig welding
UNIT 4 Total Quality Management .pptx
Mechanical Engineering MATERIALS Selection
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
CH1 Production IntroductoryConcepts.pptx
Geodesy 1.pptx...............................................
PPT on Performance Review to get promotions
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx

알고리즘 연합캠프 세미나 1-B (Bitwise DP)

  • 3. Bitwise Operator • A | B => A or B • A & B => A and B • A ^ B => A xor B • A = 1001, B = 0101 • A | B = 1101, A & B = 0001, A ^ B = 1100 • ~A => ones complement of A => • A << x => left shift A as x • A >> x => right shift A as x • A = 00110101 • ~A = 11001010, A << 2 = 11010100, A >> 2 = 00001101 BitwiseDP 3
  • 4. • S M . • add x: S x . x • remove x: S x . x • check x: S x 1, 0 • toggle x: S x x , x • all: S {1, 2, ..., 20} . • empty: S . • 1 ≤ x ≤ 20 • 1 ≤ M ≤ 3,000,000 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 4
  • 5. • bool exist [21] • exist[i]: i true, false BitwiseDP Problem: https://www.acmicpc.net/problem/11723 5
  • 7. • . • stat = 50 = 0….000000000000000110010(2) • stat: {2, 5, 6} • i-1 : i / • . BitwiseDP Problem: https://www.acmicpc.net/problem/11723 7
  • 8. • add x BitwiseDP Problem: https://www.acmicpc.net/problem/11723 8
  • 9. • add 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 9 …01010001(2) …01011001(2)
  • 10. • add 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 10 …01010001(2) + …00001000(2) …01011001(2)
  • 11. • add 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 11 …01011001(2) + …00001000(2)
  • 12. • add 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 12 …01011001(2) + …00001000(2) …01100001(2)
  • 13. • add 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 13 …0101?001(2) or …00001000(2)
  • 14. • add 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 14 …0101?001(2) or …00001000(2) …0101?001(2)
  • 15. • add 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 15 …0101?001(2) or …00001000(2) …0101?001(2)
  • 16. • add 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 16 …0101?001(2) or …00001000(2) …0101?001(2) 1|0 = 1 0|0 = 0 0 or
  • 17. • add 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 17 …0101?001(2) or …00001000(2) …0101?001(2)
  • 18. • add 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 18 …0101?001(2) or …00001000(2) …01011001(2) 1|1 = 1 0|1 = 1 1 or 1
  • 19. • add x = stat | (1 << (x – 1)) BitwiseDP Problem: https://www.acmicpc.net/problem/11723 19 …0101?001(2) or …00001000(2) …01011001(2)
  • 20. • remove 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 20 …0101?001(2)
  • 21. • remove 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 21 …0101?001(2) & …11110111(2) …0101?001(2)
  • 22. • remove 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 22 …0101?001(2) & …11110111(2) …0101?001(2)
  • 23. • remove 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 23 …0101?001(2) & …11110111(2) …0101?001(2) 1&1 = 1 0&1 = 0 1 and
  • 24. • remove 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 24 …0101?001(2) & …11110111(2) …0101?001(2)
  • 25. • remove 4 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 25 …0101?001(2) & …11110111(2) …01010001(2) 0&0 = 0 1&0 = 0 0 and 0
  • 26. • remove x = stat & ~(1 << (x – 1)) BitwiseDP Problem: https://www.acmicpc.net/problem/11723 26 …0101?001(2) & …11110111(2) …01010001(2)
  • 27. • bit[x] = 2x-1 • add: stat |= bit[x] • remove: stat &= ~bit[x] • check: stat & bit[x] • toggle: stat ^= bit[x] • all: stat = bit[21] - 1 • empty: stat = 0 BitwiseDP Problem: https://www.acmicpc.net/problem/11723 27
  • 29. • • N 0 9 • (0 .) • 1 ≤ N ≤ 100 BitwiseDP Problem: https://www.acmicpc.net/problem/1562 29
  • 30. • • D[i][j]: i j • D[i][j] = D[i – 1][j – 1] + D[i – 1][j + 1] BitwiseDP Problem: https://www.acmicpc.net/problem/1562 30
  • 31. • • D[i][j]: i j • D[i][j] = D[i – 1][j – 1] + D[i – 1][j + 1] • + 0 ~ 9 BitwiseDP Problem: https://www.acmicpc.net/problem/1562 31
  • 32. • D[i][j][use0][use1][use2]…[use9]: i j use0, use1, use2 .. use9 (use0, use1 … use9 = 0 or 1) BitwiseDP Problem: https://www.acmicpc.net/problem/1562 32
  • 33. • D[i][j][use0][use1][use2]…[use9]: i j use0, use1, use2 .. use9 (use0, use1 … use9 = 0 or 1) • [use0][use1][use2]…[use9]: 2 * 2 * 2 * … * 2 => 210 => 0000000000(2) ~ 1111111111(2) BitwiseDP Problem: https://www.acmicpc.net/problem/1562 33
  • 34. • D[i][j][used]: i j used • used: 0000000000(2) ~ 1111111111(2) => 210 • used k(0~9) bit: k BitwiseDP Problem: https://www.acmicpc.net/problem/1562 34
  • 35. • D[i][j][used]: i j used • used: 0000000000(2) ~ 1111111111(2) => 210 • used k(0~9) bit: k BitwiseDP Problem: https://www.acmicpc.net/problem/1562 used: 101 = 0001100101(2) 5 0 8 35
  • 36. • D[i][j][used]: i j used • used . • D[i][j - 1][used] -> D[i + 1][j][used | 2j] • D[i][j + 1][used] -> D[i + 1][j][used | 2j] BitwiseDP Problem: https://www.acmicpc.net/problem/1562 36
  • 37. • : ∑ 𝐷 𝑁 𝑖 [1023]- ./0 • N (used == 1111111111(2)) BitwiseDP Problem: https://www.acmicpc.net/problem/1562 37
  • 38. • : O(N * 10 * 210) • * * • 1 ≤ N ≤ 100 BitwiseDP Problem: https://www.acmicpc.net/problem/1562 38
  • 40. • 1 N . • N • . ( ) • Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 40
  • 41. • • * * … • N * (N – 1) * (N – 2) * … * 1 => O(N!) Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 41
  • 42. • • * * … • N * (N – 1) * (N – 2) * … * 1 => O(N!) • 2 ≤ N ≤ 16 Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 42
  • 43. • • * * … • N * (N – 1) * (N – 2) * … * 1 => O(N!) • 2 ≤ N ≤ 16 • 16! => 20,922,789,888,000 Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 43
  • 44. • 1 -> 2 -> 3 -> 4 -> ? • 1 -> 3 -> 2 -> 4 -> ? • : 1, 2, 3, 4 4 • : 4 1 Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 44
  • 45. • 1 -> 2 -> 3 -> 4 -> ? • 1 -> 3 -> 2 -> 4 -> ? • • . Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 45
  • 47. • • • / • (1 ) * (2 ) * (3 ) * … * (N ) • 2N Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 47
  • 48. • d[cur][visited]: i visited Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 48
  • 49. • d[cur][visited]: i visited Problem: https://www.acmicpc.net/problem/2098 BitwiseDP visited: 101 = 01100101(2) 49
  • 50. • d[cur][visited]: i visited Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 7 1 8 50 visited: 101 = 01100101(2)
  • 51. • d[cur][visited]: i visited • d[cur][visited] + cost[cur][next] -> d[next][visited + 2next] • 2next : next • next • visited & 2next == 0 Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 51
  • 52. • d[cur][visited] visited cur d[cur][visited] . Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 10011010 10011010 10011010 10011010 00011010 10001010 10010010 10011000 min 52 cur visited 2,4,5 2,4,8 2,5,8 4,5,8 cur visited 8 5 4 2
  • 53. • d[cur][visited] visited cur d[cur][visited] . • visited . Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 10011010 10011010 10011010 10011010 00011010 10001010 10010010 10011000 min 53 cur visited 2,4,5 2,4,8 2,5,8 4,5,8 cur visited 8 5 4 2
  • 54. • d[cur][visited]: i visited • d[cur][visited] = minpast ∈ visited (d[past][visited – 2cur] + cost[past][cur]) • 2cur : cur • past cur , visited • visited & 2past == 2past Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 54
  • 55. • • ( ) . Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 55
  • 56. • • ( ) . Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 56 P1 P2 P3 P4 P5 P6
  • 57. • • ( ) . Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 57 P1 P2 P3 P4 P5 P6
  • 58. • : O(N * N * 2N) • : O(N * 2N) • / : O(N) Problem: https://www.acmicpc.net/problem/2098 BitwiseDP 58
  • 60. • N X M • 2 X 1, 1 X 2 • • 1 ≤ N, M ≤ 14 Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 60
  • 61. • , . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 61 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 … … … …
  • 62. • , 0 . • (0, 1) (0, 10) Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 62 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 … … … …
  • 63. • (0, 1) Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 63 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 … … … …
  • 64. • 0 1 . • 0 , M-1 Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 … … … …
  • 65. • status = 20 + 21 Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 65 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 … … … … 0으로부터 한 줄의 상태: 0000000011(2)
  • 66. • (0, 10) Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 66 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 … … … …
  • 67. • status = 20 ( ) Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 67 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 … … … … 0으로부터 한 줄의 상태: 0000000001(2)
  • 68. • 1 , 0 , • 0 . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 68 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 … … … … 0으로부터 한 줄의 상태: 0000000001(2)
  • 69. • 0 10 , • 10 . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 69 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 … … … … 1로부터 ‘한 줄’의 상태: 1000000000(2)
  • 70. • i-1 0 -> i • i (i + 1) (i + M) . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 70 0 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 … …
  • 71. • i , (i – 1) • (i + M) . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 71 0 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 … …
  • 72. • i i M (i – 1) 1, (i + M) 0 . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 72 0 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 … …
  • 73. • 12 Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 73 0 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 … … 12부터 M칸의 상태: 1111001001(2)
  • 74. • 12 • 11 , 22 . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 74 0 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 … … 12부터 M칸의 상태: 1111001001(2)
  • 75. • 12 • 12 , 13 . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 75 0 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 … … 12부터 M칸의 상태: 1111001001(2)
  • 76. • 13 M • 12 M . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 76 0 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 … … 12부터 M칸의 상태: 1111001001(2)
  • 77. • status >> 1 (or status /= 2) Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 77 0 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 … … 13부터 M칸의 상태: 0111100100(2)
  • 78. • 13 • 13 , 23 . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 78 0 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 … … 12부터 M칸의 상태: 1111001001(2)
  • 79. • 13 • (13, 14) (13, 23) . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 79 0 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 … … 13부터 M칸의 상태: 0111100100(2)
  • 80. • (13, 14) (13, 14) . • 0 1 0 . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 80 0 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 … … 13부터 M칸의 상태: 0111100100(2)
  • 81. • (13, 14) , • 14 M . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 81 0 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 … … 13부터 M칸의 상태: 0111100100(2)
  • 82. • (status >> 1) + 20 • 14 M 14 0 Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 82 0 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 … … 14부터 M칸의 상태: 0011110011(2)
  • 83. • (13, 23) (13, 23) . • 23 . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 83 0 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 … … 13부터 M칸의 상태: 0111100100(2)
  • 84. • (13, 23) , • 14 M . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 84 0 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 … … 13부터 M칸의 상태: 0111100100(2)
  • 85. • (status >> 1) + 2M-1 • 14 M 23 M-1 . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 85 0 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 … … 14부터 M칸의 상태: 1011110010(2)
  • 86. • D[i][status]: i – 1 , i M status • 0 ≤ status < 2M Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 86
  • 87. • i (if (status & 1) == 1) • d[i + 1][status >> 1] += d[i][status] Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 87
  • 88. • i (if (status & 1) == 1) • d[i + 1][status >> 1] += d[i][status] • i • • Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 88
  • 89. • (if (status & 3) == 0) • d[i + 1][(status >> 1) + 1] += d[i][status] • (i (status & 1) == 0) • d[i + 1][(status >> 1) + 2M-1] += d[i][status] Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 89
  • 90. • (if (status & 3) == 0) • d[i + 1][(status >> 1) + 1] += d[i][status] • (i (status & 1) == 0) • d[i + 1][(status >> 1) + 2M-1] += d[i][status] • , i . Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 90
  • 91. • d[i + 1][(status >> 1)] += d[i][status] • if (status & 1) == 1 • d[i + 1][(status >> 1) + 1] += d[i][status] • if (status & 3) == 0, and (i % M) != M – 1 • d[i + 1][(status >> 1) + 2M-1] += d[i][status] • if (status & 1) == 0 Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 91
  • 92. • : d[N * M][0] • N * M (N * M – 1) , • N * M Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 92
  • 93. • : O(N * M * 2M) • : O(N * M) • : O(2M) Problem: https://www.acmicpc.net/problem/1648 BitwiseDP 93
  • 95. • N X M • 2 X 1, 1 X 2 • . • . • • 1 ≤ N, M ≤ 14 Problem: https://www.acmicpc.net/problem/1657 BitwiseDP 95
  • 96. • == • , . Problem: https://www.acmicpc.net/problem/1657 BitwiseDP 96
  • 97. • == • , . • , . Problem: https://www.acmicpc.net/problem/1657 BitwiseDP 97
  • 98. • D[i][status]: i – 1 ( ) , i M status • 0 ≤ status < 2M Problem: https://www.acmicpc.net/problem/1657 BitwiseDP 98
  • 99. • i (if (status & 1) == 1) • d[i + 1][status >> 1] <- d[i][status] Problem: https://www.acmicpc.net/problem/1657 BitwiseDP 99
  • 100. • i (if (status & 1) == 1) • d[i + 1][status >> 1] <- d[i][status] • i • d[i + 1][status >> 1] <- d[i][status] • d[i + 1][(status >> 1) + 1] += d[i][status] ( ) • d[i + 1][(status >> 1) + 2M-1] += d[i][status] ( ) Problem: https://www.acmicpc.net/problem/1657 BitwiseDP 100
  • 101. • d[i + 1][(status >> 1)] <- d[i][status] • always possible • d[i + 1][(status >> 1) + 1] += d[i][status] • if (status & 3) == 0, and (i % M) != M – 1 • d[i + 1][(status >> 1) + 2M-1] += d[i][status] • if (status & 1) == 0 Problem: https://www.acmicpc.net/problem/1657 BitwiseDP 101
  • 102. • : d[N * M][0] • N * M (N * M – 1) ( ) , • N * M Problem: https://www.acmicpc.net/problem/1657 BitwiseDP 102
  • 103. • : O(N * M * 2M) • : O(N * M) • : O(2M) Problem: https://www.acmicpc.net/problem/1657 BitwiseDP 103