UV 524, uva524

Source: Internet
Author: User

UV 524, uva524

Description

 

A ring is composed of n (even number) circles as shown in digoal. Put natural numbers into each circle separately, and the sum of numbers in two adjacent circles shocould be a prime.

 

 

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

 

Input N (0 <n <= 16)

 

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.

 

You are to write a program that completes abve process.

 

Sample Input
68

 

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 2

 

Solution: If brute force enumeration is used, the total number can be up to 16! = 2*10 ^ 13 ,. It will time out.

So this uses backtracking. An array is used to store the sequence to be output. The judgment condition is that if the current number is added with the sum of the previous number and the prime number is not marked. If the conditions are met, add 1 recursion and Mark. Until it reaches the recursive boundary (cur = n ).

 

 

The Code is as follows:

 

 

 

 

1 # include <cstdio> 2 # include <cstring> 3 using namespace std; 4 int vis [50], isp [50], A [50], n; 5 int is_prime (int x) // use a function to determine if it is a prime number 6 {7 for (int I = 2; I * I <= x; I ++) 8 if (x % I = 0) return 0; 9 return 1; 10} 11 12 void dfs (int cur) 13 {14 if (cur = n & isp [A [0] + A [n-1]) // no recursive boundary is found, and do not forget to judge whether the last and first values are prime numbers because they are a circle. 15 {16 for (int I = 0; I <n-1; I ++) 17 printf ("% d", A [I]); // output according to the question format, note the space issue. (I planted TM on this morning) 18 printf ("% d", A [n-1]); 19 printf ("\ n "); 20} 21 else22 for (int I = 2; I <= n; I ++) // put 23 if (! Vis [I] & isp [I + A [cur-1]) // if conditions are met, and not marked with 24 {25 A [cur] = I; 26 vis [I] = 1; // Mark 27 dfs (cur + 1); // recursion 28 vis [I] = 0; // clear mark 29} 30} 31 int main () 32 {33 int m = 0; 34 while (scanf ("% d", & n) = 1 & n) 35 {36 if (m> 0) 37 printf ("\ n"); 38 + + m; 39 for (int I = 2; I <= n * 2; I ++) 40 isp [I] = is_prime (I); 41 printf ("Case % d: \ n", m ); 42 // memset (vis, 0, sizeof (vis); 43 A [0] = 1; // A [0] Must be 144 dfs (1 ); // recursion from the beginning 45} 46 return 0; 47}

 

 

 


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.