Вы находитесь на странице: 1из 8

The Magic-Box Problem

Imagine a jar containing beads numbered from 1 to N, and an unlimited supply of magic boxes, each with b compartments, called
cells, where b ³ 2. All boxes have the same number of cells.
You may put in each cell nothing, or one bead, or another magic box, nested. The boxes are magical because they resize when you
nest them.
Your task, for any N and any b, is to put all the beads in one box using the smallest possible total number of boxes .

Box-Drawing Primitive
Draw a box with either vertical or horizontal orientation containing the elements in list xs and using the value of y, defaulting to the
empty string, to denote empty or "wasted" cells.

vhBoxAvP_, base_, xs_, y_: ""E :=


Style@Grid@If@vP,
List ž PadRight@xs, base, yD,
ListžPadRight@xs, base, yDD,
Frame ® AllD, TinyD
A couple of unit tests:

In[121]:= vhBox@False, 10, 81, 2, 3<D

Out[121]= 1 2 3

In[122]:= vhBox@True, 10, 81, 2, 3<D

1
2
3

Out[122]=

A Candidate Solution
The following candidate puts the beads in the boxes fairly efficiently, but may use more boxes than necessary.

First, if the number of beads N is less than or equal to the number of cells b in one box, just use one box, wasting at most b - 1
cells. This situation also shows that we may be forced to waste (leave empty) at least b - 1 cells.

Questions to Investigate
Can we show that it is never necessary to waste more than b - 1 cells? Is there a relationship between the maximum number of
wasted cells and the number of boxes used?
2 box004.nb

Observation
If the number N of beads is an (integer) power of of the box-size b, the beads fit perfectly in nested boxes with no wasted cells.
Define a perfectly filled box as one all of whose cells contain either individual beads or perfectly filled boxes. The number of
nesting levels of a perfectly filled box is the log, base b, of the number of beads in the perfectly filled box. This relationship extends
to the case of 0 beads, in which case we have one completely empty box -- it's not perfectly filled, but rather perfectly empty. The
number of boxes in a perfectly filled box is either 1, or 1 + b, or 1 + b + b2 , and so on. The number of boxes in a perfectly filled box
with k nesting levels is thus Ibk+1 - 1M ‘ Hb - 1L.

Construction
Basic idea: find the biggest power of b less than N and call it clumpSize. The number of clumps of size clumpSize is less than N by
construction, and each clump can fill a perfectly filled box. Take away most of the beads by putting them in multiples of clumpSize
into perfectly filled boxes. Distribute the remaining beads by putting as many individual beads at top level as possible, honoring an
guess or intuiion that wasting the fewest number of cells relates to using the fewest number of boxes.

boxBHelperAn_, z_, b_E :=


H* If the number of beads, n, is less than or
equal to the number of cells in a single box, *L
If@n £ b,
H* then just fill it as much as we can and we're done. *L
vhBox@False, b, Table@i + z, 8i, n<DD,
H* Otherwise, we must nest some boxes. *L
Module@8e, clumpSize, nPreClumps, preClumpsTotal, lastClump<,
H* This is the largest exponent such that b^e < n *L
e = Ceiling@Log@b, nD - 1D;
clumpSize = b^e;
H* The following is the number of
clumps we may put in perfectly filled boxes. *L
nPreClumps = n~Quotient~clumpSize;
H* The following is the total
number of beads put in perfectly filled boxes. *L
preClumpsTotal = clumpSize * nPreClumps;
H* The following is the
number of remaining beads to distribute. *L
lastClump = n - preClumpsTotal;
box004.nb 3

lastClump = n - preClumpsTotal;
H* The "return value" of this function is a box that contains
all the beads and nested boxes. This is the outer box. The
"OddQ" parameter just flips the boxes from horizontal
to vertical orientation for display purposes. *L
vhBox@OddQ@eD, b,
H* Build the perfectly filled boxes: *L
With@8pre = Table@boxBHelper@clumpSize,
z + Hi - 1L * clumpSize, bD, 8i, nPreClumps<D<,
H* If there are some remaining beads to distribute: *L
If@lastClump ¹ 0,
H* There are b - nPreClumps empty cells remaining at this
level; If that is enough room for lastClump beads: *L
If@lastClump £ b - nPreClumps,
H* Put all remaining beads at this level,
and we're done. *L
pre~Join~Table@z + preClumpsTotal + i, 8i, lastClump<D,
H* Otherwise,
there are too many remaining beads for this level.
There will certainly be a recursive box to contain
some of the remaining beads; so the number of cells
remaining at this level is one fewer than b-nPreClumps,
accounting for the one cell taken up by the recursive
box. Put as many as possible at this level. *L
With@8nRemainingCells = b - nPreClumps - 1<,
H* Filling up all remaining
cells at this level with single beads,
and putting the leftovers in a nested box.*L
Hpre~ Append ~ boxBHelper@lastClump - nRemainingCells,
z + preClumpsTotal, bDL~Join~
Table@z + preClumpsTotal + lastClump - nRemainingCells + i,
8i, nRemainingCells<DDD,
H* The following is the other branch of the top
; *L
4 box004.nb

