Comments

I forgot that I registered for this contest, what a pity...

However, I've found a solution for B without the need to consider the parity of n.

Implementation