-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path074.py
executable file
·44 lines (37 loc) · 1.04 KB
/
074.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import itertools, math
factorial = map(math.factorial, range(10))
def digits(n):
return map(int, str(n))
def hash(n):
return int(''.join(sorted(str(n), reverse=True)))
def next(n):
return hash(sum(map(lambda x: factorial[x], digits(n))))
memo = {}
def length(n):
if n in memo:
if memo[n] == 0:
memo[n] = -1
return 0
return memo[n]
memo[n] = 0
k = next(n)
l = length(k) + 1
if memo[n] == -1:
while k != n:
memo[k] = l
k = next(k)
memo[n] = l
return memo[n]
def count(digits, n):
s = str(n)
zeros = s.count('0')
res = factorial[digits - zeros] * (digits - zeros) ** zeros
for i in '123456789':
res /= s.count(i) or 1
return int(res)
print sum(count(digits, n)
for digits in range(2, 7)
for n in map(lambda x: int(''.join(x)),
itertools.combinations_with_replacement('9876543210',
digits))
if length(n) == 60)