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
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int is_prime(int n)
{
for(int k= 2; k*k <= n; k++)
if(n% k== 0) return 0;
return 1;
} int n, A[50], isp[50], vis[50];
void dfs(int cur)
{
if(cur == n && isp[A[0]+A[n-1]])
{
for(int i = 0; i < n; i++)
{
if(i != 0)
printf(" ");
printf("%d", A[i]);
}
printf("\n");
}
else
for(int i = 2; i <= n; i++)
if(!vis[i] && isp[i+A[cur-1]])
{
A[cur] = i;
vis[i] = 1;
dfs(cur+1);
vis[i] = 0;
}
} int main()
{
int kase = 0;
while(scanf("%d", &n) == 1 && n > 0)
{
if(kase > 0)
printf("\n");
printf("Case %d:\n", ++kase);
for(int i = 2; i <= n*2; i++)
isp[i] = is_prime(i);
memset(vis, 0, sizeof(vis));
A[0] = 1;
dfs(1);
}
return 0;
}

Program C 暴力求解的更多相关文章

  1. Program A - 暴力求解

    Description   Write a program that finds and displays all pairs of 5-digit numbers that between them ...

  2. Program L 暴力求解

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  3. Program B 暴力求解

    Given a sequence of integers S = {S1,S2,...,Sn}, you should determine what is the value of the maxim ...

  4. POJ 1562(L - 暴力求解、DFS)

    油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting ...

  5. 逆向暴力求解 538.D Weird Chess

    11.12.2018 逆向暴力求解 538.D Weird Chess New Point: 没有读好题 越界的情况无法判断,所以输出任何一种就可以 所以他给你的样例输出完全是误导 输出还搞错了~ 输 ...

  6. 隐型马尔科夫模型(HMM)向前算法实例讲解(暴力求解+代码实现)---盒子模型

    先来解释一下HMM的向前算法: 前向后向算法是前向算法和后向算法的统称,这两个算法都可以用来求HMM观测序列的概率.我们先来看看前向算法是如何求解这个问题的. 前向算法本质上属于动态规划的算法,也就是 ...

  7. BestCoder Round #79 (div.2)-jrMz and angles,,暴力求解~

    jrMz and angle       Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Other ...

  8. hdu6570Wave (暴力求解)

    Problem Description Avin is studying series. A series is called "wave" if the following co ...

  9. <字符串匹配>KMP算法为何比暴力求解的时间复杂度更低?

    str表示文本串,m表示模式串; str[i+j] 和 m[j] 是正在进行匹配的字符; KMP的时间复杂度是O(m+n)  ,  暴力求解的时间复杂度是O(m*n) KMP利用了B[0:j]和A[i ...

随机推荐

  1. maven install 报错Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project*****

    [ERROR]Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-co ...

  2. opencl gauss filter优化(三)

    1.根据前两次的最终结果: 使用普通buffer,Horizontal 5ms, Vertical 17 ms 使用image buffer:Horizontal 9.4ms, Vertical 6. ...

  3. 微信jssdk

    http://203.195.235.76/jssdk/?from=timeline&isappinstalled=0

  4. 通过yum安装nginx-mysql-php-fastcgi配置LNMP

    最近指想服务器跑静态文件,所以想单独配置个nginx的webserver,然而并不是我想象的那么简单,使用rpm包来安装会发生很多软件依赖的错误: 当我尝试使用yum安装nginx的时候,总是提示未找 ...

  5. CentOS7+JDK8编译Hadoop2.6.4

    1. 下载相关软件 apache-maven-3.3.1-bin.tar.gz protobuf-2.5.0.tar.gz hadoop-2.6.4-src.tar.gz 2.配置好jdk8环境(请看 ...

  6. js TAb

    有点乱,用到了自定义getByClass函数.sibling函数 <!DOCTYPE html> <html lang="en"> <head> ...

  7. linux下如何优雅的挂载一个外界设备(比如优盘)

    最近从事linux,实验室一个破服务器,能连上网,但是输入这样的命令:        yum -y install gcc yum -y install gcc-c++ ,居然说是没有这样的镜像,也罢 ...

  8. Longest Consecutive Sequence [LeetCode]

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. response.getWriter().write()与out.print()的区别

    1.首先介绍write()和print()方法的区别:  (1).write():仅支持输出字符类型数据,字符.字符数组.字符串等  (2).print():可以将各种类型(包括Object)的数据通 ...

  10. debug && release

    http://www.cnblogs.com/awpatp/archive/2009/11/05/1597038.html Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调 ...