use strict;
use warnings;
my @pattern = (
[1],
[1, 2],
[1, 3],
[1, 2, 3, 4],
[1, 5],
[1, 2, 3, 4, 5, 6],
[1, 3, 5, 7],
[1, 2, 4, 5, 7, 8],
);
my @mask = (0, 1, 2, 4, 8, 16, 32, 64, 128, 256);
sub permute {
my ($n, $v, $b, $s) = @_;
if ($n < 1){
$s->($v, $b);
}
foreach my $i (1..9){
($b & $mask[$i]) and next;
permute($n - 1, $v * 10 + $i, $b | $mask[$i], $s);
}
}
my $total = 0;
foreach my $i (1..8){
my %memo;
permute($i, 0, 0, sub{
my ($d, $db) = @_;
foreach my $dp (2..9){
(not ($db & $mask[$dp]) or ($d % $dp)) and next;
foreach my $np (@{$pattern[$dp]}){
my $n = $d / $dp * $np;
my $nb = $memo{$n} or next;
(($nb ^ $mask[$np]) == ($db ^ $mask[$dp])) or next;
# say "$n/$d";
$total++;
}
}
$memo{$d} = $db;
});
}
say $total;
dXNlIHN0cmljdDsKdXNlIHdhcm5pbmdzOwp1c2UgZmVhdHVyZSBxdyhzYXkpOwoKbXkgQHBhdHRlcm4gPSAoCgl1bmRlZiwKCXVuZGVmLAoJWzFdLAoJWzEsIDJdLAoJWzEsIDNdLAoJWzEsIDIsIDMsIDRdLAoJWzEsIDVdLAoJWzEsIDIsIDMsIDQsIDUsIDZdLAoJWzEsIDMsIDUsIDddLAoJWzEsIDIsIDQsIDUsIDcsIDhdLAopOwoKbXkgQG1hc2sgPSAoMCwgMSwgMiwgNCwgOCwgMTYsIDMyLCA2NCwgMTI4LCAyNTYpOwoKc3ViIHBlcm11dGUgewoJbXkgKCRuLCAkdiwgJGIsICRzKSA9IEBfOwoKCWlmICgkbiA8IDEpewoJCSRzLT4oJHYsICRiKTsKCQlyZXR1cm47Cgl9Cglmb3JlYWNoIG15ICRpICgxLi45KXsKCQkoJGIgJiAkbWFza1skaV0pIGFuZCBuZXh0OwoJCXBlcm11dGUoJG4gLSAxLCAgJHYgKiAxMCArICRpLCAkYiB8ICRtYXNrWyR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