Problem D. Permutation

HDU - 6429