Bear and Prime Numbers

CodeForces - 385C