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那里没有 ...
- mysql根据时间查询日期的优化
例如查询昨日新注册用户,写法有如下两种: EXPLAIN select * from chess_user u where DATE_FORMAT(u.register_time,'%Y-%m-%d' ...
- HDU-3790 最短路最小花费
判断路径相等时的情况 #include <iostream> #include <cstring> #include <algorithm> #include &l ...
- 《第一行代码》Android特色开发,基于位置服务,出现的问题
手机GPS定位较慢.精度高.耗电量多,网络定位较快.精度低.耗电量少 当位置精度要求非常高的时候,使用GPS定位:一般情况下,使用网络定位. 按<第一行代码>写了一个定位程序,真机一直没有 ...
- Flex和MyEclipse10整合时候需要注意的问题
1.myeclipse和flex的位数要一致,不能混着安装 2.独立安装完Adobe Flash Builder 4.6 Installer之后,在其的安装文件夹下有一个utilities文件夹下有一 ...
- 08_Spring自定义标签
[ 项目工程 ] [ Person.java 模型类 ] package com.spring.selfxml.model; /** * Created by HigginCui on 2018/9/ ...
- 13_Redis安全
[1.设置密码] [2.绑定IP] [3.命令禁止或重命名] [4.修改默认端口]
- qt MessageBOX 消息
void MessageBox::slotQuestion() { switch(QMessageBox::question(this,"Question",tr("It ...
- Linux 下载百度网盘大文件
Linux 下没有百度网盘客户端,用浏览器下载速度慢得急死人 鼠标移到链接处, 右键, 然后复制链接 接着在终端里输入 axel -n 50 -o filename.ext “粘贴链接到此处” axe ...
- Thread调用SaveFileDialog
public void ThreadSaveFileDialog(string sourceFileName) { Thread importThread = new Thread(new Param ...