hdu-1016-Prime Ring Problem


Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34318    Accepted Submission(s): 15175


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.


 

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.
 

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
 

Source
Asia 1996, Shanghai (Mainland China)
 

Recommend
JGShining

题目分析:

就是输出两想加都等于素数的环

第一个代码是全排列的一个代码模板



#include 
#include 
int n,m,a[10],arr[10],mark[10];
void dfs(int v){
    if(v >= n){
        for(int i = 0;i < n;i++)
            printf("%d ",a[i]);
        printf("\n");
        return ;
    }
    for(int i = 0; i< n;i++){
            mark[i]=1;
            dfs(v+1);
            mark[i] = 0;  
        }
    }
}
int main(){
    while(scanf("%d",&n)==1){
    	int i;
        memset(mark,0,sizeof(mark));
        for(i=0;i
#include
int n;
int a[21],mark[21];
int prime[] = {0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0}; //这里是前40的素数表
void dfs(int v)
{
    if(v==n+1)
    {
        if(prime[a[v-1]+1])//这里记得判断首尾相加是不是素数
        {
            for(int i=1;i







你可能感兴趣的:(ACM,ACM)