素数环

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/C

题意:

输入正整数n,把整数1~n组成一个环,使相邻的两个整数之和均为素数。输出时从整数1开始逆时针排列。

同一个环应恰好输出一次。n<=16

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 分析:
因为每个环对应一个排列,且排列总数高达2*10^13,为了防止超时用回溯法。
 #include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int vis[],a[];
int n;
bool is_prime(int m)
{ if(m<=) return true;
int y=(int)sqrt(m);
for(int j=;j<=y;j++)
if(m%j==) return false;
return true;
}
void dfs(int cur)
{
if(cur==n&&is_prime(a[]+a[n-])) //递归边界(测试第一个和最后一个数)
{
for(int j=;j<n-;j++)
cout<<a[j]<<' ';
cout<<a[n-]<<endl;
}
else
{
for(int i=;i<=n;i++)
{ if(!vis[i]&&is_prime(i+a[cur-])) //如果i没有用过,并且与前一个数之和为素数
{
a[cur]=i;
vis[i]=; //设置标志
dfs(cur+);
vis[i]=; //清除标志
}
}
}
}
int main()
{
int t=;
while(cin>>n)
{ memset(a,,sizeof(a));
memset(vis,,sizeof(vis));
a[]=;
if(t++) cout<<endl;
cout<<"Case "<<t<<':'<<endl;
dfs();
}
return ;
}
 

素数环 Primg Ring Problem的更多相关文章

  1. UVA524 素数环 Prime Ring Problem

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

  2. 洛谷UVA524 素数环 Prime Ring Problem

    标签:搜索与回溯 题目: 从1到20这20个数摆成一个环,要求相邻的两个数的和是一个素数. 算法分析: 非常明显,这是一道回溯的题目.从1开始,每个空位有20种可能,只要填进去的数合法:与前面的数不相 ...

  3. HDU - 1016 Prime Ring Problem 经典素数环

    Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

  4. 素数环:NYOJ--488--dfs||hdu-1016-Prime Ring Problem

    /* Name: NYOJ--488--素数环 Author: shen_渊 Date: 15/04/17 15:30 Description: DFS,素数打个表,37以内就够用了 */ #incl ...

  5. 1016-Prime Ring Problem,素数环,深搜!

    Prime Ring Problem                                                                                   ...

  6. Prime Ring Problem素数环(HDU1016)

    Prime Ring Problem 思路:先看成一条链,往里头填数,满足任意相邻两数和为质数(这可以打表预处理出40以内的所有质数,扩展的时候枚举),填完了后检查首尾是否满足条件.字典序可以采用扩展 ...

  7. uva 524 prime ring problem——yhx

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

  8. hdu 1016 Prime Ring Problem(DFS)

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

  9. Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black

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

随机推荐

  1. thinkphp 两表、三表联合查询

    //两表联合查询 $Model = M('T1');$Model->join('left join t2 on t1.cid = t2.id')->select();// $list = ...

  2. 腾讯微博的账号登录及api操作

    .tqq.php <?php /** * PHP Library for t.qq.com * * @author */ class tqqPHP { function __construct( ...

  3. STUN和TURN技术浅析

    转自:http://blog.csdn.net/yu_xiang/article/details/9227023 在现实Internet网络环境中,大多数计算机主机都位于防火墙或NAT之后,只有少部分 ...

  4. C语言函数的读写

    文件打开关闭函数:fopen()和fclose() <FILE *fopen(char *filename, char *mode)| int fclose(FILE *fp)> 字符读写 ...

  5. loj 1429(可相交的最小路径覆盖)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1429 思路:这道题还是比较麻烦的,对于求有向图的可相交的最小路径覆盖,首先要解决成环问 ...

  6. 权限管理AppOpsManager

    AppOps工具类 import android.annotation.TargetApi; import android.app.AppOpsManager; import android.cont ...

  7. Uva 679 Dropping Balls

    这道题如果模拟着来写,思路很简单 #include <iostream> #include <cstring> using namespace std; int T,D,I,c ...

  8. js:方法2. 字符串

    String.charAt()/String.charCodeAt() string.charAt(n); n:The index of the character that should be re ...

  9. 20145223《Java程序程序设计》第2周学习总结

    20145223 <Java程序设计>第2周学习总结 教材学习内容总结 一: 1.基本的类型: (1)整数:short(2字节).int(4字节).long(8字节) (2)字节 byte ...

  10. 【转】linux network namespace 学习

    原文地址:https://segmentfault.com/a/1190000004059167 介绍 在专业的网络世界中,经常使用到Virtual Routing and Forwarding(VR ...