HDU1016 Prime Ring Problem

来源:互联网 发布:windows defender红叉 编辑:程序博客网 时间:2024/06/04 19:59

Prime Ring Problem

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


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
68
 

Sample Output
Case 1:1 4 3 2 5 61 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
 

Source

Asia 1996, Shanghai (Mainland China)


题意:环中任意相邻两数之和为素数,并且第一个数一直是1


思路:这道题可以用深搜递归写,当然暴力也可以(只是不知道会不会超时)


这里介绍用递归的解法:


#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>using namespace std;int a[25];int vis[25];int n;int judge(int m){    int i,j;    int c=sqrt(m);    for(i=2;i<=c;i++)        if(m%i==0)        break;    if(i<=c)    return 1;    return 0;}void dfs(int num){    int i=1;    if(num==n&&judge(a[0]+a[n-1])==0)//第一位和最后一位也相邻,要判断一下是否和为素数    {        for(i=0;i<n-1;i++)          printf("%d ",a[i]);        printf("%d\n",a[n-1]);    }    else    {        for(i=2;i<=n;i++)//递归构造全排列        {            if(judge(i+a[num-1])==0&&vis[i]==0)            {                a[num++]=i;                vis[i]=1;                dfs(num);//继续下一位                vis[i]=0;                num--;            }        }    }}int main(){    int t=0;    while(cin>>n)    {        t++;        printf("Case %d:\n",t);        memset(vis,0,sizeof(vis));        a[0]=1;//第一位有特殊要求        dfs(1);        cout<<endl;    }    return 0;}
这道题在南阳理工后来又提交总是Timelimitexceeded.就觉得递归没问题,只要在开始之前判断一下奇偶就可以了。奇数肯定是没有满足条件的。
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<math.h>#include<algorithm>using namespace std;int vis[20];int num[20];int ans=0;int prime(int n){    int i;    if(n==0||n==1)        return -1;    if(n==2||n==3)        return 1;    for(i=2;i<=sqrt(n);i++)    {        if(n%i==0)        {            return -1;        }    }    return 1;}void dfs(int n,int cur,int a[]){    if(cur==n)    {        if(prime(1+a[n-1])==1)        {            cout<<"1";            for(int i=1;i<n;i++)            cout<<" "<<a[i];            cout<<endl;            ans++;        }        return ;    }    for(int i=2;i<=n;i++)    {        if(vis[i]==0&&prime(a[cur-1]+i)==1)        {            vis[i]=1;            a[cur]=i;            dfs(n,cur+1,a);            vis[i]=0;        }    }}int main(){    int n,i,a[20],t=0,flag=0;    while(cin>>n,n!=0)    {        t++;        cout<<"Case "<<t<<":"<<endl;        if(n%2&&n!=1)        {            printf("No Answer\n");            continue;        }        memset(vis,0,sizeof(vis));        a[0]=1;        vis[0]=1;        dfs(n,1,a);        cout<<endl;    }    return 0;}




0 0
原创粉丝点击