User:Slaunger/spoj

This is not a homework problem, but it is a subproblem in one of the online problems in SPOJ (which I do for fun and learning). Thus, I am not asking for a solution, but asking for some hints and directions to look for, which enables me to solve the problem myself.

The subproblem is a simple cryptanalysis problem: Given a prime number p and an integer


 * $$0 \le q < p$$,

find the integer


 * $$0 \le j \le \frac{p-1}{2}$$

such that q is the remainder of


 * $$\frac{j^2}{p}$$

The most simpleminded manner to solve this would be by a brute force method, where I would simply iterate through all possible values of j, compute the remainder r of $$\frac{j^2}{p}$$, and stop when r=q. However, not only is this a stupid and boring approach, but p also has a value of a few billions, and in the actual problem I need to find j for fixed p for thousands of different values of q within a few seconds of computational time.

So, it is very simple to find r(j; p), the problem is finding the reverse mapping j(r; p).

Apparently, for uniquely resolving j this implies that the mapping between j and r is a one-to-one function.

Actually, the limit I have stated for j is not stated in the actual problem, but since


 * $$j^2 \quad(\operatorname{mod}\ p) = (p-j)^2 \quad(\operatorname{mod}\ p)$$

I can see that j and p-j gives the same remainder. Thus, j should only extend up to (p-1)/2 (p is odd) as after that the mapping is mirrored and no longer one-to-one.

To further approach the problem, not being very mathematically knowledgeable about modular artithmetics, I have tried to start with small primes p = 3, 5, 7, 11, 13,... and simply write down the r(j) for the different possible js to try and find a pattern or some systematics in how to go the other way and find generalizations valid for much larger ps. For instance for p = 11, I do find that the mapping is one-to-one, as no two js 0,...,5 give the same remainder

Simple p=11 example j    r   floor(j*j/p) 0    0           0 1     1           0 2     4           0 3     9           0 4     5           1 5     3           2

For all the primes I have tried the mapping is one-to-one, although I have not yet understood why that is so? Nor have I managed to find a pattern.

I would appreciate a hint.