A prime number is a positive number, which is divisible by exactly two different integers. A digit prime is a prime number whose sum of digits is also prime. For example the prime number 41 is a digit prime because 4+1 = 5 and 5 is a prime number. 17 is not a digit prime because 1+7 = 8, and 8 is not a prime number. In this problem your job is to find out the number of digit primes within a certain range less than 1000000.
Input
First line of the input file contains a single integer N (0 < N ≤ 500000) that indicates the total number of 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 that indicates 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 this problem. cin and cout is too slow for this problem to get it within time limit.
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define ll long long
const int maxn=1000005;
int vis[maxn],sign[maxn];
void init()
{
memset(vis,true,sizeof(vis));
for(int i=2;i<=maxn;i++)
if(vis[i])
for(int j=2;j*i<=maxn;j++)
vis[i*j]=false;
}
bool ok(int x)
{
int k=0;
while(x)
{
k+=x%10;
x/=10;
}
return vis[k];
}
int main()
{
init();
int t;
cin>>t;
for(int i=2;i<=maxn;i++)
if(vis[i]&&ok(i))
sign[i]=1;
for(int i=2;i<=maxn;i++) sign[i]+=sign[i-1];
while(t--)
{
int i,j;
scanf("%d%d",&i,&j);
printf("%d\n",sign[j]-sign[i-1]);
}
return 0;
}
该程序计算在1000000以下指定范围内满足条件的数字素数(其数字之和也是素数)的数量。通过初始化素数表并检查每个数字的数字之和是否为素数来实现。对于每个输入的范围,程序输出该范围内数字素数的总数。

1万+

被折叠的 条评论
为什么被折叠?



