Link: https://uva.onlinejudge.org/external/117/11774.pdf

I understand that the for n == m answer is 2. But I can't figure out the solution when n != m. I mean I basically do not understand the theory behind the solution (apart from trying out for small test cases). Any help is really appreciated.

Let's decrease all numbers by 1 as real programmers do :).

Now we can observe that if we had a number

xon some place then after one permutation we have . Now we just want to know what is the minimum power ofnwhich gives 1 modulonm- 1.A similar problem but harder: Problem B

If it is not too much of a trouble can you please elaborate a bit? I could not understand to be honest. :( :'(

Look at the first picture 9*3. We see that 3 becomes 1 (I calculate with 1 decreased from every number — actually it's 4 becomes 2). Now let's see that . The same could be observed for every other number except the one in the right bottom.

Now let's take 1. We want for some number of multiplications. That's what I meant saying about lowest power of

n.Observe grid when n=m. In this case answer is always 2. If n!=m if you look some cases you will see answer is n+m. Good luck :)

TooNewbie your concept is totally wrong.. :)