題目:給出一個閉區間,和一個數,問該區間中包含幾個含有該數的素數
分析:直接上埃法就行。
Code:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 4000000+5;
bool nprime[maxn];
int ans;
char c[maxn];
void init() {
memset(nprime, 0, sizeof(nprime));
nprime[0] = nprime[1] = 1;
for(int i = 2; i < maxn; i++) {
if(!nprime[i]) {
for(int j = i+i; j < maxn; j += i)
nprime[j] = 1;
}
}
}
int main() {
init();
int a, b, d;
while(scanf("%d%d%d", &a, &b, &d) != EOF) {
ans = 0;
for(int i = a; i <= b; i++) {
if(!nprime[i]) {
sprintf(c, "%d", i);
int len = strlen(c);
for(int j = 0; j < len; j++)
if(c[j] == '0'+d) {
ans++;
break;
}
}
}
printf("%d\n", ans);
}
return 0;
}