fork download
  1. def main():
  2. A, B = map( int, input().split())
  3. if A > B:
  4. A, B = B, A
  5. divisors = set()
  6. div = 1
  7. while div * div <= A:
  8. if A % div == 0:
  9. x = div
  10. y = A // div
  11. if B % x == 0:
  12. divisors.add(x)
  13. if B % y == 0:
  14. divisors.add(y)
  15. div = div + 1
  16. Q = int(input())
  17. divs = list(divisors)
  18. for i in range(0, Q):
  19. low, high = map( int, input().split())
  20. s, e, mid, sol = 0, len(divs) - 1, 0, -1
  21. while(s <= e):
  22. mid = s + (e - s) // 2
  23. if divs[mid] <= high:
  24. sol = mid
  25. s = mid + 1
  26. else:
  27. e = mid - 1
  28. if(divs[sol] < low):
  29. print(-1)
  30. else:
  31. print(divs[sol])
  32.  
  33. if __name__ == "__main__":
  34. main()
Success #stdin #stdout 0.02s 9232KB
stdin
9 27
3
1 5
10 11
9 11
stdout
3
-1
9