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 ;)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.