blob: 83ed4c23f9508b7baeb12d38fcddd4b3a5d6e351 (
plain)
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
|
from sys import argv
from copy import deepcopy
import numpy as np
s = int(argv[1])
c = np.array([int(x) for x in argv[2:]])
solutions = []
cnt = np.array([0 for _ in range(len(c))])
while (True):
val = np.dot(c, cnt)
if val >= s:
if val == s:
solutions.append(deepcopy(cnt))
# rotate "odometer"
cnt[-1] = 0
i = -2
cnt[i] += 1
while i >= -len(cnt) and np.dot(c, cnt) > s:
cnt[i] = 0
i -= 1
if i >= -len(cnt):
cnt[i] += 1
if i < -len(cnt):
break
else:
cnt[-1] += 1
print("There are", len(solutions), "ways to make sum", s)
for sol in solutions:
tmp = []
for i in range(len(c)):
tmp += [c[i] for _ in range (sol[i])]
print(tmp)
|