1

We are given the following integers $a\geq1,b\geq1$ and $c\geq1$.

I am standing on a real line at point $b$. I always can move right by $b$ points or move left by $a$ points. I want to find out if I can reach point $c$.


So we need to find if there exist integers $n1,n2$ such that $c-b=bn_1-an_2$.

This problem is from coding, but I couldn't find a necessary and sufficient condition to check whether we can reach the point $c$ from point $b$. I feel that we need to play with $\mathrm{mod}(b)$, $\mathrm{mod}(a)$, $\mathrm{mod}(b-a)$.

Bill Dubuque
  • 272,048
Lee
  • 1,910
  • 12
  • 19
  • 1
    You may check if $c$ is a multiple of the GCD of $a$ and $b$; if yes then we can. Otherwise, the RHS of $c-b = bn_1-an_2$ is a multiple of $\operatorname{GCD}(a,b)$, but the LHS is not. – peterwhy Aug 24 '22 at 11:25
  • By general theory in the dupe $,bx-ay = c-b$ is solvable $\iff bx\equiv c-b\pmod{!a},$ is solvable $\iff d=(a,b)\mid c-b\iff d\mid c,,$ and if so the general solution is obtained from any particular solution $,(x_0,y_0),$ as $,(x_0,y_0) + n(a/d,b/d),,$ so choosing $n$ large enough yields positive solutions. – Bill Dubuque Aug 24 '22 at 18:53

1 Answers1

4

What you need is Bezout's lemma, which says that $\{ ax+by : a,b \in \mathbb{Z} \}=\{ ad : a \in \mathbb{Z} \}$ where $d$ is the GCD of $x$ and $y$. Therefore in your problem we need to know whether the GCD of $a$ and $b$ divides $c-b$. This reduces to whether it divides $c$, since it obviously divides $b$.

To finish your problem you need to deal with the nonnegativity constraint, but this is not a big deal. Once you have a solution at all, add a large enough multiple of $(a/\mathrm{gcd}(a,b),b/\mathrm{gcd}(a,b))$ to it that you have a nonnegative solution. (Or just use a multiple of $(a,b)$, if you don't care about having the smallest possible solution.)

Ian
  • 101,645