#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. HDU 1078 FatMouse and Cheese (记忆化搜索+dp)

    详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...

  2. 用户home目录下的.gitconfig 和 库文件夹目录下的 .gitignore 示例

    .gitconfig文件: [user] name = hzh email = @qq.com [core] editor = vi quotepath = false [merge] tool = ...

  3. 机器学习笔记1——Introduction

    Introduction What is Machine Learning? Two definitions of Machine Learning are offered. Arthur Samue ...

  4. Ubuntu 14.04 配置 Java SE

    首先下载Java SE,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html: 下载后把压缩包拷贝到自定义的目 ...

  5. docker学习资料整理(持续更新中..)

    docker最近可以说火得一踏糊涂,跟 51大神在交流技术的时候这个东西会多次被提到,当我们还玩vm+linux/freebsd的时候,人家已经上升到更高层次了,这就是差距,感觉好高大上的样子,技术之 ...

  6. 蜗牛—JSP学习之JavaBean初识

    初识: <%@ page language="java" import="java.util.*" pageEncoding="utf-8&qu ...

  7. 编译C++,找不到头文件(fatal error: string: No such file or directory)

    在androidproject中编译C++时,找不到头文件,报错例如以下: fatal error: string: No such file or directory 解决该问题须要在Android ...

  8. [React] React Fundamentals: Precompile JSX

    The JSX Transformer library is not recommended for production use. Instead, you'll probably want to ...

  9. android 61 logcat

    package com.itheima.logcat; import android.os.Bundle; import android.app.Activity; import android.ut ...

  10. unix进程的环境--unix环境高级编程读书笔记

    http://blog.csdn.net/xiaocainiaoshangxiao/article/category/1800937