## Sunday, 19 May 2013

### A number theory problem from online games Part 1

The situation comes from online game in a very natrual style.

Unlike modern banking system, the statistics (like money) in online games are usually integer, but that makes some problem (to the producer sometimes, and sometimes to players) in particular the statistic conversion.

Like, we are obtaining energy A from energy B at a rate of 36.5%. It is natrual that it takes 365 units of B for us to get 1000 units of A. For what if we want 1 units of A? 3 units?

In the game that I am playing they take the rounding off approach --- of course --- this is beneficial if we goes B->A but not-so-good when we go in another way. For example 1 unit of A is supposed to take 0.365 units of B and now it's rounded off to zero. 3 units of A takes 1 unit of B to convert with. Of course, the system won't let you to obtain 1 unit of A 'with zero unit of B' --- you need at least one unit of input.

The problem comes: in what ratio (in particular, the amount of A I want every time) so that we maximize our gain (i.e. ratio above the supposed rate)? We can now state our problem:

Instance: A real number $r \in (0,1)$
Prroblem: Find $a_0\in \mathbb{N}$ to maximize $f(a_0)=r-\frac{[ra_0]}{a_0}$.

Well, r causes a bit of trouble when it isn't rational, so we modify the instance a bit:

Instance: A sequence of real number $\left\{ r_n \right\} = \left\{\frac{p_n}{q_n}\right\}$ that converges to $r\in (0,1)$. It follows that the sequence is an approximation to $r$ that $p_n,q_n\in \mathbb{N}$ and $(p,q)=1$. Moreover, it follows that $|\frac{p_n}{q_n}-r|\leq |\frac{p}{q}-r|$ for all fractions that $q_n\leq q$. If $r\in \mathbb{Q}$, the sequence constantly equal to the fraction that equals to $r$.

We should also specify that we can't convert to something (larger than zero) using zero resources. So the problem becomes:

Problem: Find $a_0 \in \mathbb{N}$ to maximize $f(a_0)$ as defined above, with $ra_0 \geq 1$.

Where should we start with? It is clear that when $r \in \mathbb{Q}$ the problem can be easily solved. To see this we shall consider rational instance for the rest of (this part of) the passage with the notation $r=\frac{p}{q}$.

Lemma. For $r = \frac{p}{q} \in \mathbb{Q}$, $a_0\leq q-1$.

Proof. For $(p,q)=1$, there is a multiplicative inverse of p mod q. Let $a_0 = p^{-1}$ in $\mathbb{Z}_q$, it is clear that $f(a_0) = \frac{q-1}{a_0q}$.

For $a\geq q$, $f(a)=\frac{k}{aq}\leq \frac{q-1}{aq}\leq \frac{q-1}{a_0q}=f(a_0)$.

It's like that the above lemma gives us a possible answer to the problem, but is it the best answer? Let's divide it in two cases:

Theorem. If $pa_0=q-1$, then $2a_0$ is the optimal answer.

Proof. It is clear that $a_0$ can't be a valid answer, and $f(a)\leq f(2a_0)$ for all $a\geq 2a_0$ using similar arguement with the lemma. To see that $f(a)\leq f(2a_0)$ for $a_0+1\leq a\leq 2a_0$, notice that $a_0p = q-1$ so that the integer part of $\frac{ap}{q}$ do not increase in the given range, the result immediately follows.

Similarly, if $pa_0\geq q$ we check all $1\leq a \leq a_0$ to get the optimal answer. It follows that the optimal answer is at most $a_0$ using the similar arguement.

If we want to be faster, check all crossing and the corresponding $a$ value so that $pa$ is just under the multiple of q, now we should get the answer.

Why 'should'? Think about the situation:

Suppose we get $a_0$ as the 'optimal' answer using the faster way, is $a_0-1$ a better answer? Is this even possible?... We shall see it shortly...