Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- n = int(input())
- dp = [float('inf')] * (n+1)
- dp[1] = 0
- for i in range(1, n):
- for op in range(1, 4):
- if op == 1 and i + 1 <= n:
- dp[i+1] = min(dp[i+1], dp[i] + 1)
- elif op == 2 and i * 2 <= n:
- dp[i*2] = min(dp[i*2], dp[i] + 1)
- elif op == 3 and i * 3 <= n:
- dp[i*3] = min(dp[i*3], dp[i] + 1)
- res = []
- pos = n
- while pos != 1:
- if dp[pos-1] == dp[pos] - 1:
- res.append('1')
- pos -= 1
- elif pos % 2 == 0 and dp[pos//2] == dp[pos] - 1:
- res.append('2')
- pos //= 2
- elif pos % 3 == 0 and dp[pos//3] == dp[pos] - 1:
- res.append('3')
- pos //= 3
- print(''.join(res[::-1]))
Advertisement
Add Comment
Please, Sign In to add comment