AcWing 1075. 数字转换(Python)
原题链接
中等
作者:
习学学
,
2021-03-22 15:33:50
,
所有人可见
,
阅读 353
Python 代码
N = 50010
h, e, ne = [-1] * N, [0] * (2*N), [0] * (2*N)
idx = 0
max_step = 0
def add(a, b):
global idx
e[idx] = b
h[a], ne[idx] = idx, h[a]
idx += 1
def div(x):
res = 1
for i in range(2, int(x**0.5) + 1):
if x % i == 0:
res += i
if x // i != i:
res += x // i
return res
def dfs(cur):
global max_step
nn = h[cur]
ret1, ret2 = 0, 0
while nn != -1:
son = e[nn]
tmp = dfs(son) + 1
if tmp >= ret1: ret1, ret2 = tmp, ret1
elif tmp > ret2: ret2 = tmp
nn = ne[nn]
max_step = max(max_step, ret1 + ret2)
return ret1
n = int(input())
for a in range(1, n+1):
b = div(a)
if b < a:
add(b, a)
dfs(1)