Problem 1010 - 素数环问题
#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 - 素数环问题的更多相关文章
- 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 ...
- hdu1016 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(素数环)(回溯法)
题意:输入n,把1~n组成个环,相邻两个数之和为素数. 分析:回溯法. #pragma comment(linker, "/STACK:102400000, 102400000") ...
- 素数环问题[XDU1010]
Problem 1010 - 素数环问题 Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 972 Acc ...
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- nyist 488 素数环(搜索+回溯)
素数环 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 有一个整数n,把从1到n的数字无反复的排列成环,且使每相邻两个数(包含首尾)的和都为素数,称为素数环. ...
- 【DFS】素数环问题
题目: 输入正整数n,对1-n进行排列,使得相邻两个数之和均为素数,输出时从整数1开始,逆时针排列.同一个环应恰好输出一次.n<=16 如输入: 6 输出: 1 4 3 2 5 6 1 6 5 ...
- 素数环 南阳acm488(回溯法)
素数环 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简 ...
随机推荐
- Linux 新手常用命令
ubuntu的root用户默认是禁止的,需要手动打开才行 事实上ubuntu下的所有操作都用不到root用户,由于sudo的合理使用,避免了root用户下误操作而产生的毁灭性问题 root账号启用方法 ...
- Linux I2C设备驱动编写(三)-实例分析AM3359
TI-AM3359 I2C适配器实例分析 I2C Spec简述 特性: 兼容飞利浦I2C 2.1版本规格 支持标准模式(100K bits/s)和快速模式(400K bits/s) 多路接收.发送模式 ...
- CentOS 7下安装xampp和testlink
记录一下最近安装testlink的经历,供大伙儿参考,有问题可以留言讨论,这里就不截图了 先说下安装版本: CentOS-7.0-1406-x86_64-DVDxampp-linux-1.8.3-5- ...
- ASP.NET MVC- VIEW Creating Custom HTML Helpers Part 2
The goal of this tutorial is to demonstrate how you can create custom HTML Helpers that you can ...
- java数据结构之hash表
转自:http://www.cnblogs.com/dolphin0520/archive/2012/09/28/2700000.html Hash表也称散列表,也有直接译作哈希表,Hash表是一种特 ...
- C#面向对象基础:virtual方法,abstract方法,区别
virtual 关键字用于修饰方法.属性.索引器或事件声明,并使它们可以在派生类中被重写.默认情况下,类中的方法是非虚的,非虚的方法不能在子类中被覆盖(override),但是可以隐藏(new),但这 ...
- 将内容重定向到剪切板(clip.exe)
Add-Type -Assembly PresentationCore[Windows.Clipboard]::SetText("abc中文def")先用 $output | Ou ...
- 字符集详解 ------------------------ UNICODE +UTF8
http://my.oschina.net/goldenshaw/blog?catalog=3294521 http://my.oschina.net/goldenshaw/blog?catalog= ...
- LINUX系统全部参数 sysctl -a + 网络参数设置
http://blog.lifeibo.com/?p=380 1.sysctl sysctl命令被用于在内核运行时动态地修改内核的运行参数,可用的内核参数在目录/proc/sys中 [root@ser ...
- linux 系统调优2
换作Linux: 1.杀使用内存大,非必要的进程 2.增加连接数 3.磁盘分区的碎片整理 4.服务优化,把不要的服务关闭 5.更换性能更好的硬件,纵向升级 常见优化手段: 1.更换性能更好的硬件,纵 ...