|
> No.1970[元記事へ]
> 問題
> 相異なるn個のボールと区別のつかないm個の箱がある。
> 各箱にボールを1個以上配る配り方は何通りあるか。
>
> 答え
> 第2種スターリング数 S2(n,m)
漸化式をもとに並びを列挙してみました。
!第2種スターリング数 S2(n,k) になる並びの列挙
LET N=7 !1≦N
LET K=3 !1≦K≦N
LET M=StirlingS2(N,K)
PRINT M !debug
DIM A$(M,K) !m個の並び
FOR i=1 TO M
FOR J=1 TO K
LET A$(i,J)=""
NEXT J
NEXT i
LET C=0
CALL try(N,K, C,A$)
FOR i=1 TO C !結果を表示する
PRINT i;": ";
FOR J=1 TO K
PRINT A$(i,j);" ";
NEXT J
PRINT
NEXT i
END
!第2種スターリング数の表と漸化式との関連
! ×1 ×2 ×3 ×4 ×5 ×6 ×7 …
!1: 1
!2: ① ①
! \│
!3: ① (3) ①
! \│ \│
!4: ① (7) (6) 1
! \│ \│
!5: ① (15) (25) 10 1
! \│ \│
!6: 1 (31) (90) 65 15 1
! \│
!7: 1 63 301 350 140 21 1
! :
! :
EXTERNAL SUB try(N,K, C,A$(,)) !漸化式をもとに並びを列挙していく
IF K=1 THEN !左端 S2(n,1) のとき、並び{1,2,3,…,n}
LET C=1
LET A$(C,1)="1"
FOR i=2 TO N
LET A$(C,1)=A$(C,1) & "," & STR$(i)
NEXT i
LET A$(C,1)="{" & A$(C,1) & "}"
PRINT N;K;C !debug
ELSEIF K=N THEN !対角線位置 S2(n,n) のとき、並び{{1},{2},{3},…,{n}}
LET C=1
FOR i=1 TO N
LET A$(C,i)="{" & STR$(i) & "}"
NEXT i
PRINT N;K;C !debug
ELSE
!漸化式 S2(n,k) = S2(n-1,k-1) + k*S2(n-1,k) より、
!左側 並び{(1,2,3,…,n-1),{n]}
!ただし、(1,2,3,…,n-1)は、1から(n-1)までの数字による(k-1)個の並びである。
LET T=StirlingS2(N-1,K-1) !作業変数
DIM L$(T,K)
CALL try(N-1,K-1, L,L$) !S2(n-1,k-1)の並び
!S2(n,k)の並びを生成する
FOR i=1 TO L ! !数字nをk番目に追加する
FOR J=1 TO K-1
LET A$(i,J)=L$(i,J)
NEXT J
LET A$(i,K)="{" & STR$(N) & "}"
NEXT i
!右側 並び[{1,n},2,3,…,k]、[1,{2,n},3,…,k]、… 、[1,2,3,…,{k,n}]
!ただし、[1,2,3,…,k]は、1から(n-1)までの数字によるk個の並びである。
LET T=StirlingS2(N-1,K) !作業変数
DIM R$(T,K),D$(K*T,K)
CALL try(N-1,K, R,R$) !S2(n-1,k)の並び
FOR i=1 TO R !数字nを1からk番目に追加する
FOR J=1 TO K
FOR M=1 TO K !個数はk倍になる
IF M=J THEN
LET W$=R$(i,J)(2:LEN(R$(i,J))-1) !両端の{}を削除する
LET D$(K*(i-1)+M,J)="{" & W$ & "," & STR$(N) & "}"
ELSE
LET D$(K*(i-1)+M,J)=R$(i,J)
END IF
NEXT M
NEXT J
NEXT i
!S2(n,k)の並びを生成する
FOR i=1 TO K*R
FOR J=1 TO K
LET A$(i+L,J)=D$(i,J)
NEXT J
NEXT i
LET C=L+K*R !個数
PRINT "(";N;",";K;")=";C !debug
END IF
END SUB
EXTERNAL FUNCTION StirlingS2(n,k) !第2種スターリング数 ※n>0
IF k<1 OR k>n THEN
LET StirlingS2=0
ELSEIF k=1 OR k=n THEN
LET StirlingS2=1
ELSE
LET StirlingS2=StirlingS2(n-1,k-1) + k*StirlingS2(n-1,k)
END IF
END FUNCTION
実行結果
301
5 1 1
4 1 1
3 1 1
2 1 1
2 2 1
( 3 , 2 )= 3
( 4 , 2 )= 7
( 5 , 2 )= 15
( 6 , 2 )= 31
4 1 1
3 1 1
2 1 1
2 2 1
( 3 , 2 )= 3
( 4 , 2 )= 7
( 5 , 2 )= 15
3 1 1
2 1 1
2 2 1
( 3 , 2 )= 3
( 4 , 2 )= 7
2 1 1
2 2 1
( 3 , 2 )= 3
3 3 1
( 4 , 3 )= 6
( 5 , 3 )= 25
( 6 , 3 )= 90
( 7 , 3 )= 301
1 : {1,2,3,4,5} {6} {7}
2 : {1,2,3,4,6} {5} {7}
3 : {1,2,3,4} {5,6} {7}
4 : {1,2,3,5,6} {4} {7}
5 : {1,2,3,5} {4,6} {7}
6 : {1,2,3,6} {4,5} {7}
7 : {1,2,3} {4,5,6} {7}
8 : {1,2,4,5,6} {3} {7}
9 : {1,2,4,5} {3,6} {7}
10 : {1,2,4,6} {3,5} {7}
11 : {1,2,4} {3,5,6} {7}
12 : {1,2,5,6} {3,4} {7}
13 : {1,2,5} {3,4,6} {7}
14 : {1,2,6} {3,4,5} {7}
15 : {1,2} {3,4,5,6} {7}
16 : {1,3,4,5,6} {2} {7}
17 : {1,3,4,5} {2,6} {7}
18 : {1,3,4,6} {2,5} {7}
19 : {1,3,4} {2,5,6} {7}
20 : {1,3,5,6} {2,4} {7}
21 : {1,3,5} {2,4,6} {7}
22 : {1,3,6} {2,4,5} {7}
23 : {1,3} {2,4,5,6} {7}
24 : {1,4,5,6} {2,3} {7}
25 : {1,4,5} {2,3,6} {7}
26 : {1,4,6} {2,3,5} {7}
27 : {1,4} {2,3,5,6} {7}
28 : {1,5,6} {2,3,4} {7}
29 : {1,5} {2,3,4,6} {7}
30 : {1,6} {2,3,4,5} {7}
31 : {1} {2,3,4,5,6} {7}
32 : {1,2,3,4,7} {5} {6}
33 : {1,2,3,4} {5,7} {6}
34 : {1,2,3,4} {5} {6,7}
35 : {1,2,3,5,7} {4} {6}
36 : {1,2,3,5} {4,7} {6}
37 : {1,2,3,5} {4} {6,7}
38 : {1,2,3,7} {4,5} {6}
39 : {1,2,3} {4,5,7} {6}
40 : {1,2,3} {4,5} {6,7}
41 : {1,2,4,5,7} {3} {6}
42 : {1,2,4,5} {3,7} {6}
43 : {1,2,4,5} {3} {6,7}
44 : {1,2,4,7} {3,5} {6}
45 : {1,2,4} {3,5,7} {6}
46 : {1,2,4} {3,5} {6,7}
47 : {1,2,5,7} {3,4} {6}
48 : {1,2,5} {3,4,7} {6}
49 : {1,2,5} {3,4} {6,7}
50 : {1,2,7} {3,4,5} {6}
51 : {1,2} {3,4,5,7} {6}
52 : {1,2} {3,4,5} {6,7}
53 : {1,3,4,5,7} {2} {6}
54 : {1,3,4,5} {2,7} {6}
55 : {1,3,4,5} {2} {6,7}
56 : {1,3,4,7} {2,5} {6}
57 : {1,3,4} {2,5,7} {6}
58 : {1,3,4} {2,5} {6,7}
59 : {1,3,5,7} {2,4} {6}
60 : {1,3,5} {2,4,7} {6}
61 : {1,3,5} {2,4} {6,7}
62 : {1,3,7} {2,4,5} {6}
63 : {1,3} {2,4,5,7} {6}
64 : {1,3} {2,4,5} {6,7}
65 : {1,4,5,7} {2,3} {6}
66 : {1,4,5} {2,3,7} {6}
67 : {1,4,5} {2,3} {6,7}
68 : {1,4,7} {2,3,5} {6}
69 : {1,4} {2,3,5,7} {6}
70 : {1,4} {2,3,5} {6,7}
71 : {1,5,7} {2,3,4} {6}
72 : {1,5} {2,3,4,7} {6}
73 : {1,5} {2,3,4} {6,7}
74 : {1,7} {2,3,4,5} {6}
75 : {1} {2,3,4,5,7} {6}
76 : {1} {2,3,4,5} {6,7}
77 : {1,2,3,6,7} {4} {5}
78 : {1,2,3,6} {4,7} {5}
79 : {1,2,3,6} {4} {5,7}
80 : {1,2,3,7} {4,6} {5}
81 : {1,2,3} {4,6,7} {5}
82 : {1,2,3} {4,6} {5,7}
83 : {1,2,3,7} {4} {5,6}
84 : {1,2,3} {4,7} {5,6}
85 : {1,2,3} {4} {5,6,7}
86 : {1,2,4,6,7} {3} {5}
87 : {1,2,4,6} {3,7} {5}
88 : {1,2,4,6} {3} {5,7}
89 : {1,2,4,7} {3,6} {5}
90 : {1,2,4} {3,6,7} {5}
91 : {1,2,4} {3,6} {5,7}
92 : {1,2,4,7} {3} {5,6}
93 : {1,2,4} {3,7} {5,6}
94 : {1,2,4} {3} {5,6,7}
95 : {1,2,6,7} {3,4} {5}
96 : {1,2,6} {3,4,7} {5}
97 : {1,2,6} {3,4} {5,7}
98 : {1,2,7} {3,4,6} {5}
99 : {1,2} {3,4,6,7} {5}
100 : {1,2} {3,4,6} {5,7}
101 : {1,2,7} {3,4} {5,6}
102 : {1,2} {3,4,7} {5,6}
103 : {1,2} {3,4} {5,6,7}
104 : {1,3,4,6,7} {2} {5}
105 : {1,3,4,6} {2,7} {5}
106 : {1,3,4,6} {2} {5,7}
107 : {1,3,4,7} {2,6} {5}
108 : {1,3,4} {2,6,7} {5}
109 : {1,3,4} {2,6} {5,7}
110 : {1,3,4,7} {2} {5,6}
111 : {1,3,4} {2,7} {5,6}
112 : {1,3,4} {2} {5,6,7}
113 : {1,3,6,7} {2,4} {5}
114 : {1,3,6} {2,4,7} {5}
115 : {1,3,6} {2,4} {5,7}
116 : {1,3,7} {2,4,6} {5}
117 : {1,3} {2,4,6,7} {5}
118 : {1,3} {2,4,6} {5,7}
119 : {1,3,7} {2,4} {5,6}
120 : {1,3} {2,4,7} {5,6}
121 : {1,3} {2,4} {5,6,7}
122 : {1,4,6,7} {2,3} {5}
123 : {1,4,6} {2,3,7} {5}
124 : {1,4,6} {2,3} {5,7}
125 : {1,4,7} {2,3,6} {5}
126 : {1,4} {2,3,6,7} {5}
127 : {1,4} {2,3,6} {5,7}
128 : {1,4,7} {2,3} {5,6}
129 : {1,4} {2,3,7} {5,6}
130 : {1,4} {2,3} {5,6,7}
131 : {1,6,7} {2,3,4} {5}
132 : {1,6} {2,3,4,7} {5}
133 : {1,6} {2,3,4} {5,7}
134 : {1,7} {2,3,4,6} {5}
135 : {1} {2,3,4,6,7} {5}
136 : {1} {2,3,4,6} {5,7}
137 : {1,7} {2,3,4} {5,6}
138 : {1} {2,3,4,7} {5,6}
139 : {1} {2,3,4} {5,6,7}
140 : {1,2,5,6,7} {3} {4}
141 : {1,2,5,6} {3,7} {4}
142 : {1,2,5,6} {3} {4,7}
143 : {1,2,5,7} {3,6} {4}
144 : {1,2,5} {3,6,7} {4}
145 : {1,2,5} {3,6} {4,7}
146 : {1,2,5,7} {3} {4,6}
147 : {1,2,5} {3,7} {4,6}
148 : {1,2,5} {3} {4,6,7}
149 : {1,2,6,7} {3,5} {4}
150 : {1,2,6} {3,5,7} {4}
151 : {1,2,6} {3,5} {4,7}
152 : {1,2,7} {3,5,6} {4}
153 : {1,2} {3,5,6,7} {4}
154 : {1,2} {3,5,6} {4,7}
155 : {1,2,7} {3,5} {4,6}
156 : {1,2} {3,5,7} {4,6}
157 : {1,2} {3,5} {4,6,7}
158 : {1,2,6,7} {3} {4,5}
159 : {1,2,6} {3,7} {4,5}
160 : {1,2,6} {3} {4,5,7}
161 : {1,2,7} {3,6} {4,5}
162 : {1,2} {3,6,7} {4,5}
163 : {1,2} {3,6} {4,5,7}
164 : {1,2,7} {3} {4,5,6}
165 : {1,2} {3,7} {4,5,6}
166 : {1,2} {3} {4,5,6,7}
167 : {1,3,5,6,7} {2} {4}
168 : {1,3,5,6} {2,7} {4}
169 : {1,3,5,6} {2} {4,7}
170 : {1,3,5,7} {2,6} {4}
171 : {1,3,5} {2,6,7} {4}
172 : {1,3,5} {2,6} {4,7}
173 : {1,3,5,7} {2} {4,6}
174 : {1,3,5} {2,7} {4,6}
175 : {1,3,5} {2} {4,6,7}
176 : {1,3,6,7} {2,5} {4}
177 : {1,3,6} {2,5,7} {4}
178 : {1,3,6} {2,5} {4,7}
179 : {1,3,7} {2,5,6} {4}
180 : {1,3} {2,5,6,7} {4}
181 : {1,3} {2,5,6} {4,7}
182 : {1,3,7} {2,5} {4,6}
183 : {1,3} {2,5,7} {4,6}
184 : {1,3} {2,5} {4,6,7}
185 : {1,3,6,7} {2} {4,5}
186 : {1,3,6} {2,7} {4,5}
187 : {1,3,6} {2} {4,5,7}
188 : {1,3,7} {2,6} {4,5}
189 : {1,3} {2,6,7} {4,5}
190 : {1,3} {2,6} {4,5,7}
191 : {1,3,7} {2} {4,5,6}
192 : {1,3} {2,7} {4,5,6}
193 : {1,3} {2} {4,5,6,7}
194 : {1,5,6,7} {2,3} {4}
195 : {1,5,6} {2,3,7} {4}
196 : {1,5,6} {2,3} {4,7}
197 : {1,5,7} {2,3,6} {4}
198 : {1,5} {2,3,6,7} {4}
199 : {1,5} {2,3,6} {4,7}
200 : {1,5,7} {2,3} {4,6}
201 : {1,5} {2,3,7} {4,6}
202 : {1,5} {2,3} {4,6,7}
203 : {1,6,7} {2,3,5} {4}
204 : {1,6} {2,3,5,7} {4}
205 : {1,6} {2,3,5} {4,7}
206 : {1,7} {2,3,5,6} {4}
207 : {1} {2,3,5,6,7} {4}
208 : {1} {2,3,5,6} {4,7}
209 : {1,7} {2,3,5} {4,6}
210 : {1} {2,3,5,7} {4,6}
211 : {1} {2,3,5} {4,6,7}
212 : {1,6,7} {2,3} {4,5}
213 : {1,6} {2,3,7} {4,5}
214 : {1,6} {2,3} {4,5,7}
215 : {1,7} {2,3,6} {4,5}
216 : {1} {2,3,6,7} {4,5}
217 : {1} {2,3,6} {4,5,7}
218 : {1,7} {2,3} {4,5,6}
219 : {1} {2,3,7} {4,5,6}
220 : {1} {2,3} {4,5,6,7}
221 : {1,4,5,6,7} {2} {3}
222 : {1,4,5,6} {2,7} {3}
223 : {1,4,5,6} {2} {3,7}
224 : {1,4,5,7} {2,6} {3}
225 : {1,4,5} {2,6,7} {3}
226 : {1,4,5} {2,6} {3,7}
227 : {1,4,5,7} {2} {3,6}
228 : {1,4,5} {2,7} {3,6}
229 : {1,4,5} {2} {3,6,7}
230 : {1,4,6,7} {2,5} {3}
231 : {1,4,6} {2,5,7} {3}
232 : {1,4,6} {2,5} {3,7}
233 : {1,4,7} {2,5,6} {3}
234 : {1,4} {2,5,6,7} {3}
235 : {1,4} {2,5,6} {3,7}
236 : {1,4,7} {2,5} {3,6}
237 : {1,4} {2,5,7} {3,6}
238 : {1,4} {2,5} {3,6,7}
239 : {1,4,6,7} {2} {3,5}
240 : {1,4,6} {2,7} {3,5}
241 : {1,4,6} {2} {3,5,7}
242 : {1,4,7} {2,6} {3,5}
243 : {1,4} {2,6,7} {3,5}
244 : {1,4} {2,6} {3,5,7}
245 : {1,4,7} {2} {3,5,6}
246 : {1,4} {2,7} {3,5,6}
247 : {1,4} {2} {3,5,6,7}
248 : {1,5,6,7} {2,4} {3}
249 : {1,5,6} {2,4,7} {3}
250 : {1,5,6} {2,4} {3,7}
251 : {1,5,7} {2,4,6} {3}
252 : {1,5} {2,4,6,7} {3}
253 : {1,5} {2,4,6} {3,7}
254 : {1,5,7} {2,4} {3,6}
255 : {1,5} {2,4,7} {3,6}
256 : {1,5} {2,4} {3,6,7}
257 : {1,6,7} {2,4,5} {3}
258 : {1,6} {2,4,5,7} {3}
259 : {1,6} {2,4,5} {3,7}
260 : {1,7} {2,4,5,6} {3}
261 : {1} {2,4,5,6,7} {3}
262 : {1} {2,4,5,6} {3,7}
263 : {1,7} {2,4,5} {3,6}
264 : {1} {2,4,5,7} {3,6}
265 : {1} {2,4,5} {3,6,7}
266 : {1,6,7} {2,4} {3,5}
267 : {1,6} {2,4,7} {3,5}
268 : {1,6} {2,4} {3,5,7}
269 : {1,7} {2,4,6} {3,5}
270 : {1} {2,4,6,7} {3,5}
271 : {1} {2,4,6} {3,5,7}
272 : {1,7} {2,4} {3,5,6}
273 : {1} {2,4,7} {3,5,6}
274 : {1} {2,4} {3,5,6,7}
275 : {1,5,6,7} {2} {3,4}
276 : {1,5,6} {2,7} {3,4}
277 : {1,5,6} {2} {3,4,7}
278 : {1,5,7} {2,6} {3,4}
279 : {1,5} {2,6,7} {3,4}
280 : {1,5} {2,6} {3,4,7}
281 : {1,5,7} {2} {3,4,6}
282 : {1,5} {2,7} {3,4,6}
283 : {1,5} {2} {3,4,6,7}
284 : {1,6,7} {2,5} {3,4}
285 : {1,6} {2,5,7} {3,4}
286 : {1,6} {2,5} {3,4,7}
287 : {1,7} {2,5,6} {3,4}
288 : {1} {2,5,6,7} {3,4}
289 : {1} {2,5,6} {3,4,7}
290 : {1,7} {2,5} {3,4,6}
291 : {1} {2,5,7} {3,4,6}
292 : {1} {2,5} {3,4,6,7}
293 : {1,6,7} {2} {3,4,5}
294 : {1,6} {2,7} {3,4,5}
295 : {1,6} {2} {3,4,5,7}
296 : {1,7} {2,6} {3,4,5}
297 : {1} {2,6,7} {3,4,5}
298 : {1} {2,6} {3,4,5,7}
299 : {1,7} {2} {3,4,5,6}
300 : {1} {2,7} {3,4,5,6}
301 : {1} {2} {3,4,5,6,7}
|
|