You forgot f = 2*d + 1 - e. So the equations for f aren't technically correct.
Just did some naive checking against some numbers. I haven't looked into any permutations or patterns, just parity:
Assume x = odd, n = even:
(a = 7, b = 21)
f = even
d = even
n - 1 = odd
x = even, n = odd:
(a = 31, b = 85)
f = odd
d = odd
n - 1 = even