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

https://www.codechef.com/problems/UCL
Kindly write the solution to this problem.

I request you to try it on your own, before looking at my solution. My solution is NOT explanatory.
My solution

My solution using map: https://www.codechef.com/viewsolution/23658942