Problem in solving a problem on SPOJ

How to nail this questionthis question