Given integers $N$ and $D$, find how many pairs of integers $(i, j)$ such that $1 \le i \le j \le N$ have the greatest common divisor exactly $D$.
I know it involves Mobius inversion somehow, but I can't seem to get the exact expression. The constraints on $N$ and $D$ are quite large (of the order of $10^8$), so it should be a direct formula.