Argh, i'm damn slow.. i was sure the answer was in the
Tartaglia's triangle, but no way.
here my, plain as usual, solution:
use strict;
use warnings;
my @posts = (1..$ARGV[0] || 5);
my @pigeons = (1..$ARGV[1] || 3);
print +('1 ' x @pigeons)."\n" if @posts == @pigeons;
die sprintf "%s is not enought for %s pigeons!",scalar @posts,scalar
+@pigeons
if @pigeons > @posts;
for (@pigeons) {
my $max = @posts - @pigeons + 1;
while ($max > 1){
my @distr = (0) x ($#pigeons+1);
$distr[0] = $max;
my $remain = @posts - $max;
my $i=0;
while ($remain > 0){
$i == $#distr ? $i=1 : $i++;
$distr[$i]++;
$remain--;
}
$max--;
print +(map{ "$distr[$_-1] "} @pigeons),"\n";
}
@pigeons = (pop @pigeons,@pigeons);
}
__DATA__
perl BUk-pigeons.pl 5 3
3 1 1
2 2 1
1 3 1
1 2 2
1 1 3
2 1 2
thanks for the amusement!
At the end of writing i was surpised i needed not an hash to get uniques results..
L*
UPDATE: sadly my solutions gives too few combinations..
perl BUk-pigeons.pl 12 7 | wc -l give me only 35 combinations..
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.