https://www.codechef.com/problems/MARBLES

can any one help with the logic for the following problem