素数环(暴力)(紫书194页)

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))
-->

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 题意:
输入正整数n,把1—n组成一个环,使相邻的两个整数为素数。输出时从整数1开始,逆时针排列。恰好能构成一个环输出一次,n (0 < n <= 16) 分析:
每个环对应于1——n的一个排列,但排列总数高达16!=2*10^13,普通写法会超时,应用回溯法。
DFS。深度优先遍历。 代码:
 #include<cstdio>
#include<iostream>
using namespace std; int n, A[], isp[], vis[]; int is(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]);
printf("\n");
}
else for(int i=;i<=n;i++)
if(!vis[i]&&isp[i+A[cur-]])
{
A[cur] = i;
vis[i] = ;
dfs(cur+);
vis[i] = ;
}
} int main()
{
//int n;
int m=;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n*;i++)
isp[i] = is(i);
memset(vis, , sizeof(vis));
A[] = ;
m++;
printf("Case %d:\n",m);
dfs();
printf("\n");
}
return ;
}

素数环(C - 暴力求解)的更多相关文章

  1. 素数环问题[XDU1010]

    Problem 1010 - 素数环问题 Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty: Total Submit: 972  Acc ...

  2. Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black

    Prime Ring Problem Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

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

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

  4. nyist 488 素数环(搜索+回溯)

     素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 有一个整数n,把从1到n的数字无反复的排列成环,且使每相邻两个数(包含首尾)的和都为素数,称为素数环. ...

  5. Hdu 1016 Prime Ring Problem (素数环经典dfs)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. 【DFS】素数环问题

    题目: 输入正整数n,对1-n进行排列,使得相邻两个数之和均为素数,输出时从整数1开始,逆时针排列.同一个环应恰好输出一次.n<=16 如输入: 6 输出: 1 4 3 2 5 6 1 6 5 ...

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

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

  8. HDU 1524 树上无环博弈 暴力SG

    一个拓扑结构的图,给定n个棋的位置,每次可以沿边走,不能操作者输. 已经给出了拓扑图了,对于每个棋子找一遍SG最后SG和就行了. /** @Date : 2017-10-13 20:08:45 * @ ...

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

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

随机推荐

  1. html5 geolocation API

    清单 1. 检查浏览器支持性if (navigator.geolocation) 清单 2. 单次定位请求 API void getCurrentPosition(updateLocation, op ...

  2. bzoj 1042: [HAOI2008]硬币购物 dp+容斥原理

    题目链接 1042: [HAOI2008]硬币购物 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1706  Solved: 985[Submit][ ...

  3. linux用户管理最常用的三个文件说明(不完整版)

    涉及到三个文本文件:/etc/passwd /etc/shadow /etc/group 文件相关: /etc/passwd和用户名相关 /etc/shadow和密码相关 /etc/group和用户所 ...

  4. w3wp.exe CPU过百问题

    w3wp.exe CPU过百问题 最近发布在windows  server2012  IIS8.0上的一个WebAPI项目,才几十个人在线,CPU就会出现过百情况,并且CPU一旦过百应用程序池就自动暂 ...

  5. shouldOverrideUrlLoading相关说明

    给WebView加一个事件监听对象(WebViewClient)并重写其中的一些方法:shouldOverrideUrlLoading:对网页中超链接按钮的响应.当按下某个连接时WebViewClie ...

  6. perl5 第二章 简单变量

    第二章 简单变量  by flamephoenix 一.整型 二.浮点数 三.字符串 基本上,简单变量就是一个数据单元,这个单元可以是数字或字符串.一.整型 1.整型   PERL最常用的简单变量,由 ...

  7. oracle数据类型和对应的java类型

    由于 实体类里面 使用的是 double ,生成的Oracle  用的JDBC 类型为java.sql.Types.FLOAT 所以Oracle 数据类型为 float. 如果想保留两位小数 实体类 ...

  8. paip.最新的c++ qt5.1.1环境搭建跟hello world

    paip.最新的c++ qt5.1.1环境搭建跟hello world 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://bl ...

  9. windows下C语言调用系统文件选择对话框

    代码片段,在windows下用C语言调用文件选择对话框,以备忘 #define DEFAULT_DIR "" char extraction_path[MAX_PATH] = DE ...

  10. CSS3属性值之box-shadow

    语法:   box-shadow:inset x-offset y-offset blur-radius spread-radius color 也就是:   对象选择器 {box-shadow:投影 ...