Links: http://acm.hdu.edu.cn/showproblem.php?pid=4548
answer
#include <CString> #include <the iostream> the using namespace STD; const int n-= 1000005 ; int Prime [ 1000005 ]; BOOL VIS [ 1000005 ]; int CNT [ 1000005 ]; // number of records US prime numbers up to the j void Oula () { // play table by Euler sieve int CNT = 0 ; Memset (Prime, 0 , the sizeof (Prime)); Memset (VIS, to false , the sizeof (VIS)); for (int T = 2 ; T <= n-; T ++) { // Enumeration T IF (VIS [T])! // find the deleted number is not Prime [CNT ++] = T; // Add prime number table for ( int J = 0 ; J <CNT prime && * t [J] <= n-; J ++) { // enumeration table primes VIS [prime * t [J]] = to true ; // multiplication tag table t and the number of prime numbers together IF (t% prime [J] == 0 ) // if the number is a prime divisor enumerated encounter of t out of the loop BREAK ; } } } void beautfulprime () { CNT [ 0 ] = 0 ; CNT [ . 1 ] = 0 ; for ( int J = 2 ; J < 1000005 ; J ++ ) { CNT [J] = CNT [J- . 1 ]; // inherited on a counted number of digital Miso, then calculate the current digital meets the requirements Long Long SUM = 0 ; int K = J; the while (K) { SUM +% = K 10 ; K / = 10 ; } iF (VIS [J] == to false && VIS [SUM] == to false ) //Number j is a prime number that you figures and are prime CNT [j] ++ ; } } int main () { int m; Scanf ( " % D " , & m); Oula (); beautfulprime (); int L , R & lt; for ( int T = 0 ; T <m; T ++ ) { Scanf ( " % D% D " , & L, & R & lt); the printf ( " Case #% D:% LLD \ n- " , T + . 1 , CNT [R & lt] -CNT [L- . 1 ]); } return 0 ; }
Because large numbers, we need to hit the table in order to avoid time-out
For the first time hit the table, to find all the prime numbers right border of the screen by Euler, followed by a second hit the table in the table obtained for a number of characteristics of Miso
Array cnt stored in the digital i number of all US prime number so far, the results directly to the left and right borders do subtraction