Help on approach of ICM2001

Hi, Can anyone please explain the approach of https://www.codechef.com/ICM2020/problems/ICM2001