Hangzhou power 1016 prime ring Problem

Source: Internet
Author: User
Prime ring Problem Time Limit: 4000/2000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 25187 accepted submission (s): 11246


Problem descriptiona 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.


 
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 are to write a program that completes abve process.

Print a blank line after each case.
 
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
 
Sourceasia 1996, Shanghai (Mainland China) Pay attention to the boundary and end-to-end inference to determine whether the conditions are met.
# Include <iostream> # include <cmath> # include <cstring> # include <algorithm> # define m 25 using namespace STD; int vis [m], n, ans [m], T = 0; int sum (int A, int B) // function for determining prime numbers {int K = 0, sum, I; sum = A + B; for (I = 2; I <sum; I ++) {If (sum % I = 0) {k = 1; return 0 ;}} if (! K) return 1;} void DFS (int x) {int I; If (x = N & sum (ANS [0], ANS [n-1]) // determine whether the first and end of a recursive boundary meet the condition {for (I = 0; I <n; I ++) {if (I = 0) cout <ans [0]; else cout <"<ans [I];} cout <Endl;} else {for (I = 2; I <= N; I ++) {If (! Vis [I] & sum (I, ANS [x-1]) {ans [x] = I; vis [I] = 1; // mark used count DFS (x + 1); vis [I] = 0; // restore used count }}} int main () {int I, j; while (CIN> N) {T ++; memset (VIS, 0, sizeof (VIS )); cout <"case" <t <":" <Endl; vis [1] = 1; ans [0] = 1; DFS (1 ); cout <Endl; // There is an empty line between each example} return 0 ;}

Hangzhou power 1016 prime ring Problem

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.