埃氏素数筛 On
const int maxn = 1e+6 + 7; bool prime[maxn]; int rec[maxn], cnt; void init_prime() { cnt = 0; memset(prime, true, sizeof(prime)); prime[0] = prime[1] = false;///表明true为质数 for (int i = 2; i <= maxn; ++i){ if (prime[i]) rec[cnt++] = i; //此处边界判断为rec[j] <= maxn / i,如果写成i * rec[j] <= maxn,需要确保i * rec[j]不会溢出int for (int j = 0; j < cnt && rec[j] <= maxn / i; ++j){ prime[i * rec[j]] = false; if (i % rec[j] == 0) break; } } }