Miff's answer is definitely elegant. Since I had mine nearly finished anyway I provide it nevertheless. The good thing is that I get the same result for n=500 :-)
Let d be the number of different characters that are allowed, d=4 in your case.
Let n be the length of the string, ultimately you will be looking at even values of n.
Let u be the number of unpaired characters in a string.
Let N(n,d,u) be the number of strings of length n, built from d different characters and having u unpaired characters. Lets try to compute N.
There are quite a few corner cases to observe:
u>d or u>n => N=0
u<0 => N=0
n%2 != u%2 => N=0.
When stepping from n to n+1, u must either increase by 1 or decrease by 1, so we have a recursion according to
N(n,d,u) = f(N(n-1,d,u-1), N(n-1,d,u+1))
How many ways are there to reduce u by one. This one is easy, because we have to pair one of the u unpaired characters, which makes it just u. So the 2nd part of f will read (u+1)*N(n-1,d,u+1), with the caveat of course that we must observe that N=0 if u+1>n-1 or u+1>d.
Once we have understood this, it is easy to see what the first part of f is: in how many ways can we increase u when there are u-1 unpaired characters. Well, we have to pick one of the (k-(u-1)) characters which are paired.
So taking into account all corner cases, the recursive formula for N is
N(n,d,u) = (d-(u-1))*N(n-1,d,u-1) + (u+1)*N(n-1,d,u+1)
I am not going to read up in http://en.wikipedia.org/wiki/Concrete_Mathematics how to solve the recursion.
Instead I wrote some Java code. Again a little bit more clumsy, as is Java anyway due to its verbosity. But I had the motivation not to use recursion, since it breaks far to early, at least in Java, when the stack overflows at 500 or 1000 nesting levels.
The result for n=500, d=4 and u=0 is:
N(500, 4, 0) = 1339385758982834151185531311325002263201756014631917009304687985462938813906170153116497973519619822659493341146941433531483931607115392554498072196838958545795769042788035468026048125208904713757765805163872455056995809556627183222337328039422584942896842901774597806462162357229520744881314972303360
computed in 0.2 seconds, due to memorizing intermediate results. N(40000,4,0) computes in less than 5 seconds. Code also here: http://ideone.com/KvB5Jv
import java.math.BigInteger;
public class EvenPairedString2 {
private final int nChars; // d above, number of different chars to use
private int count = 0;
private Map<Task,BigInteger> memo = new HashMap<>();
public EvenPairedString2(int nChars) {
this.nChars = nChars;
}
/*+******************************************************************/
// encodes for a fixed d the task to compute N(strlen,d,unpaired).
private static class Task {
public final int strlen;
public final int unpaired;
Task(int strlen, int unpaired) {
this.strlen = strlen;
this.unpaired = unpaired;
}
@Override
public int hashCode() {
return strlen*117 ^ unpaired;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Task)) {
return false;
}
Task t2 = (Task)other;
return strlen==t2.strlen && unpaired==t2.unpaired;
}
@Override
public String toString() {
return "("+strlen+","+unpaired+")";
}
}
/*+******************************************************************/
// return corner case or memorized result or null
private BigInteger getMemoed(Task t) {
if (t.strlen==0 || t.unpaired<0 || t.unpaired>t.strlen || t.unpaired>nChars
|| t.strlen%2 != t.unpaired%2) {
return BigInteger.valueOf(0);
}
if (t.strlen==1) {
return BigInteger.valueOf(nChars);
}
return memo.get(t);
}
public int getCount() {
return count;
}
public BigInteger computeNDeep(Task t) {
List<Task> stack = new ArrayList<Task>();
BigInteger result = null;
stack.add(t);
while (stack.size()>0) {
count += 1;
t = stack.remove(stack.size()-1);
result = getMemoed(t);
if (result!=null) {
continue;
}
Task t1 = new Task(t.strlen-1, t.unpaired+1);
BigInteger r1 = getMemoed(t1);
Task t2 = new Task(t.strlen-1, t.unpaired-1);
BigInteger r2 = getMemoed(t2);
if (r1==null) {
stack.add(t);
stack.add(t1);
if (r2==null) {
stack.add(t2);
}
continue;
}
if (r2==null) {
stack.add(t);
stack.add(t2);
continue;
}
result = compute(t1.unpaired, r1, nChars-t2.unpaired, r2);
memo.put(t, result);
}
return result;
}
private BigInteger compute(int u1, BigInteger r1, int u2, BigInteger r2) {
r1 = r1.multiply(BigInteger.valueOf(u1));
r2 = r2.multiply(BigInteger.valueOf(u2));
return r1.add(r2);
}
public static void main(String[] argv) {
int strlen = Integer.parseInt(argv[0]);
int nChars = Integer.parseInt(argv[1]);
EvenPairedString2 eps = new EvenPairedString2(nChars);
BigInteger result = eps.computeNDeep(new Task(strlen, 0));
System.out.printf("%d: N(%d, %d, 0) = %d%n",
eps.getCount(), strlen, nChars,
result);
}
}