https://ideone.com/jfyHLZ This is Your AC solution. The problem was due to over flow when you multiply e = e * x.f Instead if u check if e <= n / x.f it will work finely. I was also unable to find this during the contest.