So we're trying to distribute 5 cards over 3 holes. Here're the holes:
|_|_|_|
Let's put the cards there. Well, they won't fit, because the '_' (underscore) occupies the whole cell, rather than just the bottom of it. Let's get rid of underscores. They're just for visials anyway.
| | | |
Now it fits. One way to do it:
1 1 |1|1|1|
or maybe like this:
1 1 |1|1|1|
First, the bottom row of ones is always the same. We don't actually need it to count combination. Lets just remove it (like in tetris). The number of ways to distribute 5 cards over 3 holes with BUK's constraint is the same as the number of ways to distribute 2 cards over 3 holes without this constraint.
1) |1|1| | 2) | |1|1| 1 3) | | |1| 4) and so on
That actually looks like a datastructure we can work with. The problem is it's two dimensional. Which is kind of a pain. But! we can flatten it. Like so:
1) | |1|1| 2) |1|1| | 3) |11| | | 4) | |11| | 5) and so on
Now there are two more things. The spaces in empty holes are not necessary. Also, the two '|' (pipes) at the left and right edges of our strings are not necessary either. That's also just visuals. Let's remove that stuff.
1) | | |11| => ||11 => 0 0 2 2) |1|1| | => 1|1| => 1 1 0 3) | |11| | => |11| => 0 2 0 4) etc
For no particular reason I used '0' instead of '|' in hacky solution :) (hm, I think that's because it reminded me of a recent thread). Like that:
1) 0011 => 0 0 2 2) 1010 => 1 1 0 3) 0110 => 0 2 0
Now we just want to count all combinations of such strings. The length of a string is $cards (which is the number of ones) + $holes - 1 (which is the number of zeros). That's our $n. And $r is obviously the number of cards. Think of it this way:
0000 - start with string of all zeros .00. - take some zeroes that way 1001 - and replace with ones 0..0 - take some zeroes that way 0110 - and replaces with ones 00.. - then that way 0011 - you got the idea
Hopefully now its clear that the number of ways we can produce such strings is n! / (r!(n-r)!), just like in a textbook.

And my hacky solution literally builds these strings and counts the length of sequences of ones :) Which is a bit of a nonsense, and GrandFather's method is a lot better, but hey! it works ;)


In reply to Re^3: Combinatorics problem. by Anonymous Monk
in thread Combinatorics problem. (Updated with more info.) by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.