1246. Prime Number Filtering
Time Limit: 2000 MS Memory Limit: 65536 K
Total Submissions: 802 (129 users) Accepted: 108 (69 users)
[My Solution]
Description
Number of prime numbers in the request output range [A, B.
Input
Multiple groups of data are input.
The first positive integer T (T <10) indicates the number of input data groups.
The second row to the T + 1 row, each row 2 digits A, B (0 <A <= B <2 ^ 31, and B-A <2 ^ 20) indicates the range of the range.
Output
Output The number of T rows. Each row has only one number, indicating the number of corresponding prime numbers.
Sample Input
2
100 200
2 1000000
Sample Output
21
78498
Source
Doraemon @ xmu
[Cpp] # include <iostream>
# Include <cmath>
Using namespace std;
# Define N 46341
# Define M 1000010
Long p [N], q [M], n, m, a [N], B [M];
Void init ()
{
Memset (a, 0, sizeof ());
Int I, j, gab = 4;
A [2] = a [3] = 1; p [0] = 2; p [1] = 3; n = 2;
For (I = 5; I <N; I + = gab ^ = 6)
{
If (! A [I])
{
A [I] = 1; p [n ++] = I; // if (I> = 46000) cout <I <"";
For (j = I; j * I <N; j ++)
A [I * j] =-1;
}
}
}
Void cnt (long l, long r)
{
Long I, j, k, size = r-l;
If (r <N)
{
M = 0;
For (I = l; I <= r; I ++)
If (a [I] = 1) m ++;
} Else
{
Memset (B, 0, sizeof (B ));
For (I = 0; I <n & p [I] * p [I] <= r; I ++)
{
J = l/p [I]; while (j * p [I] <l) j ++;
If (j <= 1) j ++;
For (; j * p [I] <= r; j ++)
B [j * p [I]-l] = 1;
}
M = 0;
For (I = 0; I <= size; I ++)
If (B [I] = 0) m ++;
}
}
Int main ()
{
Init ();
Long t, l, r;
// For (int I = 0; I <100; I ++) cout <p [I] <"";
Scanf ("% lld", & t );
While (t --)
{
Scanf ("% lld", & l, & r );
If (l <2) l = 2;
Cnt (l, r );
Printf ("% lld \ n", m );
}
Return 0;
}
# Include <iostream>
# Include <cmath>
Using namespace std;
# Define N 46341
# Define M 1000010
Long p [N], q [M], n, m, a [N], B [M];
Void init ()
{
Memset (a, 0, sizeof ());
Int I, j, gab = 4;
A [2] = a [3] = 1; p [0] = 2; p [1] = 3; n = 2;
For (I = 5; I <N; I + = gab ^ = 6)
{
If (! A [I])
{
A [I] = 1; p [n ++] = I; // if (I> = 46000) cout <I <"";
For (j = I; j * I <N; j ++)
A [I * j] =-1;
}
}
}
Void cnt (long l, long r)
{
Long I, j, k, size = r-l;
If (r <N)
{
M = 0;
For (I = l; I <= r; I ++)
If (a [I] = 1) m ++;
} Else
{
Memset (B, 0, sizeof (B ));
For (I = 0; I <n & p [I] * p [I] <= r; I ++)
{
J = l/p [I]; while (j * p [I] <l) j ++;
If (j <= 1) j ++;
For (; j * p [I] <= r; j ++)
B [j * p [I]-l] = 1;
}
M = 0;
For (I = 0; I <= size; I ++)
If (B [I] = 0) m ++;
}
}
Int main ()
{
Init ();
Long t, l, r;
// For (int I = 0; I <100; I ++) cout <p [I] <"";
Scanf ("% lld", & t );
While (t --)
{
Scanf ("% lld", & l, & r );
If (l <2) l = 2;
Cnt (l, r );
Printf ("% lld \ n", m );
}
Return 0;
}