Prime Ring problem
Time limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 32717 Accepted Submission (s): 14482
Problem 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 Input68
Sample outputcase 1:1 4 3 2 5 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 and nyoj Prime ring procedure The same
#include <stdio.h> #include <string.h> #define MAX 21int a[max];int vis[max];int prime[50];int k;void Biao () { int i,j;for (i=0;i<=50;i++) prime[i]=1;for (i=2;i<=50;i++) {if (Prime[i]) {for (j=2*i;j<=50;j+=i) {prime[j]=0;}}} prime[1]=0;} void Dfs (int cur,int n) {int i,j;if (Cur==n+1&&prime[1+a[n]]) {for (i=1;i<=n;i++) {if (i==1) printf ("%d", A[i]) ; else printf ("%d", A[i]);} printf ("\ n");} Else{for (i=2;i<=n;i++) {if (!vis[i]&&prime[i+a[cur-1]) {A[cur]=i;vis[i]=1;dfs (cur+1,n); vis[i]=0;}}} int main () {int N,m,j,i,t;biao (); K=1;while (scanf ("%d", &n)!=eof) {memset (a,0,sizeof (a)); Memset (vis,0,sizeof (Vis ); a[1]=1;vis[1]=1;printf ("Case%d:\n", k++), if (n==1) printf ("1\n"), Elsedfs (2,n);p rintf ("\ n");} return 0;}
Hdoj 1016 Prime Ring problem