US prime Time limit:3000/1000ms (Java/other) Memory limit:65535/32768k (Java/other) total submission (s): Accepted Su Bmission (s): 4font:times New Roman | Verdana | Georgia Font size:←→problem DescriptionXiao 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? Inputthe 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. OutputFor 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
Thinking of solving problems
If solved by brute force, decisive timeout. The problem requires marking first. The first marking is found in the range of primes, the second hit the table is found in the range of the United States, the third hit the table is to find the number of U.S. primes before each number.
To determine the number of us primes in the range, use num[m]-num[n-1], minus the number of us primes before num[n], and leave the num[n]
Code
#include <stdio.h> #include <string.h>int a[1100000],b[1100000],num[1100000];//is defined as the global variable int main () {int t; int M,n;int I,j,k,l=1;memset (a,0,sizeof (a)); Memset (b,0,sizeof (b)); memset (num,0,sizeof (num)); A[0]=a[1]=1;for (i=0; i<1005000;i++) {if (A[i]) continue;for (j=i+i;j<1005000;j+=i) a[j]=1;} Determine if it itself is not a prime for (i=0;i<1005000;i++) {if (a[i]==0) { j=i; k=0; while (j) { k+=j%10; j/=10; } if (a[k]==0) b[i]=1;}} Judge whether the beauty primes for (i=1;i<=1000000;i++) if (b[i]==1&&a[i]==0) num[i]=num[i-1]+1; Else
201412021633-hd-US Prime