help needed in Little Shino and Path Divisor problem hackerearth

i am getting wa please help .
question link


my answer code link
https://pastebin.com/Dhgaibam
thanks in advance

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

My modified solution : https://ideone.com/yFqT8a

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