2022-01-09 14:40:34 +09:00

35 lines
697 B
Python

import numpy
def solution(n, b):
k = len(n)
memo = {}
index = 0
search = True
while search:
x, y, z = get_x_y_z(n, k, b)
if x in memo:
print(memo)
return index - memo[x]
memo[x] = index
n = z
index+=1
def get_x_y_z(n, k, b):
pre_x = ''.join(sorted(n, reverse=True))
x = int(pre_x, b)
y = int(pre_x[::-1], b)
z = numpy.base_repr(x-y, base=b).rjust(k, '0')
return x, y, z
tests = [
['1211', 10],
['210022', 3],
['01', 2],
['10', 2],
['111111111', 2],
['999999999', 10],
]
for test in tests:
print(test, solution(test[0], test[1]))