Digit Primes UVA - 10533

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.

题目给出一种定义如果一个素数它的个位数字之和也是素数那么这个数就是一个什么数,反正就是这个数挺厉害的。

给出一个区间(包括区间端点)(t1,t2),(t1<=t2),找出区间中所有这样的数的个数。mmp,甘霖娘,场上做了1个半小时一直wa就是不知道哪里错,然后就开始怀疑1和2是不是素数,最后想了一下定义竟然觉得1和2都是素数,实际上1不是素数,然后就一直wa了。结束以后补题的时候也没看出来,问了队里的凯子哥,他也没看出来,最后是大鹏发现的,交上以后就AC了。

首先我们需要找到所有的素数,这就需要一个板子,筛选素数并打表,再然后利用前缀用一个数组 sign[i] 记录下来从1到i(包括 1 和 i )的所有这样数的个数,最后因为包括 t1 和  t2 ,所以 sign[t2] - sign[t1 - 1]就是所求结果。

彻底记住这道题了

#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cmath>
#include <stack>
#include <map>
#define ll long long
#define INF 0x3f3f3f3f
#define mod 9973
using namespace std;
const int maxn = 1000000 + 5;
int vis[maxn],sign[maxn];
void dabiao()
{
    memset(vis,0,sizeof(vis));
    memset(sign,0,sizeof(sign));
    int m = sqrt(maxn + 0.5);
    for(int i=2; i<=m; i++)
    {
        if(!vis[i])
        {
            for(int j=i*i; j<=maxn; j+=i)
                vis[j] = 1;
        }
    }//素数打表
    int num = 0;
    vis[1] = 1;
    for(int i=1; i<=maxn; i++)
    {
        if(!vis[i])
        {
            int t = i,sum = 0;
            while(t)
            {
                sum += t % 10;
                t /= 10;
            }
            if(!vis[sum]) num++;
        }
        sign[i] = num;
    }//前缀
}
int main()
{
    int n;
    dabiao();
    scanf("%d",&n);
    while(n--)
    {
        int t1,t2;
        scanf("%d%d",&t1,&t2);
        printf("%d\n",sign[t2]-sign[t1-1]);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值