Remainders Game

CodeForces - 687B