#!/usr/bin/perl
use 5.016;
use warnings;
sub rest { [ @{$_[0]}[ 1 .. $#{$_[0]} ] ] }
sub f {
my ($m, $n) = @_;
my ($t, $r) = sub {
my ($l, $s, $z, $r, $t) = @_;
$l->[0] or return __SUB__
->(rest
($l), $s, $z + 1, $r, $t);
my $x = $m * $l->[0] * 10 ** $z;
my $u = $t + $x * 10 ** $s;
rest($l), $s + $z + 1, 0, [@{$r}, [$len - $s, $x]], $u
);
[$len, $m],
[$len, 'x' . ' ' x
($len - length($n) - 1) . $n], [$len, '-' x $len],
@{$r},
[$len, '-' x $len],
[$len, $t]
);
}
say f(1234, 567);
say f(1234, 1001);
say f(123, 10101);
IyEvdXNyL2Jpbi9wZXJsCnVzZSA1LjAxNjsKdXNlIHdhcm5pbmdzOwoKc3ViIHJlc3QgeyBbIEB7JF9bMF19WyAxIC4uICQjeyRfWzBdfSBdIF0gfQoKc3ViIGYgewoJbXkgKCRtLCAkbikgPSBAXzsKCglteSAkbGVuID0gbGVuZ3RoKCRtIC4gJG4pOwoKCW15ICgkdCwgJHIpID0gc3ViIHsKCQlteSAoJGwsICRzLCAkeiwgJHIsICR0KSA9IEBfOwoKCQlAeyRsfSBvciByZXR1cm4gKCR0LCAkcik7CgoJCSRsLT5bMF0gb3IgcmV0dXJuIF9fU1VCX18tPihyZXN0KCRsKSwgJHMsICR6ICsgMSwgJHIsICR0KTsKCgkJbXkgJHggPSAkbSAqICRsLT5bMF0gKiAxMCAqKiAkejsKCQlteSAkdSA9ICR0ICsgJHggKiAxMCAqKiAkczsKCQlyZXR1cm4gX19TVUJfXy0+KAoJCQlyZXN0KCRsKSwgJHMgKyAkeiArIDEsIDAsIFtAeyRyfSwgWyRsZW4gLSAkcywgJHhdXSwgJHUKCQkpOwoJfS0+KFsgcmV2ZXJzZSBzcGxpdCAvLywgJG4gXSwgMCwgMCwgW10sIDApOwoKCXJldHVybiBtYXB7ICcgJyB4ICgkXy0+WzBdIC0gbGVuZ3RoKCRfLT5bMV0pKSA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