Shridhar wants to generate some prime numbers for his cryptosystem. Help him!
Your task is to generate all prime numbers between two given numbers.
Input
The first line contains t, the number of test cases (less then or equal to 10).
Followed by t lines which contain two numbers m and n (1 ≤ m ≤ n ≤ 1000000000, n-m ≤ 100000) separated by a space.
Output
For every test case print all prime numbers p such that m ≤ p ≤ n,
one number per line. Separate the answers for each test case by an empty line.
Example
Input:
2
1 10
3 5
Output:
2
3
5
7
3
5
Warning: large Input/Output data, be careful with certain languages (though most should be OK if the algorithm is well designed)
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve the problem: given t test cases, each with m and n, where n up to 1e9 and n-m ≤ 100000, output all primes in [m,n] per test case. Each test case output separated by empty line.
We need to generate primes in a small segment of size up to 100000, but the numbers themselves can be large (up to 1e9). So we need a segmented sieve.
Standard approach: Pre-generate primes up to sqrt(1e9) ≈ 31623. Actually sqrt(1e9) = 31622.776..., so we need primes up to 31623. We can sieve up to 100000 (or 40000) but 31623 is fine.
Generate primes using simple sieve up to 31623.
Then for each test case, create a boolean array is_prime for the segment [m, n] initially true (for numbers from m to n). Then for each prime p <= sqrt(n), mark multiples of p within the segment as composite. Need to handle if m=1: 1 is not prime.
After marking, output numbers that remain true.
We need careful indexing: let low = m, high = n. Size = high - low + 1 <= 100001.
Create array seg = [True]size. For each prime p in primes up to sqrt(high):
Find the first multiple of p in [low, high]: start = max(pp, ((low + p - 1) // p) * p). But careful: If p >= low, p itself should not be marked composite; we need to keep p as prime if it is within segment. So we should start marking from max(pp, ceil(low/p)p). But if p is prime and p >= low, p itself will be <= ceil(low/p)p? Actually if low <= p, ceil(low/p) = 1, so start = p. That would mark p as composite, which is wrong. So we need to skip p itself when marking. So standard: start = max(pp, ((low + p - 1) // p) * p). If start == p (i.e., low <= p <= high and pp may be less than low), then start = 2p? Actually we need to ensure we don't mark p. So we could start from max(pp, ceil(low/p)p) and then if start == p, add p to skip p. So set start = max(pp, ceil(low/p)p). If start == p, then start += p. So after computing start, if start == p: start += p. But careful: pp might be > p, but if pp is less than low, then start may be ceil(low/p)p which could be > p. If ceil(low/p)p == p, then low <= p, and p is within [low,high]? That means low <= p <= high. Since p is prime, we should not mark it. So we should skip it. So we can simply start = max(pp, ceil(low/p)p). Then for j from start to high step p, mark seg[j-low] = False, but if start == p, then we would mark p. So after computing start, if start == p, we start from start+p. But what if pp == p? That's only for p=1, not prime. So safe: if start == p: start += p. Or we can compute start = ceil(low/p)p, then if start == p: start += p, else start = max(start, pp). Actually we need both conditions: we should start marking from max(pp, start). So compute start = ceil(low/p)p. If start < pp: start = pp. Then if start == p: start += p. But pp is always >= p for p>=2, so if p>=2, pp >= 4, so if start == p, then p < pp, meaning start was set to p because ceil(low/p)p = p and pp > p, but we then set start = max(pp, start) = pp. So start would be pp, not p. So the case start == p cannot happen because max(pp, start) will be at least pp, which is > p. Wait: p=2, pp=4. If low <= 2, ceil(low/2)2 could be 2 if low <=2. Then max(4,2)=4, so start=4. So we never get start = p. So we don't need to worry about marking p itself because we are starting from pp which is larger than p for p>=2. So it's safe. Indeed standard segmented sieve: for each prime p, start = max(pp, ((low + p - 1)//p)p). That ensures we start from pp or the first multiple >= low. Since pp > p, we never mark p itself when p is inside the segment. Good.
But what about when p is less than low? Then ceil(low/p)p >= low, and pp may be < low; start = that multiple, which could be equal to some number that is a multiple of p, which is fine; we mark from there.
Edge case: if low <= 1, we need to mark 1 as non-prime. So after marking, if low == 1, set seg[0] = False.
Then output primes.
Constraints: t <= 10, segments small, so fine.
Now need to write solution in C++? The problem statement: "Warning: large Input/Output data, be careful with certain languages (though most should be OK if the algorithm is well designed)" So any language is fine. We can produce C++ code.
But I'm ChatGPT; I need to output the solution in code block. Usually we output C++.
We'll implement:
Sieve up to 100000? Actually sqrt(1e9)=31623, so we can sieve up to 100000 (or 50000) to be safe. But we only need up to sqrt(n) which max 31623, so sieve up to 40000 or 100000.
Simplify: compute limit = sqrt(1000000000) = 31623. We'll create sieve up to 31623+ maybe.
We'll use vector or bool array.
Steps:
Read t.
Precompute primes up to 31623 using simple Sieve of Eratosthenes.
Use a boolean array is_prime_small of size MAX = 31624 (or 100001). Mark all true then set 0,1 false.
For i from 2 to sqrt(MAX), if is_prime_small[i], then for j = i*i; j <= MAX; j+=i: mark false.
Then collect primes in vector.
For each test case:
Read m, n.
Create a boolean array seg of size n-m+1, initialized to true.
For each prime p in primes with p*p <= n:
Compute start = max(p*p, ((m + p - 1) / p) * p); // integer division to get ceil(m/p)
Note: Input constraints: n-m <= 100000, so seg array size is at most 100001, fine.
Potential issues: If m and n are large, start may overflow if we use int? But within 1e9, fits in 32-bit signed? 1e9 fits in int (max ~2e9 for signed 32-bit). But we should use long long to be safe for multiplication? pp <= n: p up to 31623, pp ~1e9, fits in 32-bit. But when computing start = ((m + p - 1) / p) * p, this may produce a number up to n, which fits in int. However, if m is 1e9, m+p-1 ~1e9+31623, still within int? 1e9+31623=1,031,623, which is less than 2,147,483,647, so okay. So int works. But to be safe, use long long.
But in C++, int is usually 32-bit, can hold up to 2,147,483,647. So okay.
But careful: In for loop j from start to n, j may be up to 1e9, but we can't iterate with int up to 1e9? That's fine; but we are marking indices, not iterating over huge range; the inner loop runs for each prime, marking multiples within segment. Since segment size is small, the number of iterations across all primes is roughly O((n-m+1) * (number of primes up to sqrt(n))/log?) Actually total number of operations is sum over primes p <= sqrt(n) of ( (n-m)/p + 1). Since n-m <= 100000 and sqrt(n)~31623, worst-case number of primes up to 31623 is about 3401. The sum of 1/p over primes is about log log sqrt(n) ~ small. So total marks ~ 100000 * (something) ~ maybe a few million, fine.
We also need fast I/O: use scanf/printf, or cin with sync. Since t <= 10 and output size moderate, can use printf.
Edge Cases: m could be less than 2, so we handle 1.
Also note: The problem statement says "Separate the answers for each test case by an empty line." So after each test case output, print a blank line. Usually after last test case, an empty line is also printed, but it's acceptable either way. We'll output exactly as required: after each test case's primes, print an empty line (including after the last). We'll check: example shows blank line between test cases. So we can output a newline after each test case's output, including an extra newline at the end.
Implementation details:
Precompute primes up to sqrt(1e9) using static array. We'll compute limit = 100000? Actually we can compute limit = floor(sqrt(1000000000)) = 31623. We'll create vector isPrime(limit+1, true). Then for i=2; ii<=limit; i++) if isPrime[i] then for j=ii; j<=limit; j+=i) isPrime[j]=false. Then collect primes.
Alternatively, we can compute up to 100000, but we only need up to 31623.
Better to compute up to 100000 to be safe? But 31623 is enough. However, note that sqrt(1e9) is 31622.776, so we need primes up to 31623 inclusive? Actually we need primes up to sqrt(n) inclusive because if pp <= n, we need to cross off multiples. If n=1e9, sqrt=31622.776, so we need primes <= 31622? Actually we need primes p such that p <= sqrt(n) (strictly pp <= n). So the maximum prime needed is the largest prime <= sqrt(n). Since sqrt(1e9) = 31622.776..., so we need primes <= 31622. So we can set MAX = 31623 (including) to capture up to 31622. So limit = 31623.
But we can just set MAX = 100000 to be safe, and it's still small.
I'll set MAX = 100000 (or maybe 1000000? But 100000 is fine). But we only need up to 31623, but extra doesn't hurt. However, we'll need to iterate primes and condition p*p <= n, so extra primes beyond sqrt(n) won't be used anyway. So we can sieve up to 100000 for simplicity.
But careful: we must generate primes up to sqrt(max_n) = sqrt(1e9) ~ 31623. Using 100000 is fine; it adds a bit more work but negligible.
I'll choose MAX = 100000.
But wait: We'll need to generate primes up to 100000, which is fine.
Better: compute limit = sqrt(1000000000) = 31623, and then sieve up to limit+1. That is efficient.
We'll do:
const int MAX = 31623; // or 100000?
But 31623 is not a round number; we can do 32000.
I'll do:
const int LIMIT = 100000; // enough
But then we'll generate primes up to LIMIT, but we only need up to sqrt(1e9) = 31623, so we can set LIMIT = 40000.
But for safety, I'll set LIMIT = 100000.
Now, segmentation: Use vector or bool array for segment. Since size <= 100001, we can allocate on stack? Better use vector seg(size, true). Or we can use bool seg[100001]; but we need dynamic size. Use vector for memory efficiency, but vector is bitset, but it's okay. However, vector may be slower for indexing? But fine. Use vector for simplicity.
Also, note that m and n can be equal.
Now implement.
Potential pitfalls: For large m, start computed as ((m + p - 1) / p) * p may overflow if using int when multiplied? Actually we use integer arithmetic: (m + p - 1) / p is integer division, then multiply by p, which could be up to n (1e9). That's okay in int. But ensure that (m + p - 1) doesn't overflow? m up to 1e9, p up to 31623, sum <= 1,000,031,623, less than 2^31, so fine. However, if we use unsigned int, still fine.
But some compilers use 32-bit int, which holds up to 2,147,483,647, so okay.
Nevertheless, we can use long long to be safe.
I'll use long long for loop variables.
Now code structure:
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100000; // actually we only need up to sqrt(1e9) = 31623, but 100k is fine.
vector primes;
void sieve() {
vector isPrime(MAX+1, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; ii <= MAX; i++) {
if (isPrime[i]) {
for (int j = ii; j <= MAX; j += i) {
isPrime[j] = false;
}
}
}
for (int i = 2; i <= MAX; i++) {
if (isPrime[i]) primes.push_back(i);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
code
sieve();
int t;
cin >> t;
while (t--) {
long long m, n;
cin >> m >> n;
int size = n - m + 1;
vector<char> isPrimeSeg(size, true);
for (int p : primes) {
if (p * p > n) break;
long long start = max(p * p, ((m + p - 1) / p) * p);
for (long long j = start; j <= n; j += p) {
isPrimeSeg[j - m] = false;
}
}
if (m == 1) isPrimeSeg[0] = false;
for (int i = 0; i < size; i++) {
if (isPrimeSeg[i]) {
cout << (m + i) << "\n";
}
}
cout << "\n";
}
return 0;
}
But note: The problem expects output format: each prime on its own line, and an empty line between test cases. That's what we do: after outputting all primes for a test case, we output a newline (which is an empty line if we already ended last line with newline). However, if we output primes each on a line, the last prime will be followed by a newline, then we output another newline, resulting in a blank line. That matches.
But check: In example, after first test case's primes (2,3,5,7) they have an empty line before second test case output. Our code will print after the last prime (7) a newline from that line, then an additional newline, so there will be one blank line (the extra newline). Good.
One more nuance: The input t can be up to 10, but t lines follow. In the example, they have:
2
1 10
3 5
So our reading is fine.
Potential performance: We generate primes up to MAX=100000, but we only need up to sqrt(1e9) which is about 31623, but generating up to 100k adds about 100k operations, trivial.
But there is one catch: When m is large and close to n, the inner loops may still be okay.
Another catch: For p large, pp may overflow int when p is up to 31623? pp = 1e9, fits in 32-bit signed int. But if we use int for p and n is up to 1e9, pp may be exactly 1e9, okay. But if we used int and p=46341, pp would overflow (2,147,268,281) > 2^31-1, but we stop at p*p <= n, so p won't exceed 31623, so safe.
So we can keep int for p.
But we used long long for start and j to avoid overflow in multiplication? Actually p*p fits in int, but to be safe we cast to long long.
Simplify: Use long long for start and j.
Edge Cases: If m <= 2, we need to output 2. Our algorithm will mark multiples of 2 starting from max(4, ceil(m/2)*2). So if m=2, ceil(2/2)*2=2, but max(4,2)=4, so start=4, so seg[2-2] remains true, so 2 is correctly prime. Good.
For m=1, we set seg[0]=false after marking. But also multiples of 2 start from 4, so 2 remains true. Good.
Also, if m=0 (not in constraints because m>=1), but anyway.
Now, memory: vector of size up to 100001, fine.
Alternative: Use bitset for speed.
Also, note: For each test case, we reset seg.
I think this is correct.
But there is a nuance: If m is 1, we set seg[0]=false after marking. However, there is a possibility that we might have marked 1 as composite via some prime? No, because we start from p*p >=4, so no marking. So fine.
Now I'll produce final answer.
But we must consider a potential improvement: The sieve for small primes up to sqrt(1e9) can be done once.
We'll output solution in C++.
Let's double-check with example:
Test1: m=1, n=10.
primes up to sqrt(10)=3. So p=2: start = max(4, ceil(1/2)*2=2) =4, mark 4,6,8,10. p=3: start = max(9, ceil(1/3)3=3) =9, mark 9. So after marking, seg[1]=? Index 0:1, set false later. Index1:2 -> true, output. Index2:3 -> true, output. Index3:4-> false. Index4:5 -> true (not marked by any prime, because 55=25 >10, so p stops at p<=3). So output 5. Index5:6 false. Index6:7 true (not marked). Output 7. Index7:8 false. Index8:9 false. Index9:10 false. Then we output 2,3,5,7. And after that we output empty line. Good.
Test2: m=3, n=5. primes up to sqrt(5)=2. p=2: start = max(4, ceil(3/2)*2=4) =4, mark 4. So seg[0] (3) true, seg[1] (4) false, seg[2] (5) true (since 5 not marked). Output 3 and 5. Then empty line.
Matches example.
Now we need to handle large input.
But there is a subtle issue: In segmented sieve, we should handle p such that p is within the segment itself: we must ensure we don't mark p. Since we start from max(pp, ...), pp > p for p>=2, so safe. Also for p that are greater than sqrt(n), we don't iterate anyway because p*p > n, so they won't be considered. That's fine; they would be primes if in the segment.
Edge Cases: If m <= p <= n but p*p > n, then p is prime. That's okay.
Now what about p = 1? Not considered.
Implementation details: Use scanf/printf for speed? But we can use cin with sync turned off. That should be fine for up to 100k outputs and 10 test cases. But output could be many primes (up to ~10000 per segment? Actually prime density ~1/log(n) ~ 1/20, so at most ~5000 primes per segment). So total output lines maybe up to 50000, fine. We can use cout.
But sometimes large I/O with cin/cout can be slower. We'll use ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); That should be fast.
Now one more nuance: We must ensure we don't include 0 or 1 as primes. We handle m=1 case. But what if m=0? Not allowed.
Now we also need to consider the case where m and n are both 1: then output nothing (but should print an empty line). Our seg size = 1, set seg[0]=false (since m==1), then no output, then newline. So prints empty line. Good.
Now, is there any special case for p where start may exceed n? Then we skip.
Now, about the limit for primes: Since we only need primes up to sqrt(1e9) = 31623, but we set MAX=100000. However, if we generate primes up to 100000, then for p up to 31623 we have them. But primes greater than 31623 but <= 100000 will have pp > 1e9, so they won't be used because we break when pp > n. So fine.
But we need to ensure that primes list includes at least all primes up to sqrt(1e9). So 31623 < 100000, so okay.
Potential memory: isPrime array of size 100001 bool, trivial.
Now test with extreme: t=1, m=999999937 (largest prime below 1e9?), n=1000000000, segment size = ~63, but we need to output primes between. Our algorithm will generate primes up to sqrt(1e9) = 31623, then for each p, mark multiples. That will mark composite numbers, leaving primes. Works.
Now potential pitfalls: When m is large, start = ((m + p - 1)/p)*p may overflow if m + p - 1 > INT_MAX? But m <= 1e9, p <= 31623, sum <= 1000031623 < 2^31=2147483648, so safe. But if we used unsigned int? Still fine. However, if we used int (signed), the sum is less than 2^31, so safe. But if we had m=2000000000, but constraint says 1e9 max, so fine.
But we used long long, safer.
Now check the loop for j: j is long long, increment by p (int). j <= n, n is long long. No overflow.
Now the only possible inefficiency: For each prime p, we iterate over j from start to n stepping by p. If the segment is small, but n can be large, but we are only iterating over j that are within [m,n] because start is at least the first multiple >= m, and we only go up to n, but step p could still produce many iterations if segment is long? Actually the segment length is at most 100000, so for each p, the number of multiples in [m,n] is at most (n-m)/p + 1 <= 100000/p + 1. So total across all p is about 100000 * sum_{p <= sqrt(n)} 1/p, which is ~100000 * log log sqrt(n) ~ 100000 * small. So manageable.
Now about time: At worst, t=10, each segment 100k, primes up to 31623 ~ 3401, sum 1/p for p up to 31623 ~ log log 31623 + M ≈ log(log(31623)) ≈ log(10.36) ≈ 2.34, plus constant. So total iterations approx 100k * 2.34 ≈ 234k per segment, times 10 = 2.34 million, plus sieve overhead. That's fine.
But we need to consider that for each prime p, we compute start, then loop. However, if p is very small (like 2), the number of multiples in segment of size 100k is about 50k, that's okay.
Now we need to ensure we don't exceed time. With 10 test cases, worst-case maybe ~500k loops for p=2? Actually for p=2, number of multiples = (n-m)/2 + 1 ≤ 50001. For p=3: ~33334, etc. Sum of multiples across all p might be larger if we sum naively: For segment size L=100000, sum_{p <= sqrt(N)} floor(L/p) ~ L * sum_{p <= sqrt(N)} 1/p. The sum of reciprocals of primes up to x is approximately log log x + B. For x=31623, log log x ~ log(10.36)=2.34. So L*2.34 ≈ 234k. That's for one segment. So total across 10 segments is 2.34 million. That's fine.
But also we have marking operations and loops, okay.
Now we can produce final code.
But need to watch out: In the marking loop, we need to ensure j - m fits in the array index; it will because j is within [m,n] inclusive. start is computed as the first multiple >= m and >= p*p, so start >= m. And we loop j from start to n inclusive, so j - m is within [0, size-1].
One more nuance: When m is small, start may be less than m? Actually we take max with ceil(m/p)*p, so start >= m. So safe.
Now we need to handle the case where m <= p < pp? Then start = pp because pp >= m? Not necessarily: if m > pp, then start = ceil(m/p)p, which could be > pp. So safe.
Now code final.
But perhaps we could micro-optimize: Use vector or vector. Using vector may be faster because vector is bit-packed and may have slower access. But with small size, it doesn't matter. But I'll use vector for clarity and speed.
Now also consider that we need to output each prime on a separate line. Use '\n' rather than endl to avoid flushing.
Now we also need to handle reading input: Use scanf is often safer. But we can use cin.
I'll produce code with scanf/printf to be safe.
But many C++ solutions use scanf/printf.
I'll choose cin with sync off for simplicity.
Now confirm no missing headers.
Now produce final answer.
But also note: Some online judges require not to use bits/stdc++.h, but it's allowed in most. I'll include , , , ? Actually we need vector, iostream, cmath maybe for sqrt? But we can compute limit as floor(sqrt(1000000000)) constant.
We'll use sqrt in code? Actually we only need to break when pp > n, we can compute pp as long long. So we don't need sqrt.
But for sieve limit, we can set MAX = 100000, so no sqrt needed. So we can avoid cmath.
I'll include <bits/stdc++.h> for simplicity.
Now produce final code.
But maybe we should be careful: The problem statement says "large Input/Output data, be careful with certain languages". So using fast I/O is recommended.
Let's use printf for output to be safe.
But we are using C++ and printing with cout may be slower if not synced.
I'll use printf for output and scanf for input.
Implementation:
#include
#include
#include
using namespace std;
const int MAX = 100000; // enough for sqrt(1e9)
vector primes;
void sieve() {
vector isPrime(MAX+1, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; ii <= MAX; i++) {
if (isPrime[i]) {
for (int j = ii; j <= MAX; j += i) {
isPrime[j] = false;
}
}
}
for (int i = 2; i <= MAX; i++) {
if (isPrime[i]) primes.push_back(i);
}
}
int main() {
sieve();
int t;
scanf("%d", &t);
while (t--) {
long long m, n;
scanf("%lld %lld", &m, &n);
int size = n - m + 1;
vector isPrime(size, 1);
code
for (int p : primes) {
long long p2 = (long long)p * p;
if (p2 > n) break;
long long start = (m + p - 1) / p * p; // ceil(m/p)*p
if (start < p2) start = p2;
for (long long j = start; j <= n; j += p) {
isPrime[j - m] = 0;
}
}
if (m == 1) isPrime[0] = 0;
for (int i = 0; i < size; i++) {
if (isPrime[i]) {
printf("%lld\n", m + i);
}
}
printf("\n");
}
return 0;
}
Now confirm: In start calculation: (m + p - 1) / p * p. Since (m + p - 1) may overflow? But m is long long, p is int, p-1 fits, so safe. The result is long long.
Check case when m = 0? Not needed.
Now test with example.
I'll mentally test: m=1, n=10. p=2, p2=4. start = ceil(1/2)*2 = 2. start < p2 so start=4. loop j=4,6,8,10: set indices 3,5,7,9 to 0. p=3, p2=9. start=ceil(1/3)*3=3. start < p2 so start=9. j=9: set index 8 to 0. p=5, p2=25>10 break. Then isPrime[0]=1? Actually m==1 so we later set isPrime[0]=0. So indices 1,2,4,6 correspond to 2,3,5,7. Output: 2,3,5,7. Then newline. Good.
Now test m=3, n=5: p=2, p2=4. start=ceil(3/2)*2 = 4. start>=p2, so start=4. j=4: index 1 (5-? Actually j-m=4-3=1) set to 0. p=3: p2=9>5 break. m!=1. So isPrime[0]= (3) true, isPrime[1]=4 false, isPrime[2]=5 true. Output 3,5. newline.
Now test m=1, n=1: size=1. Loop: p=2, p2=4>1, break. Then set isPrime[0]=0 because m==1. No output. Then newline prints empty line. Good.
Now test m=2, n=2: p=2, p2=4>2 break. m==1? no. So isPrime[0]= true? Actually 2 is prime, should output 2. start not executed. So output 2. newline.
Now test m=2, n=100: works.
Now potential bug: For p where start calculation may overflow if we used int? But we used long long.
Now also, we used vector isPrime(size, 1). In C++, char 1 is true, 0 false.
Now we need to ensure that we don't accidentally mark numbers that are prime multiples but start may be 0? Not.
Now we also need to consider the possibility that p may be larger than n? But we break if p2>n, so no.
Now about memory: For each test case, we allocate vector of size up to 100001, fine.
Now final answer: Provide the code as solution.
But I recall that some solutions use a bitset for segment and also pre-sieve small primes. The above is typical.
One more nuance: When m is less than pp, start = pp, but if p*p is less than m, we set start to ceil(m/p)*p which is >= m. So okay.
Now we might want to treat p as long long for multiplication, but we already cast.
Now we can produce final answer.
But there is an improvement: Instead of using vector, we can use vector to save memory, but it's fine.
Now I'll write the final solution in a code block.
Additionally, we can compute the limit for small primes dynamically as sqrt(1000000000) = 31623, but using 100000 is okay.
But note: if we set MAX=100000, then primes up to 100000 include primes greater than sqrt(1e9). That's fine. The sieve operation will be O(MAX log log MAX) which is trivial.
Now check: For p such that p > sqrt(n), we break early because p*p > n. So we don't use those primes. So it's fine.
Now, note: In the loop for p in primes, we have if (p * p > n) break; but p * p might overflow if p is up to 100000? pp could be 1e10, which exceeds 32-bit, but we break condition based on pp > n. If p is > sqrt(n) ~ 31623, pp > 1e9, so the condition will be true, and we break. But we need to compute pp safely. Since p is int (max 100000), pp = 10^10 which is > 2^31-1 and may overflow signed int, causing undefined behavior. So we must avoid overflow. We have two options: 1) Use long long for pp; we did p2 = (long long)p * p; then break if p2 > n. But in the condition of the for loop we have if (p2 > n) break; but we compute p2 inside loop. However, we also have break condition in for loop header? Actually we have for (int p : primes) { long long p2 = (long long)p * p; if (p2 > n) break; ... } So that's safe because we compute p2 as long long before comparison.
But what about the break condition if we wrote "if (p * p > n) break" with p as int, then p*p might overflow for p > 46340 (since sqrt(2^31-1) ≈ 46340). Since p could be up to 100000, overflow could occur, causing negative numbers and potentially incorrect break condition. So we must avoid that. In our code we used long long p2 = (long long)p * p; then if (p2 > n) break; That is safe.
Alternatively, we could have filtered primes to only include those up to 31623 by limiting sieve. But we did safe approach.
Also, start calculation: (m + p - 1) / p * p. Since m and p are long long and int, the division yields long long, safe.
Now finalize code.
But the problem statement might have multiple test cases and we need to produce an empty line after each test case, including the last. We do that.
Now I'll provide the solution.
Potential improvements: Use faster I/O with getchar/putchar, but not necessary.
Now output.
Thus final answer.
```cpp
#include
#include
using namespace std;
const int MAX = 100000; // enough for sqrt(1e9)
vector primes;
// Sieve of Eratosthenes up to MAX
void sieve() {
vector isPrime(MAX + 1, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i * i <= MAX; ++i) {
if (isPrime[i]) {
for (int j = i * i; j <= MAX; j += i)
isPrime[j] = false;
}
}
for (int i = 2; i <= MAX; ++i)
if (isPrime[i]) primes.push_back(i);
}
int main() {
sieve();
int t;
scanf("%d", &t);
while (t--) {
long long m, n;
scanf("%lld %lld", &m, &n);
int size = n - m + 1;
vector isPrime(size, 1); // true for every number in [m, n]
code
for (int p : primes) {
long long p2 = (long long)p * p;
if (p2 > n) break; // no more primes to consider
// first multiple of p inside [m, n] not less than p*p
long long start = (m + p - 1) / p * p; // ceil(m/p)*p
if (start < p2) start = p2;
for (long long j = start; j <= n; j += p)
isPrime[j - m] = 0;
}
if (m == 1) isPrime[0] = 0; // 1 is not prime
// output
for (int i = 0; i < size; ++i)
if (isPrime[i]) printf("%lld\n", m + i);
printf("\n");
}
return 0;