1031 quality rings, 1031 quality rings

Source: Internet
Author: User

1031 quality rings, 1031 quality rings
1031 prime number ring

 

Time Limit: 1 s space limit: 128000 KB title level: Gold QuestionDescription Description

A prime number ring with a size of N (N <= 17) is a number ring composed of N natural numbers ranging from 1 to N. The sum of two adjacent numbers on the number ring is the prime number. If it is a 6-sized prime number ring. For ease of description, it is required that the first number on the ring is always 1. Such as 1 4 3 2 5 6. If the numbers are arranged in the same order as the two prime number rings, they are considered to be essentially the same. Now you need to find all the essentially different number rings.

Input description Input Description


Only one number N indicates the size of the required prime number ring. For example:

Output description Output Description

Each row describes a number ring. If multiple groups of solutions exist, output data from small to large in Lexicographic Order. For example:

Sample Input Sample Input

6

Sample output Sample Output

1 4 3 2 5 6

1 6 5 2 3 4

Data range and prompt Data Size & HintN <= 17 category tags Tags click here to expand
1 # include <iostream> 2 # include <cmath> 3 using namespace std; 4 int a [10001] = {1001}; 5 int vis []; 6 int n; 7 bool pd (int x, int y) 8 {9 int k = 2, I = x + y; 10 while (k <= sqrt (I) & I % k! = 0) k ++; 11 if (k> sqrt (I) return 1; 12 else return 0; 13} 14 void printf () 15 {16 for (int I = 1; I <= n; I ++) 17 cout <a [I] <""; 18 cout <endl; 19} 20 void f (int k) // now indicates the remaining value 21 {22 if (k = n + 1 & pd (a [n], a [1]) 23 {24 printf (); 25 return; 26} 27 else28 {29 for (int I = 2; I <= n; I ++) 30 {31 if (vis [I] = 0 & pd (a [k-1], I) 32 {33 vis [I] = 1; 34 a [k] = I; 35 f (k + 1); 36 vis [I] = 0; 37 a [k] = 0; 38} 39} 40} 41} 42 int main () 43 {44 45 cin> n; 46 vis [1] = 1; 47 f (2); 48 return 0; 49}
1 # include <iostream> 2 # include <cmath> 3 using namespace std; 4 int a [10001] = {1001}; 5 int vis []; 6 int n; 7 bool pd (int x, int y) 8 {9 int k = 2, I = x + y; 10 while (k <= sqrt (I) & I % k! = 0) k ++; 11 if (k> sqrt (I) return 1; 12 else return 0; 13} 14 void printf () 15 {16 for (int I = 1; I <= n; I ++) 17 cout <a [I] <""; 18 cout <endl; 19} 20 void f (int k) // now indicates the remaining value 21 {22 if (k = n + 1 & pd (a [n], a [1]) 23 {24 printf (); 25 return; 26} 27 else28 {29 for (int I = 2; I <= n; I ++) 30 {31 if (vis [I] = 0 & pd (a [k-1], I) 32 {33 vis [I] = 1; 34 a [k] = I; 35 f (k + 1); 36 vis [I] = 0; 37 a [k] = 0; 38} 39} 40} 41} 42 int main () 43 {44 45 cin> n; 46 vis [1] = 1; 47 f (2); 48 return 0; 49}

 

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.