Prime Ring Problem
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 5 Accepted Submission(s) : 4
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
A 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 two adjacent circles should be a prime.
Note: the number of first circle should always be 1.
Note: the number of first circle should always be 1.
Input
n (0 < n < 20).
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. Print solutions in lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
You are to write a program that completes above process.
Print a blank line after each case.
Sample Input
6 8
Sample Output
Case 1: 1 4 3 2 5 6 1 6 5 2 3 4 Case 2: 1 2 3 8 5 6 7 4 1 2 5 8 3 4 7 6 1 4 7 6 5 8 3 2 1 6 7 4 3 8 5 2#include<iostream> #include<string.h> using namespace std; int n,f[21]; int prime[50]; int DFS(int x[21],int s[21],int l) { if(l==n) { if(prime[1+s[l]]==0) { cout<<"1"; for(int i=2;i<=n;i++) cout<<" "<<s[i]; cout<<endl; } return 0; } else { for(int j=2;j<=n;j++) if(x[j]==0&&prime[j+s[l]]==0) { s[l+1]=j,x[j]=1; DFS(x,s,l+1); if(j<=n) x[j]=0; } } return 0; } int main() { int s[21],x[21],l,sum; sum=0; memset(prime,0,sizeof(prime)); for(int i=2;i<=25;i++) for(int j=2;j*i<=50;j++) prime[j*i]=1; while(cin>>n) { sum++; memset(x,0,sizeof(x)); cout<<"Case "<<sum<<":"<<endl; s[1]=1;l=1; DFS(x,s,l); cout<<endl; } return 0; }