https://vjudge.net/contest/67836#problem/E

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

时间复杂度:$O(n!)$

题解:dfs 要先判断 N ,如果 N 是奇数的情况下不可能构成素数环,而且如果不先排除会报超时

代码:

#include <bits/stdc++.h>
using namespace std; int N;
int vis[30];
int a[30]; int prime(int x) {
for(int i = 2; i * i <= x; i ++)
if(x % i == 0)
return 0;
return 1;
} void dfs(int step) {
if(step == N + 1 && prime(a[1] + a[N])) {
for(int i = 1; i <= N; i ++) {
printf("%d", a[i]);
printf("%s", i != N ? " " : "\n");
}
return ;
}
for(int i = 2; i <= N; i ++) {
if(vis[i] == 0) {
if(prime(i + a[step - 1])) {
vis[i] = 1;
a[step] = i;
dfs(step + 1);
vis[i] = 0;
}
}
}
return ;
} int main() {
int cnt = 0;
while(~scanf("%d", &N)) {
memset(vis, 0, sizeof(vis));
memset(a, 0, sizeof(a));
printf("Case %d:\n", ++cnt);
if(N % 2) {
printf("\n");
continue;
}
a[1] = 1;
vis[1] = 1;
dfs(2);
printf("\n");
}
return 0;
}

  

ZOJ 1457 E-Prime Ring Problem的更多相关文章

  1. ZOJ 1457 Prime Ring Problem(dfs+剪枝)

     Prime Ring Problem Time Limit: 10 Seconds      Memory Limit: 32768 KB A ring is compose of n circ ...

  2. UVA524 素数环 Prime Ring Problem

    题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016:  https://vjudge.net/problem/HDU-10 ...

  3. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...

  4. hdu 1016 Prime Ring Problem(DFS)

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

  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. 杭电oj 1016 Prime Ring Problem

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

  7. hdu 1016 Prime Ring Problem(深度优先搜索)

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

  8. HDU1016 Prime Ring Problem(DFS回溯)

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

  9. HDU 1016 Prime Ring Problem (DFS)

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

  10. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

随机推荐

  1. 爬虫-爬虫介绍及Scrapy简介

    在编写案例之前首先理解几个问题,1:什么是爬虫2:为什么说python是门友好的爬虫语言?3:选用哪种框架编写爬虫程序 一:什么是爬虫? 爬虫 webSpider 也称之为网络蜘蛛,是使用一段编写好的 ...

  2. C指针(2)——指针在函数中的应用(程序讲解)

    3-1.c指针用作函数参数 #include<stdio.h> typedef unsigned char uint8_t; //类型自定义,通过typedef语句重新把unsigned ...

  3. jQuery File Upload 文件上传插件使用一 (最小安装 基本版)

    jQuery File Upload 是一款非常强大的文件上传处理插件,支持多文件上传,拖拽上传,进度条,文件验证及图片音视频预览,跨域上传等等. 可以说你能想到的功能它都有.你没想到的功能它也有.. ...

  4. Docker - 容器中的tomcat如何使用startup.sh启动

    网上大多介绍的catalina.sh启动,因为docker容器中,无法直接启动startup.sh. 解决方法: 编辑catalina.sh,找到 >> "$CATALINA_O ...

  5. spl_autoload_register()函数

    一.__autoload 这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数.看下面例子: printit.class.php <?php class PRINTI ...

  6. http性能测试点滴

    WeTest 导读 在服务上线之前,性能测试必不可少.本文主要介绍性能测试的流程,需要关注的指标,性能测试工具apache bench的使用,以及常见的坑. 什么是性能测试 性能测试是通过自动化的测试 ...

  7. CSS随笔3

    1. CSS部分简洁使用 * background-radious:使得边框角“圆化”. * background:pink  url(“图片路径”)  no-repeat: * border 可以有 ...

  8. Linux命令应用大词典-第25章 备份与还原

    25.1 mkisofs:创建ISO9660/Joliet/hfs文件系统

  9. MaxScript代码补全插件

    MaxScript代码补全插件 作者Nik,原文发布于ScriptSpot 安装后max自带脚本编辑器会有自动补全,效果如下:

  10. mybatis interceptor 处理查询参数及查询结果

    拦截器:拦截update,query方法,处理查询参数及返回结果. /** * Created by windwant on 2017/1/12. */ @Intercepts({ @Signatur ...