In a serial digit prime number, the first digit number, and the number till the second digit …the number till the n-th digit are all a prime number.
For example, in the number 7331, the first digit number 7 is a prime number; the number till the second digit 73 is a prime number; the number till the third digit 733 is too a prime number; the number till the last digit 7331 is also a prime number.


When a digit of a serial digit prime number to be found is given, find all serial digit prime numbers with the given digit.


Time Limit : 1 sec (Java : 2 sec) (If your program exceeds this time limit, the answers that have been already printed are ignored and the score becomes 0. So, it may be better to print a wrong answer when a specific test case might cause your program to exceed the time limit. One guide for the time limit excess would be the size of the input.)


[Input]

There can be more than one test case in the input file. The first line has T, the number of test cases.
Then the totally T test cases are provided in the following lines (T ≤ 10 )

In each test case, A digit, N (1 ≤ N ≤ 8), is given in the second line. (1 ≤ N ≤ 8)

 

[Output]
For each test case, you should print "Case #T" in the first line where T means the case number.

For each test case, you should output one N-digit serial digit prime number in each line in an ascending order.
If there is not one serial digit prime number with the given digit, output 0.

[I/O Example]

Input
2
1
4


Output
Case #1

2
3
5
7

Case #2
2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393

// get all prime number, methord 2
#include <iostream>
#include <cmath>
using namespace std;
typedef struct number{
    int pre;
    int after;
} NumberArray[8];
int Answer;
int main(int argc, char** argv)
{
    NumberArray numberArray;
    int num = 0;
    int i,j,m,temp,temp10;
    int prej,afterj;
    bool flagArray[100000]; //5 0 YYYYYYY
    int primeArray[100];
    for(i = 0; i < 100000; i++)  
        flagArray[i] = true;
    for(i=2; i<sqrt(100000.0); i++)
    {    
        if(flagArray[i])
        {
            for(int j = i; j*i <= 100000; j++)
            {
                flagArray[j*i] = false;
            }
        }
    }
    

    j = 0;
    prej = j;
    numberArray[0].pre = prej;
    for(i=2;i<10;i++)
    {
        if(flagArray[i])
        {
            primeArray[j]=i;
            ++j;
        }
    }
    afterj = j;
    numberArray[0].after = afterj;


    for(int n = 1;n<=4;n++) // n=3 standing for 2333   YYYYYYY
    {
        m=prej;
        prej = j;
        numberArray[n].pre = prej;
        for(;m<afterj;m++)
        {
            temp = primeArray[m]*10;
            temp10 = temp+10;
            while(temp<temp10)
            {
                if(flagArray[temp])
                {
                    primeArray[j]=temp;
                    ++j;
                }
                temp++;
            }
        }
        afterj = j;
        numberArray[n].after = afterj;
    }


        int T, test_case,N;
    /*
       The freopen function below opens input.txt file in read only mode, and afterward,
       the program will read from input.txt file instead of standard(keyboard) input.
       To test your program, you may save input data in input.txt file,
       and use freopen function to read from the file when using cin function.
       You may remove the comment symbols(//) in the below statement and use it.
       Use #include<cstdio> or #include <stdio.h> to use the function in your program.
       But before submission, you must remove the freopen function or rewrite comment symbols(//).
     */    

    // freopen("input.txt", "r", stdin);

    cin >> T;
    for(test_case = 0; test_case  < T; test_case++)
    {
        cin>>N;
        cout << "Case #" << test_case+1 << endl;
        if(N<=5) //YYYYYYY
        {
            for(int x = numberArray[N-1].pre;x < numberArray[N-1].after;x++)
            {
                cout << primeArray[x] << endl;
            }
        }
        else
        {
            cout<<" "<<endl;
        }
        /////////////////////////////////////////////////////////////////////////////////////////////
        /*
           Implement your algorithm here.
           The answer to the case will be stored in variable Answer.
         */
        /////////////////////////////////////////////////////////////////////////////////////////////
        //Answer = 0;
        
        // Print the answer to standard output(screen).
        
        //cout << Answer << endl;
    }

    return 0;//Your program should return 0 on normal termination.
}

 

posted on 2015-06-19 15:32  暴走路人甲  阅读(120)  评论(0)    收藏  举报