How to optimize my code for codeforces problem

In round 506, div3, I tried to solve D but I am getting TLE of test case 66. Funny thing is that for almost same value, my test case 64 is passing in 904ms. So why am I getting TLE of 2000+ms in test case 66 and how can I correct my code

LINK to my submission

unordered_map has worst case complexity of O(N). So, remove it to map instead. And second thing while using [] operator in map be careful, it creates an element if it doesn’t exist. so, in that case the number of elements in map increases leading to an increase in searching time. So, instead of that m.find() to find if that element is present and if yes then add it. hope it helps! Let me know if it won’t work.

unordered_map has worst case complexity of O(N). So, remove it to map instead. And second thing while using [] operator in map be careful, it creates an element if it doesn’t exist. so, in that case the number of elements in map increases leading to an increase in searching time. So, instead of that m.find() to find if that element is present and if yes then add it. hope it helps! Let me know if it won’t work.

maps are causing TLE on test case 32. :’(

replaced code with find and insert functions than subscripts. Still TLE.

the main issue is with modulo operator. The Accepted code : http://rextester.com/HHWGQ59229