A-prime ring problem (prime ring, deep search, play table)

Source: Internet
Author: User

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 the adjacent circles should is a PR Ime.

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 is to write a program, that completes above process.

Print a blank line after each case.

Sample Input

68

Sample Output

Case 1:1 4 3 2 5 a 6 5 2 3 4Case 2:1 2 3 8 5 6 7 2 5 8 3 4 7 4 7 6 5 8 3 6743852SOURCE:HTTP://ACM.HUST.E du.cn/vjudge/contest/view.action?cid=91073#problem/a This problem is the input n, let you find from 1 to n these integers consisting of the number of adjacent numbers and for the prime of the ring has several, starting from 1 clockwise output Note that the topic requires the output line of case N: (see this colon ah ~~~~~~~~ at that time did not see the WR), and print a blank lines after each case. , (also did not see this, really miserable AH ~ ~ ~ ~) to reduce the complexity of the technique: the maximum number of the occurrence of the prime is 37, so do not have to each case to be counted again adjacent numbers and is not prime, this may time out, and Dfs is very complex, can be used pri[i] memento i is a prime number,
#include <cstdio>#include<string.h>#include<math.h>intnum[ -], T, N, now;//Num records the last selected numberBOOLvis[ -], pri[ +];//Pri[i] = 1 means I is a prime number, pri[i] = 0 means I is not a prime numberBOOLPrimeintY//A function of type bool so that you do not have to define the variable as the return value{     for(inti =2; I <= sqrt (y); i++)    {        if(y% i = =0)            return false; }    return true;}voidDfsintNowintT//the number of T has been found, the number of T is now, that is, the current number is today, enter DFS is to find the number of t+1{    if(t = = N)//to find the nth number, look at the nth number and the first number to add whether it is a prime, because the composition is a ring, PS: Must remember!!!     {        if(pri[now+1]) {printf ("1");  for(inti =2; I <= N; i++) printf ("%d", Num[i]); printf ("\ n"); }        return; }     for(inti =2; I <= N; i++)    {        if(Vis[i] && pri[i+Now ]) {Vis[i]=0; Num[t+1] =i; DFS (i, t+1); Vis[i]=1; }    }}intMain () {intCNT =1;  while(~SCANF ("%d", &N) {memset (Vis,1,sizeof(VIS));  for(inti =3; I < +; i++)        {            if(Prime (i)) Pri[i]=1; ElsePri[i]=0; } printf ("Case %d:\n", cnt++);//when I forgot the colon!!!!!!!!!!!!!!!!!!!!!!!!!! It's awful. ~ ~num[1]=1; vis[1] =0; DFS (1,1); printf ("\ n");//I also forgot to output a blank line between each case ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!!!!!!!! Ouch ~ ~ ~    }    return 0;}

A-prime ring problem (prime ring, deep search, play table)

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.