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 :

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: