fork download
  1. def construct_sequence(n, k):
  2. binary_k = bin(k)[2:] # Binary representation of k
  3. sequence = []
  4.  
  5. # Construct the sequence
  6. for i, bit in enumerate(binary_k):
  7. if bit == '0':
  8. sequence.append(i)
  9. else:
  10. for _ in range(int(bit)):
  11. sequence.append(i)
  12.  
  13. # Add 0 to the sequence
  14. sequence.append(0)
  15.  
  16. return sequence
  17.  
  18. # Example usage
  19. n = 5
  20. k = 6
  21. sequence = construct_sequence(n, k)
  22. print(sequence)
  23.  
Success #stdin #stdout 0.03s 9780KB
stdin
Standard input is empty
stdout
[0, 1, 2, 0]