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. spring-boot配置外部静态资源的方法

    import java.io.File; import javax.servlet.Servlet; import lombok.extern.slf4j.Slf4j; import org.spri ...

  2. 【gradle报错】error: package org.apache.http does not exist

    导入项目的时候gradle报错 error: package org.apache.http does not exist 解决方法: 在build.gradle中加入 android {   use ...

  3. ubunutu_install_sublime_china

    保存下面的代码到文件sublime_imfix.c(位于~目录) #include <gtk/gtkimcontext.h> void gtk_im_context_set_client_ ...

  4. [改善Java代码]Lock与synchronized是不一样的

    很多编码者都会还说,Lock类和synchronized关键字用在代码块的并发性和内存上时语义是一样的.

  5. 【三分搜索算法】UVa 10385 - Duathlon

    题目链接 题意:“铁人三项”比赛中,需要选手在t km的路程里进行马拉松和骑自行车项目.现有n名选手,每位选手具有不同的跑步速度和骑车速度.其中第n位选手贿赂了裁判员,裁判员保证第n名选手一定会取得冠 ...

  6. Sublime Text 插件之常用20个插件

    作为一个开发者你不可能没听说过 Sublime Text.不过你没听说过也没关系,下面让你明白. Sublime Text是一款非常精巧的文本编辑器,适合编写代码.做笔记.写文章.它用户界面十分整洁, ...

  7. 整合Spring与Hibernate

    在学习spring的时候,要整合hibernate,本来是看起来挺简单的,但是遇到的远要比想到了多,而且多很多,期间几天一个bug实在难调,几度放弃,但终究柳暗花明,抑制不住喜悦就想着分享一下成果吧. ...

  8. Java加密解密相关

    关于解释加密解密中的填充方案: http://laokaddk.blog.51cto.com/368606/461279/ 关于对称加密中的反馈模式: http://blog.csdn.net/aaa ...

  9. jQuery之对话框

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...

  10. Cocos2d-x优化中多线程并发访问

    多线程并发访问在Cocos2d-x引擎中用的不是很多,这主要是因为中整个结构设计没有采用多线程.源自于Objective-C的Ref对象,需要使用AutoreleasePool进行内存管理,Autor ...