bool isnp[MAXN]; // is not prime: 不是素数 voidinit(int n) { for (int i = 2; i * i <= n; i++) if (!isnp[i]) for (int j = i * i; j <= n; j += i) isnp[j] = 1; }
欧拉筛
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
bool isnp[MAXN]; vector<int> primes; // 质数表 voidinit(int n) { for (int i = 2; i <= n; i++) { if (!isnp[i]) primes.push_back(i); for (int p : primes) { if (p * i > n) break; isnp[p * i] = 1; if (i % p == 0) break; } } }