Back to the Problem
Divide both parts by gcd(A, B) (Euclid's algorithm).
Complexity: O(log min(A, B))