Chef and Friends Editorial WA

i was trying to solve this question https://www.codechef.com/SEPT16/problems/DIVMAC

and used same logic given in editorial https://discuss.codechef.com/questions/84915/chfnfrn-editorial?sort=votes&page=2

still getting WA can anyone help to check my code https://ideone.com/sRJFio