dynamic programming

please help me to solve this spoj dp problem.

and in general what should be my approach to solve dp problems which ask to count no of ways of doing something etc.
thanks