H* The following is the other branch of the top


If: The last clump is zero; we're done. *L
preDDDDD;
boxBAn_, b_E := boxBHelper@n, 0, bD;

In[152]:= boxB@ð, 2D & ž Range@0, 16D

:
1 2 1 2 1 2 1 2 1 2 5 6
Out[152]= , 1 , 1 2 , , , 5 , 5 6 , ,
3 3 4 3 4 3 4 3 4 7

1 2 5 6 1 2 5 6

1 2 5 6 1 2 5 6
1 2 5 6 3 4 7 8 3 4 7 8
, 3 4 7 8 , 3 4 7 8 , , ,
3 4 7 8
9 10 9 10
9 9 10
11 11 12

1 2 5 6 1 2 5 6 1 2 5 6 1 2 5 6

>
3 4 7 8 3 4 7 8 3 4 7 8 3 4 7 8

, , ,
9 10 9 10 9 10 13 14 9 10 13 14
13 13 14
11 12 11 12 11 12 15 11 12 15 16

In[153]:= boxB@ð, 3D & ž Range žž 80, 19<

1 2 3 1 2 3 1 2 3

:
1 2 3 1 2 3 1 2 3
1 2 3
Out[153]= , 1 , 1 2 , 1 2 3 , 4 , 4 , 4 5 6 , 4 5 6 , 4 5 6 , 4 5 6 , 4 5 6 10 ,
5 7 7 8 7 8 9 7 8 9

1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
10 11 12 10 11 12
4 5 6 10 11 , 4 5 6 10 11 12 , 4 5 6 10 11 12 13 , 4 5 6
13 14 , 4 5 6
13 15 ,
7 8 9 7 8 9 7 8 9 7 8 9 7 8 9 14

1 2 3 1 2 3 1 2 3 10 11 12 1 2 3 10 11 12

>
10 11 12 10 11 12

4 5 6
13 14 15 16 , 4 5 6 13 14 15 17 , 4 5 6 13 14 15 , 4 5 6 13 14 15 19

7 8 9 7 8 9 16 7 8 9 16 17 18 7 8 9 16 17 18

In[154]:= boxB@ð, 4D & ž Range@0, 49D

Out[154]=
box004.nb 5

1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

:
1 2 3 4
5 6 7 8 5 6 7 8
, , , , , 5 , 5 , 5 , 5 6 7 8 , 5 6 7 8 , , ,
Out[154]= 1 1 2 1 2 3 1 2 3 4
6 6 9 9 10
9
7 10 11

1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
1 2 3 4 1 2 3 4

5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8
5 6 7 8 5 6 7 8
, , , , , 17 , 17 18 ,
9 10 11 12 9 10 11 12 9 10 11 12 9 10 11 12 9 10 11 12
9 10 11 12 9 10 11 12

13 13 14 13 14 15 13 14 15 16 13 14 15 16 13 14 15 16

1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8
17 18 19 , 17 18 19 20 , 17 18 19 20 21 , 17 18 19 20 21 22 ,
9 10 11 12 9 10 11 12 9 10 11 12 9 10 11 12

13 14 15 16 13 14 15 16 13 14 15 16 13 14 15 16

1 2 3 4 1 2 3 4 1 2 3 4
17 18 19 20 17 18 19 20 17 18 19 20
5 6 7 8 5 6 7 8 5 6 7 8
21 21
21 22 23 , 23 24 , 24 25 ,
9 10 11 12 9 10 11 12 22 9 10 11 12 22
23
13 14 15 16 13 14 15 16 13 14 15 16

1 2 3 4 1 2 3 4 1 2 3 4
17 18 19 20 17 18 19 20 17 18 19 20
5 6 7 8 5 6 7 8 5 6 7 8
21 22 23 24 21 22 23 24 21 22 23 24
25 26 , 26 27 , 27 28 ,
9 10 11 12 9 10 11 12 9 10 11 12
25 25

13 14 15 16 13 14 15 16 13 14 15 16 26

1 2 3 4 17 18 19 20 1 2 3 4 1 2 3 4 17 18 19 20
17 18 19 20

5 6 7 8 21 22 23 24 5 6 7 8 5 6 7 8 21 22 23 24
21 22 23 24
28 29 , 29 30 , 30 31 ,
9 10 11 12 25 26 9 10 11 12 9 10 11 12 25 26 27 28
25 26 27 28

