How to solve ICL1904

How to solve this Combinatorics Problem from international coding league?

Link to problem: CodeChef: Practical coding for everyone