ACM simulation questions (3) -- number theory (continued)

Source: Internet
Author: User

5. Prime Ring Problem
Problem Description

A ring is compose of n circles as shown in digoal. put natural number 1, 2 ,..., n into each circle separately, and the sum of numbers in two adjacent circles shoshould be a prime.

Note: the number of first circle shoshould always be 1.

Input

N (0 <n <20 ).

Output

The output format is shown as sample below. each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. the order of numbers must satisfy the above requirements. print solutions in lexicographical order.

You are to write a program that completes abve process.

Print a blank line after each case.

 

Sample Input68 Sample OutputCase 1:1 4 3 2 5 61 6 5 2 3 4 Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 3 21 6 7 4 3 8 5 2

N numbers (, 3... n) are enclosed in a circle, and the sum of the two adjacent numbers is a prime number. According to the given n, all numerical sequences that can form a circle meeting the conditions are calculated.

Solution:

Select 1 first, and then select a number that is equal to the prime number after adding 1. There may be many situations, such as (n = 6 ):

1 + 21 + 41 + 6 and then select the number for the second number and the prime number for each case to obtain the sequence below

1 + 2 + 31 + 2 + 51 + 4 + 31 + 6 + 5 until all the numbers are selected, list all the combinations. The calculation process is the process of constructing the following tree.

 

 

It can be seen that 1-4-3-2-5-6 and 1-6-5-2-3-4 are two valid solutions.

For tree traversal, the depth is preferred, or the breadth is preferred. If the memory occupied by the breadth is large, it is not suitable to use the solution when the space is large.

Below is the implementation of breadth first (when n = 18 and n = 20, the memory is insufficient)

/** Prime Ring Problem */public static void test4 (int n) {int values [] = new int [n]; // initialize for (int I = 0; I <n; I ++) {values [I] = I + 1;} // indicates the possible solution List in the traversal process = new ArrayList (); list. add (values); // n-1 digits after which 1 is always the first for (int I = 1; I <n; I ++) {List temp = list; list = new ArrayList (); // for each possible solution, obtain the next node for (int j = 0; j <temp. size (); j ++) {int tempValues [] = (int []) temp. get (j); // consider all possible combinations for (int k = I; k <n; K ++) {if (isPrime (tempValues [I-1] + tempValues [k]) {// create a new state, and copy the original value int [] newValues = Arrays. copyOf (tempValues, tempValues. length); // if (I! = K) {int change = newValues [I]; newValues [I] = newValues [k]; newValues [k] = change ;} // Add the new status to the list. add (newValues) ;}}}// output result for (int I = 0; I <list. size (); I ++) {int [] tempValues = (int []) list. get (I); if (isPrime (tempValues [0] + tempValues [n-1]) {for (int j = 0; j <n; j ++) {System. out. print (tempValues [j] + "");} System. out. println () ;}} System. out. println (list. size ();} below is the depth-first algorithm.

/** Prime Ring Problem (depth first) */public static void test5 (int n) {// The first n elements of the array represent the number in the Ring, the n + 1 data indicates that the first n + 1 element in the array is the int values [] = new int [n + 1] that meets the conditions. // initialize for (int I = 0; I <n; I ++) {values [I] = I + 1;} values [n] = 1; // indicates the possible solution List in the traversal process = new ArrayList (); list. add (values); StringBuffer sb = new StringBuffer (); while (list. size ()> 0) {// retrieve the first element int tempValues [] = (int []) list. get (0); // indicates the layer to which the data is processed. The first layer uses 0 to indicate int index = tempValues [n]; // traverse and generate all possible next-Layer Nodes for (int k = tempValues [n]; k <n; k ++) {if (I

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.