-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC.py
39 lines (35 loc) · 897 Bytes
/
C.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
t=int(input())
while t:
x=int(input())
dp=[0]*(x+1)
if x>9 and x<=45:
for i in range(1,10):
dp[i]=i
for i in range(10,x+1):
mi=float('inf')
l=float('inf')
j=1
while j<=i-j:
st=str(dp[j])+str(dp[i-j])
if len(st)<=l:
l=len(st)
s=set(st)
if len(st)==len(s):
li=list(st)
li.sort()
a=int(''.join(li))
if a<mi:
mi=a
j+=1
if mi!=float('inf'):
dp[i]=mi
else:
dp[i]=-1
break
print(dp[-1])
else:
if x>45:
print(-1)
else:
print(x)
t-=1