Remainders Game

CodeForces - 688D