I'm working on a problem for an online judge and I'm stuck. I'd like a nudge in the right direction (not an outright solution, please), relevant references, theorems, etc. After reading through relevant entries here and on the web, I believe the correct mathematical statement of the problem is to count the exact number of elements in a subset of a numerical semigroup $\lbrace a, b \rbrace$ (the set of all linear combinations of $a,b$ with non-negative integer coefficients). Specifically, to count the element of set $S$: \begin{equation} S = \lbrace x \vert x \in \lbrace a,b \rbrace, X_{min} \le x \le X_{max} \rbrace \end{equation} The range of possible values is: $1 \le a, b, X_{min}, X_{max} \le \approx 2^{63}$. With these sizes, only a closed form $O(1)$ solution will do. I already have a solution for the cases: $a=b$ (trivial) and $b = a + 1$ (not trivial but not too challenging, interesting case). From the answers to this question: Determine the Set of a Sum of Numbers, I suspect some sort of generalization of the gap counting formula for the case where $gcd(a,b)=1$: $gaps=(a-1)(b-1)/2$, to be able to quickly calculate the number of gaps below an arbitrary $x$ is the key for answering this question.
What I've noticed so far: I've investigated trying to find a pattern for the gaps between successive multiples of $a$ (assuming $a<b$). I've noticed that each multiple of $b$ occupies one of the $(a-1)$ positions between multiples of $a$, and that the $(a-1)$th multiple of $b$ fills that last gap. The first $b$ fills position $b_a$ and the next multiple of b occupies $(b_a + (b-a)_a)_a$. But I haven't been able to generalize anything from that yet.
(My math background is undergraduate level calculus (2 years), linear algebra/matrix theory, discrete math (for CS students), and a real analysis course, but no number theory.) Thanks for any hints / pointers!