fork download
  1. def solve():
  2. import sys
  3. input = sys.stdin.read
  4. data = input().split()
  5.  
  6. index = 0
  7. t = int(data[index])
  8. index += 1
  9. results = []
  10.  
  11. for _ in range(t):
  12. n = int(data[index])
  13. k = int(data[index + 1])
  14. index += 2
  15.  
  16. if n == 1:
  17. # Only one number, it must be k
  18. results.append(f"{k}")
  19. continue
  20.  
  21. # More than one number, aim to spread out k into n parts optimally
  22. answer = []
  23. while n > 1 and k > 0:
  24. # Consider the largest power of 2 less than or equal to k
  25. if k >= n: # We need at least 1 for each remaining position
  26. # Place the maximum power of 2 that doesn't exceed k - (n-1)
  27. max_power = 1
  28. while max_power <= (k - (n-1)):
  29. max_power <<= 1
  30. max_power >>= 1 # Last shift made max_power too big
  31.  
  32. if max_power == 0:
  33. max_power = 1
  34.  
  35. # Use this power and reduce k
  36. answer.append(max_power)
  37. k -= max_power
  38. else:
  39. # We cannot place a power of 2 that big, we place 1 instead
  40. answer.append(1)
  41. k -= 1
  42. n -= 1
  43.  
  44. # Finally add whatever remains of k into the last position if n is 1
  45. answer.append(k)
  46.  
  47. results.append(" ".join(map(str, answer)))
  48.  
  49. sys.stdout.write("\n".join(results) + "\n")
  50.  
  51.  
  52. solve()
Success #stdin #stdout 0.03s 9900KB
stdin
4
1 5
2 3
2 5
6 51
stdout
5
2 1
4 1
32 8 8 1 1 1