Prime Ring problem
Time limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 34108 Accepted Submission (s): 15098
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 INPUT6 8
Sample outputcase 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
Sourceasia 1996, Shanghai (Mainland China)
recommendjgshining | We have carefully selected several similar problems for you:1010 1072 1175 1253 1181 RE: Prime circle, dictionary output;
1#include <cmath>2#include <cstdio>3#include <cstring>4#include <iostream>5 using namespacestd;6 intdis[ -], vis[ -], my[ -];7 intN;8 intIs_prime (intA//judge the prime number;9 {Ten One if(A = =1|| A = =0) A return 0; - if(A = =2) - return 1; the Else - { - for(intI=2; I <= sqrt (a); i++) - { + if(a% i = =0) - return 0; + } A return 1; at } - } - voidDfs (inta) - { - inti; - if(A = = N && is_prime (dis[n-1] + dis[0])) in { - for(i =0; I < n; i++) to { + if(I >0) -printf" "); theprintf"%d", Dis[i]); * } $printf"\ n");Panax Notoginseng return;//The end of the sub-cycle; - } the for(i=2; i<=n; i++) + { A if(!vis[i] && is_prime (Dis[a-1] +i)) the { +Vis[i] =1; -Dis[a] =i; $Dfs (A +1); $Vis[i] =0; - } - } the } - intMain ()Wuyi { the intI, j =1; - while(~SCANF ("%d", &N)) Wu { -memset (Vis,0,sizeof(Vis)); Aboutprintf"Case %d:\n", J + +); $vis[0] =1; -dis[0] =1; -Dfs (1); -printf"\ n"); A } + return 0; the}
Hangzhou Electric 1016--prime Ring problem (DFS)