24 lines
648 B
Python

def make_permutation_iter(num, l, s, result):
if not num:
result.append((s, l))
else:
for i in range(len(l)):
make_permutation_iter(num - 1, l[:i] + l[i + 1:], s + l[i], result)
def make_permutation(num, l):
result = []
make_permutation_iter(num, l, '', result)
return result
def add_type(a, b):
s = set()
for x, left in make_permutation(a, '123456789'):
for y, cmpr in make_permutation(b, left):
multi = int(x) * int(y)
if str(sorted(cmpr)) == str(sorted(str(multi))):
s.add(multi)
return sum(s)
print(add_type(1, 4) + add_type(2, 3))