TITLE: Finding when $(a-n)(b-n)|(ab-n)$
QUESTION [16 upvotes]: Given $n$ and $k$, find the number of pairs of integers $(a, b)$ which satisfy the conditions $n < a < k, n < b < k$ and $(ab-n)$ is divisible by $(a-n)(b-n)$. Given: $0 ≤ n ≤ 100000, \ n < k ≤ 10^{18}$.
Link to problem: http://www.codechef.com/SEPT11/problems/SHORT

REPLY [2 votes]: As mentioned above, we can use the transformation $c = a-n$, $d=b-n$ to rewrite the problem as $$ cd | (c+n)(d+n) - n = cd + n(c+d+n-1). $$
Equivalently, $$ cd | n(c+d+n-1), \quad 1 \leq c,d < k-n. $$
In particular, this implies that $cd \leq n(c+d+n-1)$ and so $$ c(d-n) \leq n(d+n-1). $$
Suppose that $d \geq (1+\sqrt{2})n$. In that case we can divide by $d - n$ and deduce that
$$c \leq n \frac{d+n-1}{d-n} < n \frac{d+n}{d-n} = n \left(1 + \frac{2n}{d-n}\right) \leq (1+\sqrt{2})n. $$ So $$\min(c,d) \leq (1+\sqrt{2})n.$$
Fix some $c \leq (1+\sqrt{2})n$. For emphasis, we replace it with $C$. Rearranging, we have $$ Cd | nd + n(C+n-1) \triangleq nd + M. $$ This implies, in particular, that $d | M$. Hence it is enough to go over all factors of $M$. Since $M \approx n^2$, The average number of divisors is $2 \log n$, which is not too bad. Each of them can be tried separately.
It remains to efficiently find the factorization of all integers in the range $[n,(2+\sqrt{2})n]$. This can be done using a sieving algorithm (e.g. Eratosthenes) efficiently.