大神代码赏析:
#include<stdio.h> #define N 100000 int main() { int e[N+1] = {0}, p[N], pn = 0, an, m, i, j, t; for(i = 2; i <= N; e[i++] = 1); for(i = 2; i <= N; i++) if(e[i]) for(p[pn++] = i, j = i + i; j <= N; j += i) e[j] = 0; for(an = pn, i = 0; an < N - 1; i++) { if((t = p[i] + 1) <= N && !e[t]) e[p[an++] = t] = e[p[i]] + 1; for(j = 0, m = N / p[i]; j < pn && p[j] <= m; j++) if(!e[t = p[i] * p[j]]) e[p[an++] = t] = e[p[i]] + 1; } for(i = 3; i <= N; i++) e[i] += e[i - 1]; while(scanf("%d%d", &i, &j) != EOF) printf("%d\n", e[j] - e[i - 1]); return 0; }
时间: 2024-11-05 18:56:05