fork download
  1. # list all solutions of non-negative x where
  2. # a dot-product x = ba
  3. # *a must be in descending order
  4. def frobenius_solve(a, b):
  5. n = len(a)
  6. h = {}
  7. def f(i, c):
  8. if c == 0:
  9. return [[]]
  10. if i == len(a):
  11. return []
  12. k = (i, c)
  13. if k not in h:
  14. d = a[i]
  15. r = []
  16. for j in range(c // d + 1):
  17. for x in f(i+1, c - d*j):
  18. y = [j]
  19. y.extend(x)
  20. r.append(y)
  21. h[k] = r
  22. return h[k]
  23. r = f(0, b)
  24. for x in r:
  25. while len(x) < n:
  26. x.append(0)
  27. return r
  28.  
  29. A = [12, 16, 20, 27]
  30. B = 123
  31. for x in frobenius_solve(A[::-1], B):
  32. print(' '.join(map(str, x[::-1])))
Success #stdin #stdout 0.02s 9992KB
stdin
Standard input is empty
stdout
8 0 0 1
4 3 0 1
0 6 0 1
5 1 1 1
1 4 1 1
2 2 2 1
3 0 3 1
0 1 4 1