What Has Dirichlet Got to Do with That?

CodeForces - 39E