Permutations of the numbers with only two differences 
The question is from Pui Ching Invitational Mathematics Competition (2019)
My attempt:
obvious solution (symmetric form is omitted)
$(0,1,2,3,...,n,0)$ [difference: $1 \& n$]
$(0,1,3,5,...,2m-1,2m,2m-2,...,0)$ [difference: $1 \& 2$]
$(0,2m-1,2m-3,...,1,2m,2m-2,...,0)$ [difference: $2 \& 2m-1$]
$(0,1,3,...,2m+1,2m,...,2,0)$[difference: $1 \& 2$]
$(0,m+1,1,m+2,2,...,2m,m,0)$ [difference: $m \& m+1$]
ex:
if $n = 6$
$(0,1,2,3,4,5,6,0)$ [difference: $1 \& 6$]
$(0,1,3,5,6,4,2,0)$ [difference: $1 \& 2$]
$(0,5,3,1,6,4,2,0)$ [difference: $2 \& 5$]
$(0,4,1,5,2,6,3,0)$ [difference: $3 \& 4$]
In addition to general solutions, I found that if $n = 12$, other permutations exist:
$(0,3,1,4,6,8,11,9,12,10,7,5,2,0)$ [difference: $2 \& 3$]
$(0,9,5,1,10,6,2,11,7,3,12,8,4,0)$ [difference: $4 \& 9$]
Is there an algorithm to construct all possible permutations, or a formula to calculate the ways for any $n$? Thank you very much.
 A: The answer is 52. Of the two unique values, you know that one value must be positive and one value must be negative. Let $a$ be the positive value and let $-b$ be the negative value, so $b$ is the absolute value of the negative value. By summing the differences, we have
$$a_1-a_0 + a_2-a_1 + ... + a_{53}-a_{52} = a_{53}-a_0 = 0.$$
Thus,
$$ak - bm = 0$$
where $k$ is the number of times the positive value occurs and $m$ is the number of times the negative value occurs. We also know that $k+m = 53$ so we can rewrite the above as
$$(a+b)k - 53b = 0.$$
This says that $a+b$ is a multiple of 53 ($k$ cannot be a multiple of 53, because we can't increase more than 52 times; going from $a_{52}$ to $a_{53}$ must be a decrease.) Moreover, $a+b = 53$, because if it were greater than $106$, then either $a$ or $b$ would be greater than 53, which is impossible. This tells us the following:
\begin{align*}
a +b &= 53 \\
b &= k.
\end{align*}
Every positive pair $(a,b)$ which sum to 53 works; the algorithm is, you increase by $a$ until you can't, then you decrease by $b$ until you can't, and repeat this. In fact, if you can increase by $a$, you cannot decrease by $b$, so this is the only thing you can do.
Ex. $a=10$ and $b=43$ would generate, $10, 20, 30, 40, 50, 7, ..., 47, 4, ..., 44, 1, ..., 51, 8,...,48,...,5,...,45,...,2,...,52,...,9,...,49,6,...,46,3,...,43,0.$
This always works because subtracting $b$ is equal to adding $a$ and taking the mod $53$, and since $53$ is prime, we can always go around in a circle by keep adding $a$ mod 53.
