题目链接:https://www.luogu.org/problemnew/show/P1865
其实就是埃拉托色尼筛素数模板...
好像每个数暴力枚举到sqrt()也可以...就算当我无聊练手罢
1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 const int maxn = 1000000 + 10; 7 bool prime[maxn]; 8 int n, m, left, right, sq, tot = 0; 9 int main() 10 { 11 memset(prime,0,sizeof(prime)); 12 scanf("%d%d", &n, &m); 13 14 sq = sqrt(m); 15 prime[1] = 1; 16 for(int i = 2; i <= sq; i++) 17 if(prime[i] == 0) 18 { 19 for(int j = i*i; j <= m; j+=i) 20 prime[j] = 1; 21 } 22 23 for(int i = 1; i <= n; i++) 24 { 25 scanf("%d%d", &left, &right); 26 if(left > m || right > m || left <= 0 || right <= 0) 27 { 28 printf("Crossing the line\n"); 29 continue; 30 } 31 else 32 { 33 for(int j = left; j <= right; j++) 34 { 35 if(prime[j] == 0) 36 tot++; 37 } 38 printf("%d\n",tot); 39 tot = 0; 40 } 41 42 } 43 return 0; 44 }