UVA - 524 Prime Ring Problem(素数环)(回溯法)
题意:输入n,把1~n组成个环,相邻两个数之和为素数。
分析:回溯法。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int prime[MAXN];
int a[MAXN];
int vis[MAXN];
int n;
void init(){
for(int i = ; i < MAXN; ++i) prime[i] = ;
for(int i = ; i < MAXN; ++i){
if(prime[i]){
for(int j = i + i; j < MAXN; j += i){
prime[j] = ;
}
}
}
}
void dfs(int cur){
if(cur == n && prime[a[n] + a[]]){
for(int i = ; i <= n; ++i){
if(i != ) printf(" ");
printf("%d", a[i]);
}
printf("\n");
}
else{
for(int i = ; i <= n; ++i){
if(!vis[i] && prime[a[cur] + i]){
vis[i] = ;
a[cur + ] = i;
dfs(cur + );
vis[i] = ;
}
}
}
}
int main(){
init();
a[] = ;
int kase = ;
while(scanf("%d", &n) == ){
if(kase) printf("\n");
printf("Case %d:\n", ++kase);
dfs();
}
return ;
}
UVA - 524 Prime Ring Problem(素数环)(回溯法)的更多相关文章
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- UVa 524 Prime Ring Problem(DFS , 回溯)
题意 把1到n这n个数以1为首位围成一圈 输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的 n最大为16 利用回溯法 边生成边推断 就要快非常多了 #inc ...
- 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——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
- 题目1459:Prime ring problem(素数环问题——递归算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- UVa 524 Prime Ring Problem(回溯法)
传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...
- UVa 524 Prime Ring Problem【回溯】
题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 #include< ...
- HDOJ 1016 Prime Ring Problem素数环【深搜】
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...
- UVa 524 - Prime Ring Problem
题目大意:输入正整数n,把整数1,2...,n组成一个环,使得相邻两个整数之和均为素数.输出时从整数1开始逆时针(题目中说的不是很明白??)排列.同一个环应恰好输出一次. 枚举,并在枚举每一个数是进行 ...
随机推荐
- 【PAT甲级】1024 Palindromic Number (25 分)
题意: 输入两个正整数N和K(N<=1e10,k<=100),求K次内N和N的反置相加能否得到一个回文数,输出这个数和最小的操作次数. trick: 1e10的数字相加100次可能达到1e ...
- leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字
1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...
- ImageMagick 将PDF转图片命令
将 pdf 转一张图片 PS C:\Users\Microestc\desktop> magick convert -density -quality .pdf -append .jpeg ro ...
- Serializable的理解和使用 -----转载
1.定义 这是一个接口,当一个类实现这个接口后,这个类就变成了一个可序列化的类,它就可以被写入流,保存起来,然后也可以用流读取,反序列化. 一般情况下,一个对象会随着程序的执行完成而消失,而有时我们需 ...
- 解题报告:luogu P1516 青蛙的约会
题目链接:P1516 青蛙的约会 考察拓欧与推式子\(qwq\). 题意翻译? 求满足 \[\begin{cases}md+x\equiv t\pmod{l}\\nd+y\equiv t\pmod{l ...
- spring mvc ,spring boot 整合swagger
https://blog.csdn.net/qq_35992900/article/details/81274436
- SpringMVC 转发、重定向
转发.重定向到其它业务方法 @org.springframework.stereotype.Controller @RequestMapping("/userController" ...
- 前端学习笔记系列一:9 js中数组的拷贝
拷贝分为浅拷贝和深拷贝,在JavaScript中能够实现这两种拷贝的方式也是多种多样.以下是一维数组实现深拷贝和浅拷贝的各种方式. 一.浅拷贝 1.赋值 赋值是最直接的一种浅拷贝. let arr3 ...
- Docker介绍(一)
在TES GLOBAL,我们已经爱上Docker并从Docker的0.8版本开始就在生产环境中使用它.我们的很多开发者都参加了在DockerCon欧洲上的培训.下面是我们总结的一些tips,希望可以帮 ...
- 题解 CF1131C 【Birthday】
CF大水题 题意:给你n个人,他们的身高是a[i],让你将这几个人排成一个环,使得他们两两之间身高差的和最小. 思路:简单到爆了,恶意评分上蓝.直接将那几个人排个序,然后按序左右放就行了,也就是说1号 ...