Dirichlet's Theorem on Arithmetic Progressions
Time limit1sMemory limit128 MB
Count how many terms a*n+b in a given range of n are prime, with the terms reaching up to 10^12 and at most 10^6 values per case.
- Level
Medium6 of 10
- Topics
- Number theory, Math, Implementation, Brute force
- Solved
- No attempts yet
Problem
Dirichlet's theorem on arithmetic progressions states that for two coprime positive integers and , the arithmetic progression () contains infinitely many primes.
A prime is a positive integer greater than whose only divisors are and itself.
For example, when and , the progression is:
Even just the beginning of this progression clearly contains many primes.
Given a positive integer , an integer , and , write a program that counts how many of the terms for are prime.
Input
The input consists of several test cases. Each test case is a single line containing four integers , , , and . It is guaranteed that and . The last line contains a single , which marks the end of the input.
Output
For each test case, print one line in the format Case x: c, where is the test case number starting from and is the number of terms that are prime for .