fork download
  1. def get_color(x, y):
  2. while x > 1:
  3. # Calculate the size of the triangle at step x
  4. size = (1 << (x - 1)) - 1 # size = 2^(x-1) - 1
  5.  
  6. # Check if (x, y) is in the inverted triangle
  7. if y > size:
  8. return 0 # Blue
  9.  
  10. # Determine if we are in the left or right triangle
  11. if y == size:
  12. return 1 # Red (the point at the tip of the inverted triangle)
  13.  
  14. # Move to the previous triangle
  15. x -= 1
  16.  
  17. # Adjust y for the next layer
  18. if y > size // 2:
  19. y -= (size // 2 + 1) # Move to the right triangle
  20. # If y <= size // 2, we stay in the left triangle
  21.  
  22. return 1 # If we reach step 1, it is always red
  23.  
  24. import sys
  25.  
  26. # Read input
  27. input = sys.stdin.read
  28. data = input().splitlines()
  29. Q = int(data[0])
  30. results = []
  31.  
  32. for i in range(1, Q + 1):
  33. x, y = map(int, data[i].split())
  34. results.append(get_color(x, y))
  35.  
  36. # Print results
  37. print("\n".join(map(str, results)))
Success #stdin #stdout 0.04s 9852KB
stdin
6
1 1
5 3
8 2
8 6
5 4
6 4
stdout
1
1
1
1
1
1