首先以求1000000以内的素数为例来探讨筛法
时间复杂度:O(N*loglogN)
空间复杂度:O(N)
代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;
const int maxn = 1000005;
bool vis[maxn];
int prime[maxn];
int tot;
void init() {
tot = 0;
memset(vis, false, sizeof(vis));
for(int i = 2; i < maxn; i ++) {
if(!vis[i]) {
prime[tot ++] = i;
for(int j = i * 2; j < maxn; j += i) {
vis[j] = true;
}
}
}
}
int main() {
init();
for(int i = 0; i < 100; i ++) {
cout << prime[i] << " ";
}
return 0;
}
每个合数只会被它最小的质因数筛去,因此时间复杂度为O(N)。
此种为线性筛法
代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;
const int maxn = 1000005;
bool vis[maxn];
int prime[maxn];
int tot;
void init() {
tot = 0;
memset(vis, false, sizeof(vis));
for(int i = 2; i < maxn; i ++) {
if(!vis[i]) prime[tot ++] = i;
for(int j = 0; j < tot; j ++) {
if(i * prime[j] > maxn) break;
vis[i * prime[j]] = true;
if(i % prime[j] == 0) break;
}
}
}
int main() {
init();
for(int i = 0; i < 100; i ++) {
cout << prime[i] << " ";
}
return 0;
}
代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;
const int maxn = 1000005;
bool vis[maxn];
int prime[maxn];
int fai[maxn];
int tot;
void init() {
memset(vis, false, sizeof(vis));
fai[1] = 1;
tot = 0;
for(int i = 2; i < maxn; i ++) {
if(!vis[i]) {
prime[tot ++] = i;
fai[i] = i - 1;
}
for(int j = 0; j < tot; j ++) {
if(i * prime[j] >= maxn) break;
vis[i * prime[j]] = true;
if(i % prime[j] == 0) {
fai[i * prime[j]] = fai[i] * prime[j];
break;
}
else {
fai[i * prime[j]] = fai[i] * (prime[j] - 1);
}
}
}
}
int main() {
init();
for(int i = 1; i < 100; i ++) {
cout << fai[i] << " ";
}
return 0;
}
代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std;
const int maxn = 1000005;
bool vis[maxn];
int prime[maxn];
int mu[maxn];//莫比乌斯函数
int tot;
void init() {
memset(vis, false, sizeof(vis));
mu[1] = 1;
tot = 0;
for(int i = 2; i < maxn; i ++) {
if(!vis[i]) {
prime[tot ++] = i;
mu[i] = -1;
}
for(int j = 0; j < tot; j ++) {
if(i * prime[j] >= maxn) break;
vis[i * prime[j]] = true;
if(i % prime[j] == 0) {
mu[i * prime[j]] = 0;
break;
}
else {
mu[i * prime[j]] = -mu[i];
}
}
}
}
int main() {
init();
for(int i = 1; i < 100; i ++) {
cout << mu[i] << " ";
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u014355480/article/details/47345561