1031 Prime number Ring (deep search practice)

Source: Internet
Author: User

Topic website : http://codevs.cn/problem/1031/
Title Description Description
A prime ring of size n (n<=17) is a number ring consisting of 1 to N of a total of n natural numbers, and the sum of each of the two contiguous numbers on a number of rings is prime. As a prime-number ring of size 6. For convenience of description, the first digit on the number ring is always 1. If available 1 4 3 2 5 6来 description. If the two prime number rings, the numbers are arranged in the same order as essentially the same. Now ask you to find all the different rings of the essence.

Enter a description input Description

There is only one number n, which represents the size of the required prime ring. Such as:

Outputs description Output Description
Each line describes a number loop, and if there are multiple sets of solutions, the output is from small to large in dictionary order. Such as:

sample input to sample
6

sample output Sample outputs
1 4 3 2 5 6

1 6 5 2 3 4

Data Size & Hint
N<=17

puzzle: Brute Force search
The code is detailed :

#include <cstdio>#include <cmath>using namespace STD;inta[ -],n,b[ -];//a is used to store optional data and B is used to store the data that has been placedintFintN//test is a prime number, 1 is not, 0 is a prime number{ for(intI=2; i<=sqrt(n); i++) {if(n%i==0)return 0; }return 1;}voidDfsintX//Search{if(x==n) {if(F (b[x-1]+1)){//See if the last and first can be added to a prime number             for(intI=0; i<n;i++) {printf("%d", B[i]); }printf("\ n"); }    }Else{intU for(intI=1; i<n;i++)//Traverse an optional number{if(a[i]!=0&&f (a[i]+b[x-1]) {u=a[i];                B[x]=u; a[i]=0; DFS (x+1); b[x]=0;            A[i]=u; }        }if(b[x]==0)return; }}intMain () {scanf("%d", &n);if(n%2==1)return 0;//For odd number direct no answer (don't ask me why, I just put it all tested again, the specific verification also need tell)     for(intI=1; i<n;i++) a[i]=i+1; b[0]=1;//Make sure the first one is number 1thDfs1);//Starting from 1th PS: number No. 0 is already 1.}

1031 Prime number Ring (deep search practice)

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.