A prime number is a positive number, which is divisible by exactly two different integers. A digit primeis a prime number whose sum of digits is also prime. For example the prime number 41 is a digit primebecause 4 + 1 = 5 and 5 is a prime number. 17 is not a digit prime because 1 + 7 = 8, and 8 is nota prime number. In this problem your job is to find out the number of digit primes within a certainrange less than 1000000.
Input
First line of the input file contains a single integer N (0 < N ≤ 500000) that indicates the total numberof inputs. Each of the next N lines contains two integers t1 and t2 (0 < t1 ≤ t2 < 1000000).
Output
For each line of input except the first line produce one line of output containing a single integer thatindicates the number of digit primes between t1 and t2 (inclusive).
Sample Input
3
10 20
10 100
100 10000
Sample Output
1
10
576
Note: You should at least use scanf() and printf() to take input and produce output for thisproblem. cin and cout is too slow for this problem to get it within time limit.
问题简述:(略)
问题分析:
筛选法是必要的,先找出素数。
然后再算一下前缀和,区间值计算就变得简单了。
程序说明:(略)
题记:(略)
参考链接:(略)
AC的C++语言程序如下:
/* UVA10533 Digit Primes */
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
const int N = 1e6;
const int SQRTN = ceil(sqrt((double) N));
bool isPrime[N + 1];
int prefixsum[N + 1];
// Eratosthenes筛选法
void esieve(void)
{
memset(isPrime, true, sizeof(isPrime));
isPrime[0] = isPrime[1] = false;
for(int i=2; i<=SQRTN; i++) {
if(isPrime[i]) {
for(int j=i*i; j<=N; j+=i) //筛选
isPrime[j] = false;
}
}
}
int sum_digits(int n, int base)
{
int sum = 0;
while(n) {
sum += n % base;
n /= base;
}
return sum;
}
void maketable()
{
prefixsum[0] = 0;
for(int i = 1; i <= N; i++)
prefixsum[i] = (isPrime[i] && isPrime[sum_digits(i, 10)]) ? prefixsum[i - 1] + 1 : prefixsum[i - 1];
}
int main()
{
esieve();
maketable();
int n, t1, t2;
scanf("%d", &n);
while(n--) {
scanf("%d%d", &t1, &t2);
printf("%d\n", prefixsum[t2] - prefixsum[t1 - 1]);
}
return 0;
}