13 14 15 16 27 13 14 15 16 13 14 15 16 29

1 2 3 4 17 18 19 20 1 2 3 4 17 18 19 20 1 2 3 4 17 18 19 20

5 6 7 8 21 22 23 24 5 6 7 8 21 22 23 24 5 6 7 8 21 22 23 24
, 33 , 33 34 ,
9 10 11 12 25 26 27 28 9 10 11 12 25 26 27 28 9 10 11 12 25 26 27 28

13 14 15 16 29 30 31 32 13 14 15 16 29 30 31 32 13 14 15 16 29 30 31 32

, , ,
6 box004.nb

1 2 3 4 17 18 19 20 1 2 3 4 17 18 19 20 1 2 3 4 17 18 19 20

5 6 7 8 21 22 23 24 5 6 7 8 21 22 23 24 5 6 7 8 21 22 23 24
33 34 35 , 33 34 35 36 , 33 34 35 36 37 ,
9 10 11 12 25 26 27 28 9 10 11 12 25 26 27 28 9 10 11 12 25 26 27 28

13 14 15 16 29 30 31 32 13 14 15 16 29 30 31 32 13 14 15 16 29 30 31 32

1 2 3 4 17 18 19 20 1 2 3 4 17 18 19 20
33 34 35 36 33 34 35 36
5 6 7 8 21 22 23 24 5 6 7 8 21 22 23 24
37 38 , 37 39 ,
9 10 11 12 25 26 27 28 9 10 11 12 25 26 27 28 38

13 14 15 16 29 30 31 32 13 14 15 16 29 30 31 32

1 2 3 4 17 18 19 20 1 2 3 4 17 18 19 20
33 34 35 36 33 34 35 36
5 6 7 8 21 22 23 24 5 6 7 8 21 22 23 24
37 37 38 39 40
40 , 41 ,
9 10 11 12 25 26 27 28 38 9 10 11 12 25 26 27 28
39
13 14 15 16 29 30 31 32 13 14 15 16 29 30 31 32

1 2 3 4 17 18 19 20 1 2 3 4 17 18 19 20
33 34 35 36 33 34 35 36
5 6 7 8 21 22 23 24 5 6 7 8 21 22 23 24
37 38 39 40 37 38 39 40
42 , 43 ,
9 10 11 12 25 26 27 28 9 10 11 12 25 26 27 28
41 41

13 14 15 16 29 30 31 32 13 14 15 16 29 30 31 32 42

1 2 3 4 17 18 19 20 33 34 35 36 1 2 3 4 17 18 19 20
33 34 35 36

5 6 7 8 21 22 23 24 37 38 39 40 5 6 7 8 21 22 23 24
37 38 39 40
44 , 45 ,
9 10 11 12 25 26 27 28 41 42 9 10 11 12 25 26 27 28
41 42 43 44

13 14 15 16 29 30 31 32 43 13 14 15 16 29 30 31 32

1 2 3 4 17 18 19 20 33 34 35 36 1 2 3 4 17 18 19 20 33 34 35 36

5 6 7 8 21 22 23 24 37 38 39 40 5 6 7 8 21 22 23 24 37 38 39 40
46 , 47 ,
9 10 11 12 25 26 27 28 41 42 43 44 9 10 11 12 25 26 27 28 41 42 43 44

13 14 15 16 29 30 31 32 45 13 14 15 16 29 30 31 32 45 46

1 2 3 4 17 18 19 20 33 34 35 36 1 2 3 4 17 18 19 20 33 34 35 36

>
5 6 7 8 21 22 23 24 37 38 39 40 5 6 7 8 21 22 23 24 37 38 39 40
, 49
9 10 11 12 25 26 27 28 41 42 43 44 9 10 11 12 25 26 27 28 41 42 43 44

13 14 15 16 29 30 31 32 45 46 47 48 13 14 15 16 29 30 31 32 45 46 47 48

In[158]:= boxB@ð, 10D & ž 8199, 200, 201, 208, 209<

Out[158]=
box004.nb 7

1 2 3 4 5 6 7 8 9 10 101 102 103 104 105 106 107 108 109 110

11 12 13 14 15 16 17 18 19 20 111 112 113 114 115 116 117 118 119 120

21 22 23 24 25 26 27 28 29 30 121 122 123 124 125 126 127 128 129 130

31 32 33 34 35 36 37 38 39 40 131 132 133 134 135 136 137 138 139 140

