HDU 1016 Prime Ring Problem(素数环问题)
传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=1016
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 63806 Accepted Submission(s): 27457
Note: the number of first circle should always be 1.
You are to write a program that completes above process.
Print a blank line after each case.
8
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define max_v 105
int a[max_v];
int vis[max_v];
int n;
int isp(int x)//素数检测
{
for(int i=;i<=sqrt(x);i++)
{
if(x%i==)
return ;
}
return ;
}
void dfs(int cur)//素数环问题 全排列思想加素数的检测
{
if(cur==n&&isp(a[]+a[n-]))//判断到最后一个数了
{
printf("%d",a[]);//打印
for(int i=;i<n;i++)
{
printf(" %d",a[i]);
}
printf("\n");
return ;
}else
{
for(int i=;i<=n;i++)//找适合放在cur位置的i
{
if(!vis[i]&&isp(i+a[cur-]))//满足要求
{
a[cur]=i;//放入
vis[i]=;//标记
dfs(cur+);//搜索
vis[i]=;//回退
}
}
}
}
int main()
{
int t=,k=;
while(~scanf("%d",&n))
{
//if(k)
//printf("\n");
memset(vis,,sizeof(vis));//标记清空
a[]=;//确定1的位置
printf("Case %d:\n",t);
dfs();//从1开始放数
t++;
k++;
printf("\n");
}
return ;
}
HDU 1016 Prime Ring Problem(素数环问题)的更多相关文章
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDOJ 1016 Prime Ring Problem素数环【深搜】
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- [HDU 1016]--Prime Ring Problem(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU - 1016 Prime Ring Problem 经典素数环
Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...
- hdu 1016 Prime Ring Problem(dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1016 Prime Ring Problem(深度优先搜索)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- Spring学习笔记:Spring动态组装打印机
一.如何开发一个打印机 1.可灵活配置使用彩色魔盒或灰色魔盒 2.可灵活配置打印页面的大小 二.打印机功能的实现依赖于魔盒和纸张 三.步骤: 1.定义墨盒和纸张的接口标准 package cn.pri ...
- 互联网轻量级框架SSM-查缺补漏第七天(MyBatis的解析和运行原理)
第七章MyBatis的解析和运行原理 SqlSessionFactory是MyBatis的核心类之一,其最重要的功能就是提供创建MyBatis的核心借口SqlSession,所以要先创建SqlSess ...
- eclipse切换workspace后配置问题
正常情况下如果切换了eclipse的workspace后,需要重新配置eclipse,但是可以将原工作目录中的.metadata/.plugins/org.eclipse.core.runtime拷贝 ...
- Python中@修饰符的作用。
'@'符号用作函数修饰符是python2.4新增加的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.也就是说@A def f(): 是非法的. 只可以在模块或类定义层内对函数进行修饰, ...
- php explode时间分割
<?php $str = "2017-02-27 13:40:42"; $first=explode(' ',$str); $second=explode('-', $fir ...
- ECMA6所有知识点大概笔记
ECMAScript和JavaScript的关系是,前者是后者的规格,后者是前者的一种实现 初学者一开始学习JavaScript,其实就是在学3.0版的语法. -------------------- ...
- Android ImageButton单击切换按钮图片效果
正常状态的效果: 按钮按下的效果图片: 一.在java中为图片按钮增加触摸监听的函数来实现图片切换,代码如下: ImageButton btn = (ImageButton)findViewById( ...
- 十一、使用a标签打电话、发短信、发邮件
<a href="tel:400-888-6633">拨打电话<a> <a href="sms:19956321564">发 ...
- Spring返回json数据
第一种形式:使用注解@ResponseBody @RequestMapping(value = "/admin/jq", method = RequestMethod.GET) @ ...
- 工作总结:mvc分层架构
pojo:plain ordinary java object 简单无规则java对象,我个人觉得它和其他不是一个层面上的东西,VO和PO应该都属于它 po:persistant object 持久对 ...