HOJ2276 of Problem solving report SOJ2498 Count Prime
Description
Given an integer interval [L, R] (l <= R <= 2147483647, R-l <= 1000000), please calculate the number of prime (s) In the interval.
Input
There is one line in the input, which contains and integer:l, R.
Output
There is only one line, which contains the number of prime (s) in the interval.
Sample Input
2 11
Sample Output
5
The main topic: output [L,r] The number of primes within, of which r-l not more than 1e6.
Analysis: This problem l,r range is very large, with the ordinary prime number sieve can not be solved, so it is obviously an interval prime sieve problem. The general idea is to first find a small range of prime numbers, and then use a small number of prime numbers to screen large numbers in a small range of primes. Then we can count the number of characters in a large number of small interval. The specific step is to first obtain the first prime number of the >=a, and then start the sieve twice times from the prime number. At last, we can count the number of characters in a large number of small interval.
on the code:
#include <iostream> #include <algorithm> #include <cmath>using namespace Std;const int maxn = 50000; const int MAXM = 1e6 + 10;typedef long long ll;int ISPRIME[MAXN]; Small number of primes to determine int ISPRIMEB[MAXM]; Large number of primes determine ll prime[maxn];//small prime ll primeb[maxm];//large prime number int cnt,cntb;void getp ()//linear prime sieve faster than normal sieve {for (int i = 1; i < MAXN; i++) Isprime[i] = 1;for (int i = 2; i < MAXN; i++) {if (Isprime[i]) {prime[cnt++] = i;} for (int j = 0; J < cnt&&prime[j] * I < MAXN; j + +)//using the minimum quality factor and residual mass factor product strategy to sieve {ISPRIME[PRIME[J] * i] = 0;if (i%prime [j] = = 0) break;}}} void Getintp (ll a,ll b)//interval prime sieve {for (int i = 0; I <= b-a; i++) isprimeb[i] = 1;int k;for (int i = 0; i < cnt&& amp;prime[i]*prime[i]<=b; i++) {k = A/prime[i];if (K*prime[i] < a) k++; Find the first prime number of >=a if (k <= 1) k++; If K==1 is said to be the first prime number, of course, can not be filtered out, in fact, K can not be equal to 0while (K*prime[i] <= b) {Isprimeb[k*prime[i]-a] = 0;//sieve walk k++;} CNTB = 0;for (int i = 0; I <= b-a; i++) {if (Isprimeb[i]) primeb[cntb++] = i + A;}} int main () {ll A, b;getp (); WHIle (Cin >> a >> b) {ll ans = 0;if (b < MAXN) {for (int i = A; I <= b; i++) if (isprime[i]) ans++;} ELSE{CNTB = 0;GETINTP (A, b); for (int i = 0; I <= b-a;i++) if (isprimeb[i]) ans++;} cout << ans << endl;} return 0;}
The number of numbers is basically finished, and the next plate is the theorem of Poyal and repulsion. Congruence equation, good to die. ,
HOJ2276 of Problem Solving report SOJ2498 Count prime