Code golf: Distributing the balls (I)

12

1

Challenge

In this task you have compute the number of ways we can distribute A balls into B cells with with every cell having at-least one ball.

The inputs A and B are given in a single line separated by a blank,the inputs are terminated by EOF.

You may like to check your solutions here.

Input

0 0
1 0
12 4
6 3
18 17
20 19
15 13
18 9
20 20
17 14
9 2
14 13
18 11

Output

1
0
14676024
540
54420176498688000
23112569077678080000
28332944640000
38528927611574400
2432902008176640000
21785854970880000
510
566658892800
334942064711654400

Constraints

  • Every A and B can be distinguishable.
  • 0 <= A,B <= 20
  • You can use any language of your choice
  • Shortest solution wins!

Quixotic

Posted 2011-03-28T13:54:49.617

Reputation: 2 199

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 and 1 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; or stirling2(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.733

1

@Debanjan, I see your reference and raise you Mathworld (eqn. 5 says that S(n,0) is 1 if n=0 and 0 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 – 2011-03-30T14:42:56.793

@Peter Taylor:I didn't knew about exponential Riordan group,thanks for the update :-) – Quixotic – 2011-03-30T15:24:48.617

Answers

4

JavaScript (90 93)

function f(a,b){n=m=r=1;for(i=b;i>0;n*=-1){r+=n*m*Math.pow(i,a);m=m*i/(b-i--+1)}return--r}

http://jsfiddle.net/RDGUn/2/

Obviously, any math-based language such as APL will beat me because of syntax verbosity and lack of built-in mathematical constructs :)

Edit Also, I don't have any input-related functionality except parameters passed into the function, not sure how to use standard input with JavaScript...

Edit: Move i-- into m=m* expression; move n*=-1 into for; start r=1 to combine assignments and remove extraneous one on return. (save 3 chars)

mellamokb

Posted 2011-03-28T13:54:49.617

Reputation: 5 544

You could use the spidermonkey shell - it at least has readline and print. I don't know what others here use.

– Jesse Millikan – 2011-03-28T22:05:45.247

@Jesse: Interesting. I'm going to lose anyway lol. – mellamokb – 2011-03-29T03:11:17.220

prompt and alert are JavaScript's "standard" io, as they're the typical blocking io calls, despite the fact that you'd never typically use blocking io with JavaScript. – zzzzBov – 2011-03-30T18:10:57.553

4

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+1th 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 nth 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;}/

Peter Taylor

Posted 2011-03-28T13:54:49.617

Reputation: 41 901

1Pretty good for a beginner, there is a few possible small scale reductions, immediately I find [0] -> 1,, the space after zip can just be removed, and the other space can be removed if you just store in an operator instead of k. I haven't stepped through your code yet, but I suspect you might get away with just using a value without putting it in an array in some of the spots. – aaaaaaaaaaaa – 2011-03-30T01:38:22.540

1+1,I don't understand Golfscript but this looks sufficiently fast,and yet very short. – Quixotic – 2011-03-30T15:23:50.597

@eBusiness and @Peter Taylor:On a different note .. how much you guys rate Golfscript on the scale of easy to learn? – Quixotic – 2011-03-30T15:26:44.557

