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. openfire消息通知推送

    package cn.zsmy.utils.openfire; import java.io.BufferedReader; import java.io.InputStreamReader; imp ...

  2. Android_SDK的常用命令

    一.配置环境变量 要想使用这些命令,就必须先配置环境变量.  将android-sdk-windows目录下的platform-tools目录和tools目录配置到path环境变量中 二.adb命令 ...

  3. OpenGL的gluPerspective透视投影变换函数详解[转]

    函数原型void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) 首先得设置gluPersp ...

  4. PacBio全基因组测序和组装

    PacBio公司的业务范围也就5个(官网): Whole Genome Sequencing Targeted Sequencing Complex Populations RNA Sequencin ...

  5. 【转】loading 三种实现方式

    转载:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=76872 一.通过动画实现 定义res/anim/loading.xml如 ...

  6. java 反射机制的实例

    [案例1]通过一个对象获得完整的包名和类名 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package Reflect;   /**  * 通过一个对象获得完整的包名和类名 ...

  7. dede在线留言

    登录dede后台,在[核心]---[频道维护]---[自定义表单]中根据需要创建需要的表单.   点击[增加新的自定义表单],添加在线留言表单.确定即可. 注意: ①在这里只需要修改[自定义表单名称: ...

  8. VirtualBox相关问题总结

    欢迎关注我的社交账号: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://gith ...

  9. C++编译错误cannot have cv-qualifier

    C++编译错误cannot have cv-qualifier 在C++中CV指const和volatile两个关键字.有两种情况不能使用CV限定. 一.非成员函数不能含有CV限定,即const和vo ...

  10. 显示段落p中的前半部分内容 剩下的用三个点代替,点击更多时显示所有内容

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...