ZOJ 1457 E-Prime Ring Problem
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的更多相关文章
- ZOJ 1457 Prime Ring Problem(dfs+剪枝)
Prime Ring Problem Time Limit: 10 Seconds Memory Limit: 32768 KB A ring is compose of n circ ...
- UVA524 素数环 Prime Ring Problem
题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016: https://vjudge.net/problem/HDU-10 ...
- uva 524 prime ring problem——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 杭电oj 1016 Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1016 Prime Ring Problem(深度优先搜索)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU1016 Prime Ring Problem(DFS回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1016 Prime Ring Problem (DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
随机推荐
- Spark Streaming job的生成及数据清理总结
关于这次总结还是要从一个bug说起....... 场景描述:项目的基本处理流程为:从文件系统读取每隔一分钟上传的日志并由Spark Streaming进行计算消费,最后将结果写入InfluxDB中,然 ...
- python--模块之sys与python解释器交互模块
作用:sys模块是与python解释器交互的一个接口.它提供了一系列有关python运行环境的变量和函数. 常用函数:import sys sys.argv #命令行参数list,第一个元素是程序本身 ...
- Python学习:5.函数以及基础语句
函数 一.创建函数 Python的函数的学习是Python基础的一个重要部分,函数可以重复利用,减少代码量,接下来我们就学习一下如何创建一个函数,以及函数的使用. 1.创建一个函数的基本格式 def ...
- python代理爬取存入csv文件
爬取高匿代理 from urllib import request import re import time f = open('西1.csv','w',encoding='GBK') header ...
- 使用bison和yacc制作脚本语言(2)
我们先来想一下语法 一般脚本语言不需要定义类型直接在赋值的时候确定 我们主要考虑一下变量的类型 a = 1; b = 1.1; c = "str"; 一般来讲,我们使用这三种类型, ...
- 高斯消元c++(非常暴力)
暴力方法(已更新): #include<iostream> using namespace std; const int maxn = 1000; int n; double a[maxn ...
- python--函数汇总
函数: 定义和特性: 定义:函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名()即可 特性:1,代码重用2,保持一致性3,可扩展性 函数的创建: 一,格式:p ...
- APP如何发布到Google play 商店
APP如何发布到Google play 商店?以及有哪些需要注意的点 2015-05-13 10:07 19773人阅读 评论(1) 收藏 举报 分类: iPhone游戏开发(330) 链接:ht ...
- 20145234黄斐《网络对抗技术》实验一,逆向及Bof基础实践
实践内容 本次实践的对象是一个名为hf20145234的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段, ...
- 2017Noip普及组游记
Day0 一天都基本在休息,早上信心赛,大家都是400整. 下午一群人窝在教室里打三国杀. Day1:Before Contest 早上大约十点到了试场,在考提高组,不能进. 喝了一杯咖啡去除早起的身 ...