标签:
埃式素数筛法的复杂度是:
求区间
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long int LL;
const int M = 100009,INF = 0x3fffffff;
vector<int> prime;
void get_prime(void) {
bool isprime[1000009];
memset(isprime, 0, 1000009);
for (int i = 2; i <= 1000000; i++) {
if (!isprime[i]) {
prime.push_back(i);
for (int j = i; j <= 1000000; j += i) isprime[j] = true;
}
}
}
int primes(LL a, LL b) {
bool isprime[1000009];
int ans = 0;
memset(isprime, 0 , b - a);
for (int i = 0; i < prime.size() && prime[i] < b; i++) {
int j = 0;
bool find = false;
for (LL k = a; k < b; k++, j++) if (k % prime[i] == 0) { find = true; break; }
if (!find) continue;
for (; j < b - a; j += prime[i]) { isprime[j] = true; if (j + a == prime[i]) ans++; }
}
for (int i = 0; i < b - a; i++) if (!isprime[i]) ans++;
if (a <= 1) ans -= 2 - a;
return ans;
}
int main(void) {
//problem: , address:
LL a, b;
get_prime();
while (~(scanf("%lld%lld", &a, &b))) {
cout << primes(a, b) << endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/jibancanyang/article/details/47108025