US prime // HDU - 4548 // Number Theory

US prime // HDU - 4548 // Number Theory


topic

Xiao Ming comparative study of the number of love, comes to the number, his mind would emerge out of a good majority of the problems today, Xiao Ming want to test your understanding of prime numbers.
  The problem is this: a decimal number, if a prime number, and its the digits and is prime number, is called "US-prime", such as 29, itself is a prime number, and 2 + 9 = 11 is a prime number, so it is US prime.
  Given a range, you can calculate how many US prime number within this range right?
Input
The first input line T a positive integer, T represents a total set of data (T <= 10000).
Next, a total of T rows each input two integers L, R (1 <= L <= R <= 1000000), a left and right values interval.
Output
For each test, the first number of output Case, then the number of outputs in the interval of US prime numbers (values of L, R).
Each set of data per line, specific examples refer to the output format.
The Input the Sample
. 3
. 1 100
2 2
. 3. 19
the Sample the Output
Case #. 1: 14
Case # 2:. 1
Case #. 3:. 4
Link: https: //vjudge.net/contest/351853#problem/G

Thinking

First sieve sieve again a prime number and the subject needs

Code

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int a[1000001]= {1,1};
int b[30200];
int len=0;

void prime_choose(){
    for(int i=2; i*i<=1000000; i++)               //筛素数
        for(int j=i*i; j<=1000000; j+=i)
            a[j]=1;
    return ;
}

void judge(){                                      //利用筛过的数组判断
    for(int i=2; i<=1000000; i++){
        if(!a[i]){
            int sum=i%10+i/10%10+i/100%10+i/1000%10+i/10000%10+i/100000;
            if(!a[sum])
                b[len++]=i;
        }
    }
    return ;
}
int main()
{
    prime_choose();
    judge();
    int t;
    cin>>t;
    int num=1;
    while(t--){
        int l,r,sum=0;
        cin>>l>>r;
        for(int i=0; i<len; i++){
            if(b[i]>=l&&b[i]<=r)
                sum++;
        }
        printf("Case #%d: %d\n",num++,sum);
    }
    return 0;
}

note

The first use of the array of screen to prevent timeouts.

Published 25 original articles · won praise 9 · views 1747

Guess you like

Origin blog.csdn.net/salty_fishman/article/details/104069163