-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathk-factorization.py
More file actions
36 lines (26 loc) · 819 Bytes
/
k-factorization.py
File metadata and controls
36 lines (26 loc) · 819 Bytes
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
#!/bin/python3
import sys
allres = []
def k_factorization(n, arr, cur, result):
#print("n = {} cur = {} arr = {} res = {}".format(n, cur, arr, result))
if cur == n:
allres.append(list(result))
return True
elif cur > n:
return False
for ind, el in enumerate(arr):
result.append(cur*el)
k_factorization(n, arr[ind:], cur*el, result)
result.pop()
return False
if __name__ == "__main__":
n, k = input().strip().split(' ')
n, k = [int(n), int(k)]
arr = sorted(list(map(int, input().strip().split(' '))))
result = [1]
k_factorization(n, arr, 1, result)
#print("final: {}".format(allres))
if not allres:
print(-1)
else:
print(" ".join(list(map(str, min(allres, key = len)))))