@Debanjan, depends on what you already know. It's a functional stack-based language. I've used a functional languages before (SML - plus I've written functional-style code in OO languages), and I've used stack-based languages before (Java bytecode assembled with Jasmin, PostScript), so the only real hurdle I face is learning what operators are available. If you only know languages from the Algol family (C, Java, etc) then you'll have three hurdles to jump at once. – Peter Taylor – 2011-03-30T16:00:38.100

@Debanjan - It's much easier than it looks, you can start writing code almost immediately, but of course it takes some time to learn all the little tricks. – aaaaaaaaaaaa – 2011-03-30T17:59:47.967

@Peter Taylor:Well I am am comfortable with C,C++ and Python only.\ – Quixotic – 2011-03-30T18:52:01.307

@eBusiness:Hm I that's almost true for every language I guess,but this one seems just dots and lines to me :/ – Quixotic – 2011-03-30T18:55:20.337

@Debanjan As I said, it's easier than it looks, but you won't get far without reading some documentation http://www.golfscript.com

– aaaaaaaaaaaa – 2011-03-30T19:03:31.843

@Debanjan: I had never done GolfScript before this problem. I took gnibbler's answer, and the documentation, and a command-line GolfScript executor for testing, and after about an hour-and-a-half I understood his code. There's not very many operators, they're just short and powerful. Like @eBusiness says, just start going through character by character with the documentation, and you'll begin to understand it. – mellamokb – 2011-03-31T13:13:24.100

@mellamokb and @eBusiness :Thanks,but I am an windows,I don't find the golfscript executor for the same?! – Quixotic – 2011-03-31T15:59:11.827

@Debanjan, I've been tempted for a while to post on meta asking whether anyone wants to port the Golfscript interpreter from Ruby to JavaScript so that those of us who can't install Ruby (e.g. on our work machines, so that we could golf over lunch) can still play around with it. – Peter Taylor – 2011-03-31T16:46:01.420

@Peter Taylor - GolfScript for a large part build on Ruby functionality, a JavaScript implementation would be considerably more complicated, and a lot slower. It would be cool to have, but if I were to spend my time on such a thing I'd probably be more inclined to make an interpreter for a language designed to provide a common ground for golfing with an absolute speed benchmark and a way of counting complexity rather than just characters. – aaaaaaaaaaaa – 2011-04-01T12:22:14.320

@Debanjan: I'm on windows as well. I installed Ruby to my C:\ drive, downloaded the golfscript.rb (http://www.golfscript.com/golfscript/golfscript.rb -> File, save as), and then you just run it from the command line: echo INPUT | ruby golfscript.rb myfile.gs from the directory where you installed Ruby. myfile.gs is your GolfScript code stored in a file (the name is arbitrary).

– mellamokb – 2011-04-01T18:28:58.977

@eBusiness, you've got me thinking now about how creating a new language would allow fixing several problems in Golfscript. Alas, I don't have time to do more than muse on ideas at the moment. – Peter Taylor – 2011-04-01T23:04:23.453

@Peter Taylor - Completely unrelated, you got me thinking about how to get my solution below 37 characters :-P – aaaaaaaaaaaa – 2011-04-01T23:25:31.540

3

GolfScript - 45 38 36 characters

Medium-force dirty implementation recurrence relation (38 36 characters):

n%{~{.2$*{\(.2$f\2$(f+*}{=}if}:f~p}/

The recurrence relation I stole from Peter Taylors solution, it goes like this:

f(x, y) = y * ( f(x-1, y) + f(x-1, y-1) )

With special cases if either variable is 0.

My implementation does not reuse previous results, so each function call branch to two new calls, unless one of the zero cases have been reached. This give a worst case of 2^21-1 function calls which takes 30 seconds on my machine.

Light-force series solution (45 characters):

n%{~.),0\{.4$?3$,@[>.,,]{1\{)*}/}//*\-}/p;;}/

aaaaaaaaaaaa

Posted 2011-03-28T13:54:49.617

Reputation: 4 365

3

J, 40

4 :'|-/x(^~*y!~])i.1x+y'/&.".;._2(1!:1)3 

E.g

4 :'-/((x^~|.@:>:)*y&(!~))i.y'/x:".>{.;:(1!:1)3
15 13
28332944640000

<1sec for all test cases.

Edits

  • (52 → 47) Reduce with -/ instead of alternating (1 _1)* (J-B's idea)
  • (47 → 53) Noticed multiline input requirement :-/
  • (53 → 48) Exploit symmetry of binomials.
  • (48 → 48) Make tacit!
  • (48 → 41)
  • (41 → 40) Squeeze increment+conversion into 1x+

Eelvex

Posted 2011-03-28T13:54:49.617

Reputation: 5 204

1Hey! That was my idea! O:-) – J B – 2011-03-30T11:03:16.240

Ok, I'll steal that 1x+ then, but that only buys me back 1 character, whereas you took 5! – J B – 2011-03-30T15:20:40.007

3

J, 38 to 42

Depending on your strictness preferences about interactive languages and output presentation, take your pick from the J spectre of solutions:

  • 38 shortest interactive: 4 :'|-/(!&y*^&x)i.1x+y'/&".;._2(1!:1)3
    Launch jconsole, enter it, then paste the input (end with C-d). You'll notice the output is space-separated (J is a vector language, it performs the computation on the whole input as a whole and returns it as a 1D vector, whose default presentation is on a single line). I consider that ok, the spirit of this problem is computation, not presentation. But if you insist on having newlines instead:
  • 39 longer interactive: 4 :'|-/(!&y*^&x)i.1x+y'/&.".;._2(1!:1)3
    Replacing Compose (&) with Under (&.) returns a vector of strings, whose presentation ends up on separate lines.
  • 42 batch mode: 4 :'echo|-/(!&y*^&x)i.1x+y'/&".;._2(1!:1)3
    Run from the command line as $ jconsole balls.ijs < balls.in

If you voted this up, you might want to go give Eelvex's solution some credit as well.

J B

Posted 2011-03-28T13:54:49.617

Reputation: 9 638

You need an Under &. for it to work properly on interactive mode. – Eelvex – 2011-03-30T15:16:32.443

@Eelvex you must have a different interpretation of "properly". I start jconsole, paste code, paste input, C-d, and receive the output. No under needed. What's yours? – J B – 2011-03-30T15:19:59.633

Our codes combined: 4 :'|-/(!&y*^&x)i.1x+y'/&.".;._2(1!:1)3. 39 chars. – Eelvex – 2011-03-30T15:20:32.287

Without echo or Under I get the output in one line only (instead of multiple lines). – Eelvex – 2011-03-30T15:21:56.527

@Eelvex indeed, but that's not explicitely forbidden. – J B – 2011-03-30T15:24:35.247

3

Common Lisp (83)

(defun b (x y)
  (if (= (* x y) 0)
      (if (= (+ x y) 0) 1 0)
      (* y (+ (b (decf x) y) (b x (1- y)))))))

It seems like there should be a shorter way to test the base cases, but nothing occurs to me offhand.

Dr. Pain

Posted 2011-03-28T13:54:49.617

Reputation: 261

2

J, 55 characters

(wd@(4 :'(y^x)--/(!&y*^&x)|.i.y')/@".@,&'x');._2(1!:1)3
  • Passes current test cases. I think I understand the math...
  • j602, console only (wd). Input on stdin, output on stdout.

Bash test script:

jconsole disballs.ijs <<END
12 4
6 3
END

Jesse Millikan

Posted 2011-03-28T13:54:49.617

Reputation: 1 438

What does that j6xx's wd do? – J B – 2011-03-29T04:39:30.780

I really meant j602... I'm guessing it's in j601 also. It's defined as echo, which is defined as 0 0&$@(1!:2&2). I'm not sure what that means, but it does something like pretty-print rank 1 items with a line break. (I'm just now noticing that uses 2 rather than 4... I think it still goes to stdout in console mode, at least.) – Jesse Millikan – 2011-03-29T04:44:13.913

I'm having trouble running this code. Do I just type it into the console? – mellamokb – 2011-03-29T12:38:56.583

@mellamokb I've using something like the test script above, with the program saved as disballs.ijs and the correct path to j602/bin/jconsole. – Jesse Millikan – 2011-03-29T14:23:04.340

@Jesse: I'm running this on windows. I get << was unexpected at this time. Sorry I'm new to J input, I always used it in console mode. – mellamokb – 2011-03-29T15:31:37.310

@mellamokb: I've been running it under Windows in a Bash installed by msysgit. To run it otherwise, you'll have to modify the program to end with 2 toJ(1!:1)3 (add ' toJ' before (1!:1)3) and then run jconsole.exe disballs.ijs, and type in 12 4<ENTER>6 3<ENTER><CTRL-Z>. There's probably an easy way to do this in Windows batch with an input file, but you might find it easier to get a Unix shell on there for compatibility with most of the golfers here. – Jesse Millikan – 2011-03-29T16:12:47.180

2

Golfscript - 26 chars

Warning: The 12 4 case needs a lot of memory (although not as much as the answer below) and takes a quite a while to run

~:)\?:x,{x+)base(;.&,)=},,

Obviously this answer has some problems, but I will leave it here because the comments refer to it and mellamokb's answer is based off it.

Golfscript - 24 chars

Warning: The 12 4 case needs a lot of memory and takes a quite a while to run

~:o)\?,{o)base[0]-,o=},,

