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 ...
随机推荐
- Safari不能连接到服务器
系统偏好设置-网络-高级-代理,把"网页代理"和"安全网页代理"两个复选项去掉,最下面"使用被动FTP模式"复选项保留,即可解决!
- windows 下共享内存使用方法示例
windows下共享内存使用方法较 linux 而言微微复杂 示例实现的功能 有一个视频文件,一块内存区域 : 程序 A,将该视频写入该内存区域 : 程序 B,从该内存区域读取该视频 : 代码模块实现 ...
- hash_equals()函数
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/92 了解下hash_equals的概念: bool hash_e ...
- Foreign websites
[社交] 1.Twitter. It's what's happening. 2.Telegram Messenger 3.Facebook - Log In or Sign Up 4.Instagr ...
- [Upper case conversion ] 每个单词的首小写字母转换为对应的大写字母
Given a string , write a program to title case every first letter of words in string. Input:The firs ...
- phpexcel导出成绩表
效果图如下: 代码如下:代码注释不全,请大家参考phpexcel中文帮助手册 <?php require_once 'PHPExcel.php'; function bfb($n) { retu ...
- ES6 对象的扩展(上)
属性的简介表示法 允许直接写入变量和函数作为对象的属性和方法,这样的书写更简洁. function f( x, y ) { return { x, y }; } // 等同于 function f( ...
- locust 参数,数据详解
参数 说明-h, –help 查看帮助-H HOST, –host=HOST 指定被测试的主机,采用以格式:http://10.21.32.33–web-host=WEB_HOST ...
- php array_walk
PHP array_walk() 函数 对数组中的每个元素应用用户自定义函数: <?php function myfunction($value,$key) { echo "The k ...
- 454ITS数据按barcode和primer分类程序v1.0
不知道有什么好办法可以让primer允许漏配,现在仅仅是允许错配,还是有一些没有配上,454数据有些primer漏配了一些,下一步解决这个问题 #include <cstdio> #inc ...