Article Directory
Subject to the effect
Problem-solving ideas
The complexity of the Egyptian-style screening method is a. But the scope of this problem is can not be used angstroms type sieve. And space complexity is not allowed. But we can learn from the Egyptian-style ideological sieve method, which is based on two facts;
- (1) Number engagement withinthe smallestprime factor must not exceed . which is and is a composite number, so it must be less than the quality factor
- (2) make Primes table, and from the cross out all multiples of a prime number, the rest is a prime number.
So to understand this question, we need to maintain a
- : is used to determine Whether the number is a prime number within the range.
Since digital too, in order to save space. We maintain a
- : to judge whether it is a prime number.
Code
#include<iostream>
using namespace std;
typedef long long ll;
const int MAXN = 1000000+5;
ll is_prime1[MAXN];
ll is_prime2[MAXN];
void get_primes(ll a, ll b)
{
// ------------初始化 --------------
for(ll i=0; i*i<b; i++)
is_prime1[i] = 1;
is_prime1[0] = is_prime1[1] = 0;
for(ll i=a; i<b; i++)
is_prime2[i-a] = 1;
// ---------------------------------
for(ll i=2; i*i<b; i++)
{
if(is_prime1[i])
{
// 筛is_primes1
for(ll j=(ll)2*i; j*j<b; j+=i)
is_prime1[j] = 0;
// 筛is_primes2
for(ll j=(ll)(a-1+i)/i*i; j<b; j+=i)
is_prime2[j-a] = 0;
}
}
}
int main()
{
ll a, b;
cin >> a >> b;
get_primes(a, b);
ll ans = 0;
for(ll i=a; i<b; i++)
{
if(is_prime2[i-a])
ans++;
}
cout << ans << endl;
}
Knowledge Point
- The above code in such a line
j = (ll)(a-1+i)/i*i
. is rounded up, so this effect is obtained by the first computing of multiples.