UVA524 素数环 Prime Ring Problem
题目OJ地址:
https://www.luogu.org/problemnew/show/UVA524
hdu oj 1016: https://vjudge.net/problem/HDU-1016
zoj 1457 :https://vjudge.net/problem/ZOJ-1457
题意翻译
输入正整数n,把整数1,2,...,n组成一个环,使得相邻两个整数之和均为素数。输出时,从整数1开始逆时针排列。同一个环恰好输出一次。.
多组数据,读入到EOF结束。
第i组数据输出前加上一行Case i:
相邻两组数据中间加上一个空行。
Prime Ring Problem
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.
Inputn (0 < n < 20).
OutputThe 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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int N;
int b[]={};
int total=,a[]={};
int search(int); //回溯过程
int print(); //输出方案
int pd(int x,int y); //判断素数x+y是否质数 int main()
{
int t=;
while(scanf("%d",&N)!=EOF)
{
a[]=;
for(int i=;i<;i++) a[i]=;
for(int i=;i<;i++) b[i]=;
t++;
printf("Case %d:\n",t);
search();
printf("\n");
}
//printf("%d\n",total); //输出总方案数
}
int search(int t)
{
int i;
for(i=;i<=N;i++) //有20个数可选
if((!b[i])&&pd(a[t-],i)) //判断与前一个数是否构成素数及该数是否可用
{
a[t]=i;
b[i]=;
if (t==N) { if(pd(a[N],a[])==) print();}
else search(t+);
b[i]=;
}
}
int print()
{
int j;
total++;
//printf("<%d>",total);
printf("%d",a[]);
for(j=;j<=N;j++)
printf(" %d",a[j]);
printf("\n");
}
int pd(int x,int y)
{
int k=,i=x+y;
while(k<=sqrt(i)&&i%k!=) k++;
if(k>sqrt(i)) return ;
else return ;
}
本题目分析见 https://www.cnblogs.com/huashanqingzhu/p/4747009.html
这里要注意:洛谷OJ的测试数据比较弱,n最大是16. hdu oj和uva oj原题的n是到20的。
UVA524 素数环 Prime Ring Problem的更多相关文章
- 洛谷UVA524 素数环 Prime Ring Problem
标签:搜索与回溯 题目: 从1到20这20个数摆成一个环,要求相邻的两个数的和是一个素数. 算法分析: 非常明显,这是一道回溯的题目.从1开始,每个空位有20种可能,只要填进去的数合法:与前面的数不相 ...
- 素数环 Primg Ring Problem
素数环 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/C 题意: 输入正整数n,把整数1~n组成一个 ...
- HDU - 1016 Prime Ring Problem 经典素数环
Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...
- Prime Ring Problem素数环(HDU1016)
Prime Ring Problem 思路:先看成一条链,往里头填数,满足任意相邻两数和为质数(这可以打表预处理出40以内的所有质数,扩展的时候枚举),填完了后检查首尾是否满足条件.字典序可以采用扩展 ...
- 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 ...
- Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black
Prime Ring Problem Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
- [HDU 1016]--Prime Ring Problem(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- Prime Ring Problem
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
随机推荐
- ssh centos 上传文件
ssh centos 上传文件命令(ftp开不起的情报况下): rz -be 下载文件: sz
- 谈谈《Dotnet core结合jquery的前后端加密解密密码密文传输的实现》一文中后端解密失败的原因
详情请看<Dotnet core结合jquery的前后端加密解密密码密文传输的实现>,正常来讲,这个博客里面的代码是没有问题的,但是我有时候却会直接报错,原因是后台解密失败:Interna ...
- C# DGVPrinter.cs 打印方法
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- BZOJ.5251.[八省联考2018]劈配mentor(最大流)
BZOJ 洛谷 对于每个人,每次枚举一个志愿看是否能增广即可. 对于第二问,可以保留第一问中\(n\)次增广前后的\(n\)张图,二分,在对应图上看是否能增广即可. 貌似匈牙利的某种写法比网络流优多了 ...
- python基础一 ------字符串的多种分隔符分隔
#-*-coding:utf-8-*-''' 字符串的切割 当需要的分隔符是一个是: s.split("分隔符") 当分隔符是多个时: s = "abcd,1 ...
- BZOJ2130 : 魔塔
考虑从$0$到$n$枚举$A$的通关楼层. 设$f[i]$表示$B$通关$i$层时$C$最多能得到多少金币,因为金币数非负,所以也可以看作最多通关多少层. 当$A$的通关楼层往上多$1$的时候,这把钥 ...
- Little Pony and Alohomora Part 3 [HihoCoder 1075]
描述 一日,崔克茜来到小马镇表演魔法. 其中有一个节目是开锁咒:舞台上有 n 个盒子,每个盒子中有一把钥匙,对于每个盒子而言有且仅有一把钥匙能打开它.初始时,崔克茜将会随机地选择 k 个盒子用魔法将它 ...
- 爬虫——request
命名规范 module_name,模块 package_name,包 ClassName,类 method_name,方法 ExceptionName,异常 function_name,函数 GLOB ...
- java.lang.IllegalArgumentException: Attribute 'items' is required and must be a Collection, an Array or a Map
很有可能是涉及items的时候写成了item导致此错
- stdio.h cstdio的区别
stdio.h cstdio string.h cstring math.h cmath .h是c语言的习惯,在c++中,替换为在前面加个c