SFXPAL recursive relation

https://www.codechef.com/SNCKEL19/problems/SFXPAL

can anyone tell how to solve this question??

what is step by step logic and recursive relation??