gnibbler

Posted 2011-03-28T13:54:49.617

Reputation: 14 170

2I can't figure how you came up with that code, not only will this method run out of memory for large inputs, I also can't figure what the increment operators are good for. That you actually hit the target for 6 3 seems to be nothing but luck. – aaaaaaaaaaaa – 2011-03-29T01:21:46.177

That's no clown, that's my wife! – Jesse Millikan – 2011-03-29T04:21:32.747

2I don't understand Golfscript but as you said and I agree your approach is too slow. – Quixotic – 2011-03-29T07:21:59.953

What? 2 upvotes for this solution. Didn't I say it clear enough, it is WRONG, even for the smallscale cases that it can handle the results are wrong. – aaaaaaaaaaaa – 2011-03-29T09:59:45.290

I ran for [4, 2] and the result is 24. My code returns 14 for [4, 2]. Who's right? – mellamokb – 2011-03-29T12:39:33.493

@mellamokb:For input [4,2] the correct answer is 14. – Quixotic – 2011-03-29T16:20:15.967

I think this is a working solution: ):£\.(£\?:¢;?,{¢+}%{£base£,\-[0]=},, (i used ¢ and £ for variables so they wouldn't get parsed with the base keyword). – mellamokb – 2011-03-29T16:58:23.287

@eBusiness: The answer is on the right track, just had a few bugs which I worked out with my answer (with considerably more chars, never used GolfScript before :). I think the idea for incrementing is so that the numbers use non-zero digits for representing what cell they are in, otherwise you have a problem with the base-k format starting with 0's, and they will be left out. If it starts with 1 instead, those left-most digits will be included. – mellamokb – 2011-03-29T17:13:24.507

@mellamokb your solution ain't any better, if you want a solution of approximately that principle then here is one: ~:B\:A?,{B base.,A=[]0if+B,&,B=},, – aaaaaaaaaaaa – 2011-03-29T17:58:17.027

@eBusiness: Except for the minor point that my solution works... lol. – mellamokb – 2011-03-29T18:15:44.987

Ah it does, (given one add the ~ that you forgot) but not so well in my UTF-8 encoded file. You should skip ASCII extension characters, just overload an operator if you need tight variable names. – aaaaaaaaaaaa – 2011-03-29T18:28:33.927

@eBusiness: Thanks, fixed on both counts. I haven't figured out why do you need ~? If you run the code with the input prepended as numbers to the beginning, there's no reason to eval them. Or how else do you give input to GolfScript code? The documentation is very slim. – mellamokb – 2011-03-29T18:33:04.597

You get input as stdin, which is a string, so you basically eval that string to get the numbers written in it. – aaaaaaaaaaaa – 2011-03-29T18:39:28.177

@eBusiness: Got it. What do you think of my new solution (I posted a new answer). It's slightly shorter than the one you proposed (by 2 chars I think). – mellamokb – 2011-03-29T18:59:27.630

It's still brute force, it can't handle the entire input spectrum. You ain't got a true solution until you go the mathematical way. – aaaaaaaaaaaa – 2011-03-29T19:13:30.563

3@mellamokb, good on you for working out how it was supposed to work :) It only took 2 extra chars to fix that bug. Now we are in the murky area where the shortest code may be correct but not practical. Code-golf is riddled with insanely inefficient answers but microseconds vs seconds usually doesn't matter. This one is an extreme case (lots of memory too). Debanjan has indicated that the answers need to be faster, but this site is not SPOJ, this question is tagged code-golf – gnibbler – 2011-03-29T22:59:41.887

@gnibbler: Suffers from the same problems as my current solution: 0 0 yields an error and n 1 for any n causes it to hang. – mellamokb – 2011-03-29T23:09:06.823

@mellamokb, I guess 0,0 should produce 0 and n,1 should produce 1? – gnibbler – 2011-03-29T23:19:25.903

@eBusiness: I tend to agree with gnibbler. Part of the golfing excitement (to me) is finding a clever way to reduce the size of the code at the expense of performance. – mellamokb – 2011-03-30T02:56:12.007

1@gnibbler, 0 0 should produce 1; 0 k for any other k should produce 0; n 1 for n > 0 should produce 1. – Peter Taylor – 2011-03-30T05:55:26.160

2

dc, 100 chars

[0q]s5[1q]s6[l2l3>5l3 0>5l2 0=6l2 1-S2l3dS3 1-S3l1xL3s9l1xL2s9+L3*]s1[?z0=5S3S2l1xL3L2+s9fs9l4x]ds4x

Alas, dc doesn't seem to be supported by ideone. There may be a character or two still to squeeze out, but it's bedtime.

Note: this supports multiple lines of input, has sufficient precision to give the correct output even for 20 19 (curse you, Perl, for the time I wasted debugging my solution!), and gives the correct output for 0 0.

Suggestions from Nabb allow shortening at least as far as

[0q]sZ[1q]sI[?z0=ZSkSn[lnlk>Zlk0>Zln0=Iln1-SnlkdSk1-SklFxLks9lFxLns9+Lk*]dsFxfs9l4x]ds4x

at the cost of leaving junk in the register stacks (and thus running out of memory if we compute billions of answers).

Peter Taylor

Posted 2011-03-28T13:54:49.617

Reputation: 41 901

Registers are always single characters (you can use any character, which will make the code more readable), so l11 is parsed as l1 1 (you can use K as a single character token for 0 if you aren't going to change precision anyway). You can change the input loop to ?[...?z1=4]. You can inline the macro in register 1. And you can probably save heaps more characters in general, but I'll wait for it to be shorter to comprehend it. – Nabb – 2011-03-31T12:17:48.593

@Nabb, ah, I didn't read the man page carefully enough. I'm only using 8 or 9 registers, so I didn't run into the consequences of my misunderstanding. Thanks. – Peter Taylor – 2011-03-31T12:21:57.810

2

Python 140 Chars

import sys
f=lambda n,k:(n and k and n>=k and k*(f(n-1,k-1)+f(n-1,k)))or(n+k==0 and 1)or 0
for l in sys.stdin:print f(*(map(int,l.split())))

fR0DDY

Posted 2011-03-28T13:54:49.617

Reputation: 4 337

1

Golfscript (28 31 37)

~):$\.($\?:@;?,{@+}%{$base$,\-[0]=},,

Modification to gnibbler's GolfScript solution. I think this is a working solution - tested with [3,2], [4,2], [6,3], and [9,2] with correct answers. (I used $ and @ for variables to tighten up space around the base keyword).

There are two problems with gnibbler's current solution.

  1. Checking length after removing [0] does not guarantee a solution, because [1,1,1,1] would be valid for input [4,2], even though all 4 balls are in the same cell (1). So I've modified to check also that all digits are used, i.e., the array contains 1-2, so each cell contains at least one ball.
  2. In the case of input [4,2], the base-3 format of numbers 0-27 are less than 4 digits, and the left-most 0's are not included. That means [1,1] is included as a valid solution, even though it is technically actually [0,0,1,1], which means the first two balls are not placed anywhere. I fix by adding 3^3 to every entry (generically k^n-1 to the array of k^n entries) so that the first entries are shifted upward to having at least n-digits in base-k format, and the last entries will automatically be invalid anyway and won't affect the solution (because the second digit will always be 0).

Edit

~:@\?:$,{$+}%{@base(;@,\-,0=},,

`~:@\?:$,{$+@base(;@,\-,0=},,`

Better solution yet! No need to increment, just add to all of the numbers so they start with [1], and no digits will be missing (including the left-padding of 0's) once you decon that first digit. This solution should work and has been tested with same entries above. It's also a lot faster because we aren't incrementing before taking exponent to generate the array (but still suffers from same performance / memory problem for larger input).

Edit: Use gnibbler's idea of moving the addition of $ inside of the filter instead of as an extra step. (save 3 chars).

mellamokb

Posted 2011-03-28T13:54:49.617

Reputation: 5 544

Breaks on input 0 0. – Peter Taylor – 2011-03-29T20:44:59.210

Also appears to handle only one line of input. – Peter Taylor – 2011-03-29T22:09:34.707

And breaks on n 1 for any n, causes it to hang. hmm.. – mellamokb – 2011-03-29T23:10:10.490

1converting numbers to base 1 will do that :) – gnibbler – 2011-03-29T23:26:11.857

@gnibbler: Do you have any suggestions? Will I just need to throw in some if statements at the beginning to catch those cases? Seems like I'll lose a lot of ground that way. – mellamokb – 2011-03-31T13:15:54.487

0

05AB1E, 19 bytes

#D`ULX.Œʒ€gßĀ}gs_P+

NOTE: It's extremely slow, and already times out for 12 4. It is working as intended, though. Will see if I can come up with an alternative method which works for all test cases in a reasonable time. See below for a much faster version which runs all test cases in less than a second.

Try it online or verify a few more (of the smaller) test cases.

Explanation:

#               # Split the (implicit) input-string by spaces
 D              # Duplicate it
  `             # Push both values to the stack
   U            # Pop and store the second value in variable `X`
    L           # Create a list in the range [1,n] for the first value
     X.Œ        # Create a list of all possible ways to divide this list into `X` partitions
                # (including empty sublists, so we'll have to filter them out:)
        ʒ       # Filter this list of lists of partition-lists by:
         €g     #  Get the length of each partition-list
           ß    #  Get the minimum length
            Ā   #  Truthify; 0 remains 0 (falsey); anything else becomes 1 (truthy)
             }g # After the filter, take the length to get the amount left
 s              # Swap so the duplicated input-list is at the top of the stack again
  _             # Check for each value if they're equal to 0 (1 if truthy; 0 if falsey)
   P            # Take the product of the two to check if both input-values are 0
    +           # And add it to the earlier calculated product (edge case for [0,0] = 1)
                # (After which the result is output implicitly)

05AB1E, 29 bytes

Here a much faster version which works for all test cases in about 0.5 seconds on TIO:

Î#R`V©LRvyYmX*NÈ·<*+Xy*®y->÷U

Port of @mellamokb's JavaScript answer, so make sure to upvote him!

Try it online or verify all test cases.

Explanation:

Î                    # Push (result=) 0 and the input
 #                   # Split the input by spaces
  R`                 # Push the values to the stack reversed
    V                # Pop and store the first value in variable `Y`
     ©               # Store the second value in variable `®` (without popping)
      LRv            # Loop `y` in the range [`®`,1], with index `N` in the range [0,`®`):
         yYm         #  Calculate `y` to the power `Y`
            X*       #  Multiply it by `X`
                     #  (Note: `X` is 1 before setting it to another value initially)
              NÈ     #  Check if index `N` is even (1 if truthy; 0 if falsey)
                ·<   #  Double it; and decrease it by 1 (1 if truthy; -1 if falseY0
                  *  #  Multiply it to the earlier number
                   + #  And add it to the result
         Xy*         #  Push `X` multiplied by `y`
         ®y->        #  Push `®` - `y` + 1
             ÷       #  Integer divide them
              U      #  Pop and store it as new variable `X`
                     # (output the result at the top of the stack implicitly after the loop)

NOTE: Works for edge case 0 0 in this case (unlike the JavaScript answer I ported this method from), because the L builtin will create a list [0,1].

Kevin Cruijssen

Posted 2011-03-28T13:54:49.617

Reputation: 67 575