Description

 

A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  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 <= 16)

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.

You are to write a program that completes above process.

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

解题思路:如果这题用暴力枚举的话排列总数最高可高达16!=2*10^13,。就会超时。

所以这用回溯。用一个数组来储存要输出的序列,判断条件就是如果目前的数加上他前面的数的和为素数并且没有这数没被标记。满足条件就加1递归并标记。直到走到递归边界(cur==n)。

代码如下:

 #include<cstdio>
#include<cstring>
using namespace std;
int vis[],isp[],A[],n;
int is_prime(int x) //用一个函数来判断是不是素数
{
for (int i = ; i*i<= x ; i++)
if (x% i == ) return ;
return ;
} void dfs(int cur)
{
if(cur==n&&isp[A[]+A[n-]])  //判断到递归边界没有,并且不要忘了判断最后一个和第一个加起来是不是素数,因为它是一个圆。
{
for(int i=; i<n-; i++)
printf("%d ", A[i]);//按照题目格式输出,注意空格问题。(我TM栽在这一上午)
printf("%d",A[n-]);
printf("\n");
}
else
for(int i=; i<=n; i++) //从2开始一个一个放
if(!vis[i]&&isp[i+A[cur-]]) //如果满足条件,和没有被标记
{
A[cur]=i;
vis[i]=; //标记
dfs(cur+);  //递归
vis[i]=; //清除标记
}
}
int main()
{
int m=;
while(scanf("%d",&n)==&&n)
{
if(m>)
printf("\n");
++m;
for(int i=; i<=n*; i++)
isp[i]=is_prime(i);
printf("Case %d:\n",m);
 //memset(vis,,sizeof(vis));
A[]=; //A[0]一定为1
dfs();    //从一开始递归
}
return ;
}

 
 

UVA 524的更多相关文章

  1. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  2. UVa 524 Prime Ring Problem(回溯法)

    传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...

  3. Uva 524 Prime Ring

    如果用全排列生成之后,在判断是否是素数环是会超时的,应该用回溯. 回溯的时候  首先要注意 递归边界 ,结束的时候别忘记判断最后一个和第一个元素能否成立  还有要记得vis的使用和递归之后的清理. # ...

  4. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...

  5. UVa 524 Prime Ring Problem【回溯】

    题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 #include< ...

  6. 暴力求解——素环数 Prime Ring Problem ,UVa 524

    Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers i ...

  7. UVa 524 Prime Ring Problem(DFS , 回溯)

    题意  把1到n这n个数以1为首位围成一圈  输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的  n最大为16  利用回溯法 边生成边推断  就要快非常多了 #inc ...

  8. UVa 524 - Prime Ring Problem

    题目大意:输入正整数n,把整数1,2...,n组成一个环,使得相邻两个整数之和均为素数.输出时从整数1开始逆时针(题目中说的不是很明白??)排列.同一个环应恰好输出一次. 枚举,并在枚举每一个数是进行 ...

  9. 7-4素数环 uva 524

    #include<iostream> #include<cstdio> #include<cmath> #include<cstring> using ...

随机推荐

  1. android开发之路07(无硝烟的战争)

    如何做一名优秀的android面试官? 如何做一名优秀的android候选者? 提到这个问题我不得不提起我们小升初,初升高,高生升本这几个历程中我们与出题人之间的无硝烟的战争.我们总是为自己的成绩担心 ...

  2. 对x264_macroblock_cache_load的理解

    X264版本: 2004/06/03 函数作用: 将编码该宏块所需的信息加载到mb.pic.mb.cache两个结构体中,记录相邻宏块的存在性. 函数过程: 初始化坐标信息,这些坐标信息将在下面用作下 ...

  3. 转:Nginx+ffmpeg的HLS开源服务器搭建配置及开发详解

    转:http://itindex.net/detail/51186-nginx-ffmpeg-hls 本文概述: 至目前为止,HLS 是移动平台上非常重要并十分流行的流媒体传输协议.做移动平台的流媒体 ...

  4. iOS下使用sqlite3

    1.创建数据库 使用firefox的sqlite manager创建和打开数据库,详细请参考: http://www.cnblogs.com/hanjun/archive/2012/10/29/274 ...

  5. submit text 插件安装教程

    特注:import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); o ...

  6. spark集成hive遭遇mysql check失败的问题

    问题: spark集成hive,启动spark-shell或者spark-sql的时候,报错: INFO MetaStoreDirectSql: MySQL check failed, assumin ...

  7. linux学习书籍推荐linux学习书籍推荐

    引用地址:http://www.cnblogs.com/notepi/archive/2013/06/15/3137103.html Linux 学习书目推荐 Linux基础 1.<Linux与 ...

  8. 使用subst创建虚拟磁盘及设置分区卷标

    最近项目中要研究在"计算机"中添加虚拟磁盘,能够访问某远端目录,同时还要在资源管理器中可以看到创建的虚拟磁盘.关于虚拟磁盘,有几种方式: (1)映射网络磁盘:通过映射网络驱动器,可 ...

  9. IOS 解析JSON

    - (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, t ...

  10. C#之base关键字

    1.base关键字用于从派生类中访问基类的成员. 2.调用基类上已被其他方法重写的方法. 3.指定创建派生类的实例是应调用基类的构造函数. 4.访问基类的公有成员和受保护成员,不能访问私有成员 5在静 ...