Golfscript - 56 50 49 48 41 40 38 37 chars
n%{~),{!}%\{0.@{.@+2$*@)@}/;;]}*)p;}/
Note: this handles multiple lines of input, is fast (1/8 secs to do the test cases), and doesn't break for any legal input.
(The first version was also my first ever Golfscript program; thanks to eBusiness for pointing out several tricks I missed).
In order to make this a useful educational post too, here's an explanation of how it works. We start with the recurrence f(n, k) = k * (f(n-1, k) + f(n-1, k-1))
. This can be understood combinatorically as saying that to place n
distinguishable balls in k
distinguishable buckets such that each bucket contains at least one ball, you pick one of the k
buckets for the first ball (k *
) and then either it will contain at least one more ball (f(n-1, k)
) or it won't (f(n-1, k-1)
).
The values resulting from this form a grid; taking n
as the row index and k
as the column index and indexing both from 0 it starts
1 0 0 0 0 0 0 ...
0 1 0 0 0 0 0 ...
0 1 2 0 0 0 0 ...
0 1 6 6 0 0 0 ...
0 1 14 36 24 0 0 ...
0 1 30 150 240 120 0 ...
0 1 62 540 1560 1800 720 ...
. . . . . . . .
. . . . . . . .
. . . . . . . .
So turning to the program,
n%{~ <<STUFF>> }/
splits the input into lines and then for each line evaluates it, putting n
and k
on the stack, and then calls <<STUFF>>
, which is as follows:
),{!}%\{0.@{.@+2$*@)@}/;;]}*)p;
This computes the first k+1
entries of the n+1
th row of that grid. Initially the stack is n k
.
),
gives stack of n [0 1 2 ... k]
{!}%
gives stack of n [1 0 0 ... 0]
where there are k
0s.
\{ <<MORE STUFF>> }*
brings the n
to the top and makes it the number of times we execute <<MORE STUFF>>
.
Our stack currently is a row of the table: [f(i,0) f(i,1) ... f(i,k)]
0.@
puts a couple of 0s before that array. The first one will be j
and the second one will be f(i,j-1)
.
{ <<FINAL LOOP>> }/
loops through the elements of the array; for each one it puts it on top of the stack and then executes the loop body.
.@+2$*@)@
is boring stack manipulation to take ... j f(i,j-1) f(i,j)
and yield ... j*(f(i,j-1)+f(i,j)) j+1 f(i,j)
;;]
pops off the left-over k+1 f(i,k)
and gathers everything into an array, ready for the next go round the loop.
Finally, when we've generated the n
th row of the table,
)p;
takes the last element, prints it, and discards the rest of the row.
For posterity, three 38-char solutions on this principle:
n%{~),{!}%\{0.@{.@+@.@*\)@}/;;]}*)p;}/
n%{~),{!}%\{0:x\{x\:x+1$*\)}/;]}*)p;}/
n%{~),{!}%\{0.@{@1$+2$*\@)}/;;]}*)p;}/
1Are there time limits? – None – 2011-03-28T13:56:52.080
@Tim Nordenfur:Updated :-) – Quixotic – 2011-03-28T13:59:00.643
That link is invalid for me. – mellamokb – 2011-03-28T14:54:14.120
@mellamokb:How about now? – Quixotic – 2011-03-28T15:13:44.340
@Debanjan: Yep. Thanks :) – mellamokb – 2011-03-28T15:14:07.677
I take it that the balls are unique? That is, placing ball x1 in bin y1 and ball x2 in bin y2 is different than x1 in y2 and x2 in y1? – Yonatan N – 2011-03-28T19:46:19.897
@Yonatan: I believe that is what is meant by "Every A and B can be distinguishable." That is the interpretation I used when developing my answer, which seems to at least agree with the examples given. – mellamokb – 2011-03-28T20:42:48.737
3@Debanjan I don't like the idea of pasting questions from SPOJ here. People submit their code for competing there and it would be unfair to them. – fR0DDY – 2011-03-29T05:58:50.757
@fR0DDY:It's my problem and you can see similar instances here and here.Anyways,just remove the redundant newline in your submission in SPOJ,to set a new record :-)
– Quixotic – 2011-03-29T07:34:19.473@Debanjan Even though it is your question, it is a challenge problem there. It is not a tutorial problem or from anagolf where answers are revealed. – fR0DDY – 2011-03-29T07:57:06.583
SIZECON is in challenge.I was a bit bored yesterday,so I wrote this problem in here ... and then thought of adding in SPOJ and other sites so that it might reach to larger audience.Anyways,I will move it to tutorial if SPOJ users wants that. – Quixotic – 2011-03-29T08:36:35.467
You might want to add the test cases
0 0
gives 1,1 0
gives 0,0 1
gives 0. – Peter Taylor – 2011-03-29T20:16:55.193@fR0DDY:Now it's a completely different problem with different test cases. – Quixotic – 2011-03-29T21:47:17.623
@Debanjan: Please add test cases for
0 0
,0 1
and1 0
. Also, is A > B always? – Eelvex – 2011-03-30T09:03:53.733@Eelvex:Added and No. – Quixotic – 2011-03-30T12:33:39.697
@Peter Taylor:How can we distribute 0 balls into 0 cells in 1 way? – Quixotic – 2011-03-30T12:35:06.453
@Debanjan: by doing nothing. Compare with
binomial(0, 0) = 1
, the number of ways of selecting 0 balls from 0 balls; orstirling2(0, 0) = 1
(the number of ways of distributing 0 distinguishable balls into 0 indistinguishable cells). – Peter Taylor – 2011-03-30T12:44:04.197@Peter Taylor:I am changing the test case to make it more general,however I would like to add that the The basis cases of Stirling numbers of the second kind are a bit messy: $S(n,1)=1$, $S(n,n)=1$, and $S(n,m)=0$ if $n<m$ or $n=m=0$. (REFERENCE)
– Quixotic – 2011-03-30T14:27:14.7331
@Debanjan, I see your reference and raise you Mathworld (eqn. 5 says that
– Peter Taylor – 2011-03-30T14:42:56.793S(n,0)
is1
ifn=0
and0
otherwise). If you want I can find a reference for the stronger statement that Stirling2 is in the associative subgroup of the exponential Riordan group.@Peter Taylor:I didn't knew about exponential Riordan group,thanks for the update :-) – Quixotic – 2011-03-30T15:24:48.617