#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<set>
using namespace std; int a[]={,}; //a[i]=1表示i已经使用过, a[i]=0表示i尚未使用过
int b[]={,}; //环中的数字,环中的第一个元素始终为1 set<vector<int> > ans; bool isprimenum(int m)
{
int t = (int)(sqrt(m*1.0));
for(int i=; i<=t; i++)
if(m%i==)
return false;
return true;
} void primeNumCircle(int i, int n)
{
if(i>n)
{
if(isprimenum(b[]+b[n]))
{
vector<int> ivec;
for(int k=;k<=n; k++)
ivec.push_back(b[k]);
ans.insert(ivec);
}
return;
}
for(int j=; j<=n; j++)
{
if(a[j]== && isprimenum(b[i-]+j))
{
a[j] = ;
b[i] = j;
primeNumCircle(i+, n);
a[j] = ;
b[i] = ;
}
}
} int main()
{
int n;
int count=;
while(cin>>n)
{
if(count>)
cout<<endl;
for(int i=; i<; i++)
a[i]=b[i]=;
a[]=b[]=; ans.clear();
primeNumCircle(,n); cout<<"Case "<<++count<<":"<<endl;
for(set<vector<int> >::iterator it=ans.begin(); it!=ans.end(); it++)
{
vector<int> temp = *it;
for(unsigned i=; i<temp.size(); i++)
cout<<temp[i]<<" ";
cout<<endl;
}
} return ;
}

Problem 1010 - 素数环问题的更多相关文章

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

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

  2. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  3. hdu1016 Prime Ring Problem【素数环问题(经典dfs)】

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

  4. UVA - 524 Prime Ring Problem(素数环)(回溯法)

    题意:输入n,把1~n组成个环,相邻两个数之和为素数. 分析:回溯法. #pragma comment(linker, "/STACK:102400000, 102400000") ...

  5. 素数环问题[XDU1010]

    Problem 1010 - 素数环问题 Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty: Total Submit: 972  Acc ...

  6. Hdu 1016 Prime Ring Problem (素数环经典dfs)

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

  7. nyist 488 素数环(搜索+回溯)

     素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 有一个整数n,把从1到n的数字无反复的排列成环,且使每相邻两个数(包含首尾)的和都为素数,称为素数环. ...

  8. 【DFS】素数环问题

    题目: 输入正整数n,对1-n进行排列,使得相邻两个数之和均为素数,输出时从整数1开始,逆时针排列.同一个环应恰好输出一次.n<=16 如输入: 6 输出: 1 4 3 2 5 6 1 6 5 ...

  9. 素数环 南阳acm488(回溯法)

    素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简 ...

随机推荐

  1. Netbeans代码配色主题大搜集

    我用netbeans的原因之一就是因为其支持代码配色主题,这是个对于一个代码配色字体控来说是非常重要的一个功能.理所当然的,我也会收集各种配色主题来时常换换口味. 下面就是我目前收集的Netbeans ...

  2. 从MSN上拔下来的全世界国家下拉框(附带SQL执行脚本)

    <select> <option value="AL">阿尔巴尼亚</option> <option value="DZ&quo ...

  3. LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  4. 经典SQL语句大全之数据开发

    数据开发 1.按姓氏笔画排序:Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as //从少到多 ...

  5. StirngUtil工具类 之 邮箱注冊 域名不区分大写和小写方法

    /** * 传入邮箱域名所有变为小写,然后拼接前缀返回 *<b>Summary: </b> * emailDomainTransform() * @param domain * ...

  6. hibernate.hbm.xml配置文件内容说明

    下面是一个自动生成的配置文件: <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PU ...

  7. QT皮肤系统的动态切换

    应用需求: 提供皮肤切换选项,在不重启应用程序的情况下实现皮肤的动态切换. 理论基础: 1) 图片资源是如何被利用的 这里先简要说明一下实现原理,皮肤的动态切换其关键在于图片资源的加载方式.QT中每个 ...

  8. Java Interview Reference Guide--reference

    Part 1 http://techmytalk.com/2014/01/24/java-interview-reference-guide-part-1/ Posted on January 24, ...

  9. JS实现图片宽高的等比缩放

    关于图片宽高的等比缩放,其实需求就是让图片自适应父容器的宽高,并且是等比缩放图片,使图片不变形. 例如,需要实现如下的效果: 要实现上面的效果,需要知道图片的宽高,父容器的宽高,然后计算缩放后的宽高. ...

  10. 转-C# 操作 Excel 常见问题收集和整理

    经常会有项目需要把表格导出为 Excel 文件,或者是导入一份 Excel 来操作,那么如何在 C# 中操作 Excel 文件成了一个最基本的问题. 做开发这几年来,陆陆续续也接触过这样的需求,但因为 ...