help needed in Little Shino and Path Divisor problem hackerearth

i am getting wa please help .
question link

my answer code link

thanks in advance

Change line no. 55 to : if(w/j == j).

My modified solution : yFqT8a - Online C++0x Compiler & Debugging Tool - Ideone.com

Can you check my code for this problem, its working fine for all test cases except 3 cases out of total 162 cases, it is showing memory limit exceeded for them?
here is the link to the code:
https://onlinegdb.com/B15koyx38