Div II, 500 points / Div I, 250 points

Number theory question on primes.

I pretty quickly solved this one with an O(N*M) solution that was too slow to pass the largest input.  It passed all of the example cases, though: let this be a lesson to myself in the future that if I have a slow algorithm, I should manually enter a test on one of the largest possible inputs to make sure it's fast enough.