# Prime Ring problem--hdu1016

Source: Internet
Author: User

 Prime Ring problemTime limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)Total submission (s): 40419 Accepted Submission (s): 17843Problem Descriptiona Ring is compose of n circles as shown in diagram. Put Natural number 1, 2, ..., n into each circle separately, and the sum of numbers in the adjacent circles should is a PR Ime.Note:the number of first circle should always be 1.INPUTN (0 < n < 20).Outputthe 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 is to write a program, that completes above process.Print a blank line after each case.Sample Input`68`Sample Output`Case 1:1 4 3 2 5 a 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`Sourceasia 1996, Shanghai (Mainland China) `#include #include #include using namespace Std;int prime[] = { 3,5,7,11,13,17,19,23,29,31,37};//when n is less than 20 o'clock the sum of the maximum of two numbers is 37int primeflag[39];//is used to quickly determine whether a prime number, when the element value is 1 o'clock the current count is the prime of int visited[21 ];//is used to mark whether the current number has been used, a value of 1 is used, 0 means that an array with an int ans[21];//stored answer is not used, a void Dfs (int n)//Deep search and backtracking, in order to avoid timeouts using non-recursive method {int flag = 0;// Represents the previous position of the current number of fills int i = 2;//from 2 to n searches for numbers that can be filled in while (flag >= 0) {///when flag<0 indicates that all solutions have been found while (i <= N) {//when i& Gt;n indicates the current position all numbers have been tried if (visited[i]! =-1 && primeflag[ans[flag] + i] = = 1) {//When the current number has not been tried and satisfies the sum of the last number The number of primes is filled with ans[++flag] = i; Visited[i] =-1; i = 2;//after each fill in a number, the loop variable is initialized to 2 to indicate the next position to restart the search} else i++; } if (flag = = = N-1 && Primeflag[ans[flag] + 1] = = 1) {//when the last while loop has ended and flag has reached the final position and the first number meets the last number and is prime, indicates the current There is a solution for (int j = 0; J < N-1; J + +) {cout << ans[j] << ""; } cout << ans[n-1] << endl;//no space after last number} i = ans[flag];//The current position is saved when backtracking visited[i] = 0;//the Number re-marked as not used i++;//number I have tried on the flag, the next cycle as long as the number from i++ to avoid repeating the dead loop flag--;//back to a position to re-fill the number at the flag+1 location-backtracking}}i NT main () {int n,ncase = 0; for (int i = 0; i < one; i++) {primeflag[prime[i]]= 1; } while (CIN >> N) {memset (visited,0,sizeof (visited)); memset (ans,0,sizeof (ans)); Ans[0] = 1; VISITED[1] =-1; ncase++; cout << "Case" << ncase << ":" << Endl; if (n% 2 = = 0)//can satisfy test instructions produce output only if n is an even number, because the feasible solution must be odd and even alternate arrangement, when n is odd-numbered, there will be one more odd Dfs (n); cout <

Prime Ring problem--hdu1016

Related Keywords:

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.