素数环(C - 暴力求解)
素数环(暴力)(紫书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 - 暴力求解)的更多相关文章
- 素数环问题[XDU1010]
Problem 1010 - 素数环问题 Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 972 Acc ...
- Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black
Prime Ring Problem Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
- POJ 1562(L - 暴力求解、DFS)
油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting ...
- nyist 488 素数环(搜索+回溯)
素数环 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 有一个整数n,把从1到n的数字无反复的排列成环,且使每相邻两个数(包含首尾)的和都为素数,称为素数环. ...
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 【DFS】素数环问题
题目: 输入正整数n,对1-n进行排列,使得相邻两个数之和均为素数,输出时从整数1开始,逆时针排列.同一个环应恰好输出一次.n<=16 如输入: 6 输出: 1 4 3 2 5 6 1 6 5 ...
- 逆向暴力求解 538.D Weird Chess
11.12.2018 逆向暴力求解 538.D Weird Chess New Point: 没有读好题 越界的情况无法判断,所以输出任何一种就可以 所以他给你的样例输出完全是误导 输出还搞错了~ 输 ...
- HDU 1524 树上无环博弈 暴力SG
一个拓扑结构的图,给定n个棋的位置,每次可以沿边走,不能操作者输. 已经给出了拓扑图了,对于每个棋子找一遍SG最后SG和就行了. /** @Date : 2017-10-13 20:08:45 * @ ...
- 隐型马尔科夫模型(HMM)向前算法实例讲解(暴力求解+代码实现)---盒子模型
先来解释一下HMM的向前算法: 前向后向算法是前向算法和后向算法的统称,这两个算法都可以用来求HMM观测序列的概率.我们先来看看前向算法是如何求解这个问题的. 前向算法本质上属于动态规划的算法,也就是 ...
随机推荐
- 5.4.3 RegExp构造函数属性
RegExp构造函数包含一些属性(这些属性在其他语言中被看成是静态属性).这些属性适用于作用域中的所有正则表达式,并且基于所执行的最近一次正则表达式操作而变化.关于这些属性的另一个独特之处,就是可以通 ...
- php学习笔记(3)
1.计数器 <?php /* simple access counter for php3 (c)1998 David W. Bettis dbettis@eyeintegrated.com m ...
- A Byte of Python 笔记(11)异常:try..except、try..finally
第13章 异常 当你的程序中出现某些 异常的 状况的时候,异常就发生了. 错误 假如我们把 print 误拼为 Print,注意大写,这样 Python 会 引发 一个语法错误. 有一个SyntaxE ...
- [转]IOS Segment页面之间view的切换
有三个view,分别为view1.view2.view3,通过UISegmentedControl进行三个view的切换. @interface UIViewDemoViewController : ...
- JS 引用
var arr1=[1,2,3,4]; var arr2=arr1; arr2.push(5); console.log(arr1);//和arr2一样 console.log(arr1==arr2) ...
- 射频识别技术漫谈(7)——ID卡
ID(Identification)是识别的意思,ID卡就是识别卡.ID卡包含范围广泛,只要具有识别功能的卡片都可以叫ID卡,例如条码卡,磁卡都可以是ID卡,我们这儿说的当然是射频识别卡. 射频ID卡 ...
- 使用 Windows Media Center 远程控制
http://windows.microsoft.com/en-us/windows/getting-started-windows-media-center#getting-started-wind ...
- Windows Azure 网站 (WAWS) 中的服务器端包含 (SSI)
编辑人员注释:本文章由 Windows Azure 网站团队的项目经理Erez Benari 撰写. Windows Azure 网站客户普遍关心的一个问题是关于我们对服务器端包含(Server ...
- Apache 错误代码配置
ErrorDocument 400 /error_pages/400.htmlErrorDocument 401 /error_pages/401.htmlErrorDocument 403 /err ...
- Mybatis 简单的CRUD 基于XML文件配置
全部的ORM框架学习曲线都是先来一个CRUD爽一爽,以下我们就来CRUD一下,全部的配置都是基于上一篇的配置.废话不多说,直接上代码. <?xml version="1.0" ...