Digit Primes UVA – 10533

Digit Primes UVA – 10533Aprimenumberisapositivenumber,whichisdivisiblebyexactlytwodifferentintegers.Adigitprimeisaprimenumberwhosesum

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;
}

 

今天的文章Digit Primes UVA – 10533分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/62694.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注