Description
Xiao Ming's study of the logarithm of love, a talk about the number, the brain emerges a good majority of the problem, today, Xiao Ming wants to test your understanding of the prime number.The problem is this: a decimal number, if it is prime, and its numbers and is also a prime number, it is called the "United States prime", such as 29, itself is a prime, and 2+9 = 11 is also prime, so it is the United States prime. given an interval, can you figure out how many primes there are in this interval?
Input
The first line enters a positive integer t, which indicates that there is a total of T-group data (T <= 10000).Next, a total of two integers l,r (1<= L <= R <= 1000000) are entered for each line, representing the left and right values of the interval.
Output
For each set of data, the number of case is first output, and then the number of us primes in the interval (including the endpoint value l,r) is output.One row for each group of data, and a specific output format see sample.
Sample Input
31 1002 23 19
Sample Output
Case #1:14Case #2:1Case #3:4 Solving ideas: In order to not time out, it is necessary to hit the prime list. Here also to hit a number of beautiful prime numbers table. The code is as follows: (see note for details)
1#include <stdio.h>2#include <math.h>3#include <string.h>4 intprime[1000005],b[1000005];5 intn=1000000;
List of prime numbers6 voidset_prime ()7 {8prime[0]=1; Here is an extra number for Mark 19prime[1]=1; Because 1 is not a prime number, it is marked as 1 .Ten for(intI=2; I<=SQRT (N); i++) One { A if(!Prime[i]) - { - for(intJ=i*i; j<n; j+=i)//here means that multiples of I are not primes. (with j+j more intuitive, with j*j more time, but why can use J*j, I also did not respond to, after all, is small white. (⊙﹏⊙) b) theprime[j]=1; - } - } - } +
The number of prime numbers in the United States table. - voidSet_ansbiao () + { A intTime=0, K; at for(intI=0; i<n; i++) - { - if(!Prime[i])//is a prime number - { - intsum=0; -k=i; in while(k) - { tosum+=k%Ten; +k=k/Ten; - } the if(!Prime[sum])//Its and is the prime number ... *time++; Number plus 1 $ }Panax Notoginsengb[i]=Time ; The number is stored in an array - } the } + A intMain () the { + set_prime (); - Set_ansbiao (); $ intt,cas=0; $scanf"%d",&T); - while(t--) - { the intL,r; -scanf"%d%d",&l,&R);Wuyicas++; theprintf"Case #%d:%d\n", cas,b[r]-b[l-1]); Number of outputs .... - } Wu return 0; -}
HDU 4548 US Prime