use strict; my @p = qw(1 2 5 10 20 50 100 200); my $s = 0; my @a = (); my @cache; my $lc = ptn(0, scalar(@p)-1, @a); print $lc,"\n"; sub ptn { my ($k, $c, @a) = @_; my $lc = 0; if (!defined($cache[$c])) { $cache[$c] = {}; } if (exists $cache[$c]->{$k}) { return $cache[$c]->{$k}; } if ($c == 0) { for (my $i = 0; $k + $i * $p[$c] <= 200; ++$i) { if ($k + $i * $p[$c] == 200) { $a[$c] = $i; print join(",", @a),"\n"; ++$lc; } } $cache[$c]->{$k} = $lc; } else { for (my $i = 0; $k + $i * $p[$c] <= 200; ++$i) { $a[$c] = $i; $lc += ptn($k + $i * $p[$c], $c - 1, @a); } $cache[$c]->{$k} = $lc; } return $lc; }