I tried this problem on Hackerearth :
The editorial is not provided, just the code is there. I cannot understand how the author did it.
Please try this problem and please explain the approach. Can this also be done using combinatorics?
Edit 1: I spent my whole day waiting for the solution from CF community. I don't know why anyone doesn't want to help a struggling coder. :(
Edit 2: I would request people that if they cannot help then atleast don't downvote someone's blog.
Edit 3: I was wrong about the CF community. You all are kind and helpful people. :D