Nyoj 488 prime ring (Deep Search)

Source: Internet
Author: User

Nyoj 488 prime ring (Deep Search)
Prime ring time limit: 1000 MS | memory limit: 65535 KB difficulty: 2

Description

There is an integer n, which sorts the numbers from 1 to n into loops without repetition. The sum of every two adjacent numbers (including the first and last) is a prime number, which is called a prime ring.

For simplicity, we specify that each Prime Ring starts from 1. For example, it is a prime ring of 6.

Input
There are multiple groups of test data, each group input a n (0 Output
The first row of each group outputs the corresponding Case sequence number, starting from 1.
If there is a prime number ring that satisfies the meaning of the question, it is output from small to large.
Otherwise, No Answer is output.
Sample Input
6830
Sample output
Case 1:1 4 3 2 5 61 6 5 2 3 4Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 6 7 4 3 8 5 2Case 3:No Answer
Source
Hdu adaptation
Uploaded

ACM _ Ding Guoqiang

Deep Search is used;

Deep Search template:

// Deep Search template: (this is my summary. If something is wrong, please kindly advise me !) Int n; // first apply for a global variable int visit [10010]; // mark the element int a [10010]; // store the element void dfs (int cur) {// here is the recursive exit condition if (cur = n) // The Judgment condition here to check whether the array subscript starts from 1 {if () // certain conditions defined in the question. {Printf (""); // output the corresponding value} for (I = 2; I <= n; ++ I) {if () // It is also related to the conditions in the question. At that time, an additional criterion was added to determine whether {visit [I] = 1; a [k] = I; // store the data that meets the conditions in the current situation to the array dfs (k + 1); // recursive visit [I] = 0 for the following elements; // these two steps are related to the output of multiple groups of data. When the recursion jumps out, the output of the data that meets the meaning of the question must be returned }}}

The Code is as follows:

# Include
     
      
# Include
      
       
Int n; int visit [110]; int ring [110] = {1}; int prime (int x) // judge whether it is a prime number {for (int I = 2; I * I <= x; ++ I) {if (x % I = 0) return 0;} return 1;} void dfs (int k) // Deep Search {int I; if (k = N-1) // condition for loop bounce {if (prime (1 + ring [k]) // satisfy the sum of {printf ("1"); for (I = 1; I
       
        


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.