:
41 42 43 44 45 46 47 48 49 50 141 142 143 144 145 146 147 148 149 150
Out[158]= 192 193 194 195 196 197 198 199 ,
51 52 53 54 55 56 57 58 59 60 151 152 153 154 155 156 157 158 159 160

61 62 63 64 65 66 67 68 69 70 161 162 163 164 165 166 167 168 169 170

71 72 73 74 75 76 77 78 79 80 171 172 173 174 175 176 177 178 179 180

81 82 83 84 85 86 87 88 89 90 181 182 183 184 185 186 187 188 189 190

91 92 93 94 95 96 97 98 99 100 191

1 2 3 4 5 6 7 8 9 10 101 102 103 104 105 106 107 108 109 110

11 12 13 14 15 16 17 18 19 20 111 112 113 114 115 116 117 118 119 120

21 22 23 24 25 26 27 28 29 30 121 122 123 124 125 126 127 128 129 130

31 32 33 34 35 36 37 38 39 40 131 132 133 134 135 136 137 138 139 140

41 42 43 44 45 46 47 48 49 50 141 142 143 144 145 146 147 148 149 150
,
51 52 53 54 55 56 57 58 59 60 151 152 153 154 155 156 157 158 159 160

61 62 63 64 65 66 67 68 69 70 161 162 163 164 165 166 167 168 169 170

71 72 73 74 75 76 77 78 79 80 171 172 173 174 175 176 177 178 179 180

81 82 83 84 85 86 87 88 89 90 181 182 183 184 185 186 187 188 189 190

91 92 93 94 95 96 97 98 99 100 191 192 193 194 195 196 197 198 199 200

,
8 box004.nb

1 2 3 4 5 6 7 8 9 10 101 102 103 104 105 106 107 108 109 110

11 12 13 14 15 16 17 18 19 20 111 112 113 114 115 116 117 118 119 120

21 22 23 24 25 26 27 28 29 30 121 122 123 124 125 126 127 128 129 130

31 32 33 34 35 36 37 38 39 40 131 132 133 134 135 136 137 138 139 140

41 42 43 44 45 46 47 48 49 50 141 142 143 144 145 146 147 148 149 150
201 ,
51 52 53 54 55 56 57 58 59 60 151 152 153 154 155 156 157 158 159 160

61 62 63 64 65 66 67 68 69 70 161 162 163 164 165 166 167 168 169 170

71 72 73 74 75 76 77 78 79 80 171 172 173 174 175 176 177 178 179 180

81 82 83 84 85 86 87 88 89 90 181 182 183 184 185 186 187 188 189 190

91 92 93 94 95 96 97 98 99 100 191 192 193 194 195 196 197 198 199 200

1 2 3 4 5 6 7 8 9 10 101 102 103 104 105 106 107 108 109 110

11 12 13 14 15 16 17 18 19 20 111 112 113 114 115 116 117 118 119 120

21 22 23 24 25 26 27 28 29 30 121 122 123 124 125 126 127 128 129 130

31 32 33 34 35 36 37 38 39 40 131 132 133 134 135 136 137 138 139 140

41 42 43 44 45 46 47 48 49 50 141 142 143 144 145 146 147 148 149 150
201 202 203 204 205 206 207 208 ,
51 52 53 54 55 56 57 58 59 60 151 152 153 154 155 156 157 158 159 160

61 62 63 64 65 66 67 68 69 70 161 162 163 164 165 166 167 168 169 170

71 72 73 74 75 76 77 78 79 80 171 172 173 174 175 176 177 178 179 180

81 82 83 84 85 86 87 88 89 90 181 182 183 184 185 186 187 188 189 190

91 92 93 94 95 96 97 98 99 100 191 192 193 194 195 196 197 198 199 200

1 2 3 4 5 6 7 8 9 10 101 102 103 104 105 106 107 108 109 110

11 12 13 14 15 16 17 18 19 20 111 112 113 114 115 116 117 118 119 120

21 22 23 24 25 26 27 28 29 30 121 122 123 124 125 126 127 128 129 130

31 32 33 34 35 36 37 38 39 40 131 132 133 134 135 136 137 138 139 140

>
41 42 43 44 45 46 47 48 49 50 141 142 143 144 145 146 147 148 149 150
201 202 203 204 205 206 207 208 209
51 52 53 54 55 56 57 58 59 60 151 152 153 154 155 156 157 158 159 160

61 62 63 64 65 66 67 68 69 70 161 162 163 164 165 166 167 168 169 170

71 72 73 74 75 76 77 78 79 80 171 172 173 174 175 176 177 178 179 180

81 82 83 84 85 86 87 88 89 90 181 182 183 184 185 186 187 188 189 190

91 92 93 94 95 96 97 98 99 100 191 192 193 194 195 196 197 198 199 200

Вам также может понравиться