Given a sequence 0,1,1,2,2,3,3,...,n,n, find the number of ways to arrange the elements in the sequence so that there's exactly one digit between the two 1's, two digits between the two 2's, three digits between the two 3's, so on and so forth, and there are exactly n digits between the two n's.
For n=2, a possible solution would be 12102. For n=5, a possible solution would be 53141352402.
I have calculated that when n=2, the answer is 2; when n=3, the answer is 6; when n=4, the answer is 10; and when n=5, the answer is 22. But when n=6, my raw permutation function looks over 13! values and takes over an hour to calculate on my computer. I cannot think of any combinatorial enumeration or dynamic programming methods for this either. The best I can manage at the moment is backtracking with symmetry.
So my question is, what is the fastest way to compute the answer to this question for a large n and what are the mathematical intuitions for this problem? For example, is there a nontrivial upper / lower bound for the computational complexity for this problem?
Thanks for the answers in advance.
I first came across this problem in a Chinese coder forum.
An observation (too long for comment) that may speed up your recursive backtracking (though probably not by an order of magnitude).
In an acceptable permutation, the first occurrence of $k$ in position $p$ forces $k$ in position $k+p+1$ and precludes continuing with $k(k-1)$ or $kx(k-2)$ or $kxx(k-3)$ and so on.
Since half the entries are forced by the first occurrences of each $k$ you should have to examine something like $(n/2)!$ permutations, not all $n!$.