HDU1016(素数环)
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40069 Accepted Submission(s): 17675
Problem Description
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, …, n 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 < 20).
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. Print solutions in lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
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
[hdu1016](http://acm.hdu.edu.cn/showproblem.php?pid=1016)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cstring>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
int a[45], vis[45], vis1[45];
int n;
void isprime() {
vis[1] = 2;
for (int i = 2; i<=43; i++) {
if (!vis[i]) vis[i] = 1;
for (int j = 2*i; j<=43; j+=i) vis[j] = 2;
}
}
void dfs(int num) {
if (num == n && vis[a[0]+a[num-1]] == 1) {
for (int i = 0; i<num-1; i++) printf("%d ",a[i]);
printf("%d\n",a[num-1]);
}
else {
for (int i = 2; i<=n; i++) {
if (vis1[i] == 0) {
if (vis[i+a[num-1]] == 1) {
vis1[i] = 1;
a[num++] = i;
dfs(num);
num -- ; //回溯
vis1[i] = 0;
}
}
}
}
}
int main() {
int f = 1;
isprime();
//for (int i = 1; i<20; i++) cout << vis[i] << endl;
while (scanf("%d",&n) != EOF) {
printf("Case %d:\n",f++);
mem(vis1); mem(a);
a[0] = 1;
dfs(1);
printf("\n");
}
}
HDU1016(素数环)的更多相关文章
- HDU1016 素数环---(dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1016 Sample Input 6 8 Sample Output Case 1: 1 4 3 2 5 6 ...
- 素数环问题[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) ...
- 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 ...
- HDU 1016 Prime Ring Problem(素数环问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- 素数环 南阳acm488(回溯法)
素数环 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简 ...
- UVA 524 素数环 【dfs/回溯法】
Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers ...
- hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- LeetCode中的最大子串和问题(Maximum Subarray)
问题描述: Find the contiguous subarray within an array (containing at least one number) which has the la ...
- HTML篇(下·)
13.Label的作用是什么?是怎么用的? label标签来定义表单控制间的关系,当用户选择该标签时,浏览器会自动将焦点转到和标签相关的表单事件上. <label for="Name& ...
- 51Nod1203 2012集训队答辩 JZPLCM
A1339. JZPLCM(顾昱洲) 时间限制:3.0s 内存限制:256.0MB 试题来源 2012中国国家集训队命题答辩 问题描述 给定一长度为n的正整数序列a,有q次询问,每次询问一段区 ...
- bzoj 3669: [Noi2014] 魔法森林 LCT版
Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...
- bzoj 3207: 花神的嘲讽计划Ⅰ
Description 背景 花神是神,一大癖好就是嘲讽大J,举例如下: "哎你傻不傻的![hqz:大笨J]" "这道题又被J屎过了!!" "J这程序 ...
- 后缀数组之hihocoder 重复旋律1-4
蒟蒻知道今天才会打后缀数组,而且还是nlogn^2的...但基本上还是跑得过的: 重复旋律1: 二分答案,把height划分集合,height<mid就重新划分,这样保证了每个集合中的LCP&g ...
- 限制ssh远程登陆
超过十次,就添加到hosts.deny里面去 #!/bin/bash date=`date +%Y%m%d` file="/var/log/secure" max=10 if [[ ...
- C/C++调用Golang 二
C/C++调用Golang 二 <C/C++调用Golang 一>简单介绍了C/C++调用Golang的方法步骤,只涉及一个简单的函数调用.本文总结具体项目中的使用场景,将介绍三种较复杂的 ...
- kibana提示“Your Kibana index is out of date, reset it or use the X-Pack upgrade assistant.”
=============================================== 2017/12/15_第1次修改 ccb_warlock = ...
- ABP .Net Core 日志组件集成使用NLog
一.说明 NLog介绍和使用说明官网:http://nlog-project.org/ NLog和Log4net对比:https://www.cnblogs.com/qinjin/p/5134982. ...