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 ...
随机推荐
- IDEA创建Maven项目和子模块
一.新建Project a. 新建项目,点击Create New Project b. 选择使用maven来新建项目 选择新建Maven模块(对IDEA来说是项目) 如果Project SDK那里没有 ...
- 决策树之ID3算法
一.决策树之ID3算法简述 1976年-1986年,J.R.Quinlan给出ID3算法原型并进行了总结,确定了决策树学习的理论.这可以看做是决策树算法的起点.1993,Quinlan将ID3算法改进 ...
- PL/SQL: numeric or value error: character to number conversion error
在最简单的plsql块编程中出现这个错误,是因为 DBMS_OUTPUT.PUT_LINE('the x is '+x);这里面不能用“+”,而是要用“||” DECLARE x number; ; ...
- Hibernate 函数 ,子查询 和原生SQL查询
一. 函数 聚合函数:count(),avg(),sum(),min(),max() 例:(1)查询Dept表中的所有的记录条数. String hql=" select count(*) ...
- JavaScript的进阶之路(一)
JavaScript由ECMAScript BOM DOM三部分组成 ECMAScript重要版本1,3,5,6,提供核心语言功能 DOM提供访问和操作网页内容的方法和接口 BOM提供与浏览器交互的的 ...
- 2d动画开发之PIXI开发
简单的移动小游戏只要引入pixi.min.js就可以, 如果要用spine动画(龙骨也支持导出spine格式的)就要引入pixi-spine.js 如果还有声音的支持引入pixi-sound.js 学 ...
- SSRS 2008R2 执行Log 查询
1. 可以参考ExecutionLog3试图,此为系统安装Reporting Service自带的试图. 2. 可以使用以下语句查询: SELECT els.LogEntryId, els.Insta ...
- Office - Word 2013
1. 使用 Quick Parts 向Word中添加自定义属性: 2.
- SQL点点滴滴_修改数据库的兼容级别
语法 ALTER DATABASE database_name SET COMPATIBILITY_LEVEL = { 80 | 90 | 100 } 参数 database_name 要修改的数据库 ...
- [转] 你应该知道的应用UI动态设计规则
转自 CocoaChina http://www.cocoachina.com/macdev/uiue/2014/0505/8315.html 你应该知道的应用UI动态设计规则 这篇文章中,我主要阐述 ...