线性筛

tech2023-09-10  95

#include<bits/stdc++.h> using namespace std; const int N = 1000005; int prime[N], cnt; bool vis[N]; void get_prime(int n) { for (int i = 2; i <= n; i++) { if (!vis[i]) prime[cnt++] = i; for (int j = 0; prime[j] <= n / i; j++) { vis[prime[j] * i] = true; if (i % prime[j] == 0)//prime[j]一定是i的最小质因子,保证了每个合数只会被它的最小质因子筛掉 break; } } } int main() { int n; cin >> n; get_prime(n); cout << cnt; return 0; }

 

最新回复(0)