2

How can i maximize the function of the type (ax + b) % c where a, b, c are constants and are given while x is a integer variable ?

I'm not getting any idea how to start solving such problems.

1 Answers1

2

You don't say which representatives you are using modulo $c$. If oyu are choosing the integers $[0,c-1]$, then the following works.

The largest residue we can generate is $ax+b = c-k$ for some integer $k>0$. We have $ax = c-b-k$, so for $x$ to be an integer, $a$ divides $c-b-k$. So let $k$ be the smallest integer such that $a$ divides $c-b-k$. Then $c-k$ is the maximum attained value.

To get a formula, we want $c-b-k \cong 0 \pmod{a}$, so $k \cong c-b \pmod{a}$. (We must take $k=a$ if we would have taken $k=0$.) Then, in your notation, c-((c-b)%a?(c-b)%a:a) (or if(0 != (c-b)%a) ) {max = c-((c-b)%a);} else {max=c-a;}, if you don't recognize the ternary operator) is the maximum value attained. (Note that some languages do dumb things when % has negative argument(s), so you may need to make adjustments to correct for language or library deficiencies.)

Eric Towers
  • 70,953