Prime Ring Problem 

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.

 #include<cstdio>
#include<cstring>
bool prm[],vis[];
int a[],n;
bool ck(int x)
{
int i;
for (i=;i*i<=x;i++)
if (x%i==) return ;
return ;
}
void dfs(int p)
{
int i,j,k,x,y,z;
if (p==n+)
{
if (prm[a[n]+a[]])
{
printf("%d",a[]);
for (i=;i<=n;i++)
printf(" %d",a[i]);
printf("\n");
}
return;
}
for (i=;i<=n;i++)
if (vis[i]==&&prm[i+a[p-]])
{
a[p]=i;
vis[i]=;
dfs(p+);
vis[i]=;
}
}
int main()
{
int i,j,k,p,q,x,y,z,t;
bool bbb=;
for (i=;i<=;i++)
prm[i]=ck(i);
a[]=;
t=;
while (scanf("%d",&n)==)
{
if (bbb) printf("\n");
bbb=;
memset(vis,,sizeof(vis));
vis[]=;
printf("Case %d:\n",++t);
dfs();
}
}

素数环。注意边界。注意每组数据间的回车(虽然题上没说)。

uva 524 prime ring problem——yhx的更多相关文章

  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(DFS , 回溯)

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

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

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

  4. UVA - 524 Prime Ring Problem(素数环)(回溯法)

    题意:输入n,把1~n组成个环,相邻两个数之和为素数. 分析:回溯法. #pragma comment(linker, "/STACK:102400000, 102400000") ...

  5. UVa 524 Prime Ring Problem【回溯】

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

  6. UVa 524 - Prime Ring Problem

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

  7. uva 524(Prime Ring Problem UVA - 524 )

    dfs练习题,我素数打表的时候j=i了,一直没发现实际上是j=i*i,以后可记住了.还有最后一行不能有空格...昏迷了半天 我的代码(紫书上的算法) #include <bits/stdc++. ...

  8. Uva 552 Prime Ring Problem(dfs)

    题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...

  9. Uva 524 Prime Ring

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

随机推荐

  1. (旧)子数涵数·PS——文字人物

    首先我们来看一下我用到的素材(在百度图库里下载的). 一.打开PS,在PS中打开素材. 二.复制一个图层(好习惯不解释). 三.图像->调整->阈值,或者按下图示按钮后选择阈值,弹出阈值窗 ...

  2. mysql metadata lock锁

    很多情况下,很多问题从理论上或者管理上而言都是可以避免或者说很好解决的,但是一旦涉及到现实由于管理或者协调或者规范执行的不够到位,就会出现各种各样本不该出现的问题,这些问题的通常在生产环境并不会出现, ...

  3. mysql=null的优雅解决方法

    对于不是采用所有字段都是not null的mysql表设计而言,mysql提供了一个<=>操作符. 在oracle中我们的处理方式通常类似: where a = #{var} or #{v ...

  4. emberjs初学记要

    code { margin: 0; padding: 0; white-space: pre; border: none; background: transparent; } code, pre t ...

  5. 关于JS闭包,作者不详(转)

    说明:本文由两篇文章结合而成,系从他人笔记中转过来的, 具体作者不详.因为觉得不错,遂共享之.如有侵权,立删致歉. 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变 ...

  6. ng-click

    使用ng-clcik代码是发现其内的a标签失效: 于是测试下,发现绑定在document上的click事件在点击ng-click绑定的元素上也会失效: <div ng-click="c ...

  7. Smartforms常见问题

    分类: 1.使用SFSY-FORMPAGES显示总页数的时候,如果页数大于9,将会在前10页显示成星号* 解决: 有时候这样做完之后,星号*是没有了,但是字体会有颠倒或者重叠的现象. 如果出了这个问题 ...

  8. SharePoint 2013 图像呈现形式介绍

    由于图像呈现形式依赖 SharePoint Server 2013 中的其他功能,因此需确保您满足本节中的先决条件,才能执行本文中的过程.先决条件包括: • 发布网站集 您要在其中添加图像呈现形式的网 ...

  9. English Training Material - 01

    Building a relationship Cross-cultural understanding Eye contact In many Western societies, includin ...

  10. eclipse 设置豆沙绿保护色,保护眼睛

    1.菜单栏 windows–>preferences–>general–>editors–>text editors中 在Appearance color options下修改 ...