搜索专题: HDU1016Prime Ring Problem
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 52146 Accepted Submission(s): 23096
Note: the number of first circle should always be 1.
lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
6
8
Case 1:
1 4 3 2 5 6
1 6 5 2 3 4 Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2
Asia 1996, Shanghai (Mainland China)
RunId : 21243288 Language : G++ Author : hnustwanghe
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 20 + 5;
bool prime[N*3],used[N]={0};
int a[N],n;
void prime_factor(){
memset(prime,0,sizeof(prime));
prime[0] = prime[1] = 1;
for(int i=2;i<=N*2;i++)
if(!prime[i]){
for(int j=i*i;j<=N;j+=i)
prime[j] = 1;
}
}
void DFS(int cur){
if(cur > n){
if(!prime[a[1]+a[n]])
for(int i=1;i<=n;i++)
printf("%d%c",a[i],i==n?'\n':' ');
return;
}
for(int i=2;i<=n;i++){
if(!used[i]){
if(!prime[a[cur-1]+i]){
used[i] = true;
a[cur] = i;
DFS(cur+1);
used[i] = false;
}
}
}
}
int main(){
a[1] = 1;
prime_factor();
int cnt = 0;
while(scanf("%d",&n)==1){
printf("Case %d:\n",++cnt);
DFS(2);
printf("\n");
}
}
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 20 + 5;
bool prime[N*3],used[N]={0};
int a[N],n;
void prime_factor(){
memset(prime,0,sizeof(prime));
prime[0] = prime[1] = 1;
for(int i=2;i<=N*2;i++)
if(!prime[i]){
for(int j=i*i;j<=N;j+=i)
prime[j] = 1;
}
} void DFS(int cur){
if(cur > n){
if(!prime[a[1]+a[n]])
for(int i=1;i<=n;i++)
printf("%d%c",a[i],i==n?'\n':' ');
return;
}
for(int i=2;i<=n;i++){
if(!used[i]){
if(!prime[a[cur-1]+i]){
used[i] = true;
a[cur] = i;
DFS(cur+1);
used[i] = false;
}
}
}
} int main(){
a[1] = 1;
prime_factor();
int cnt = 0;
while(scanf("%d",&n)==1){
printf("Case %d:\n",++cnt);
DFS(2);
printf("\n");
}
}
最新文章
- Android技术积累:开发规范
- Android 抽屉效果的导航菜单实现
- 使用t-sql从身份证号中提取生日
- (十一)学习CSS之float属性
- C# 枚举
- [置顶] Objective-C ,ios,iphone开发基础:在UITextField输入完以后,隐藏键盘,
- mysql数据库学习(一)--基础
- 写移动端必备的meta标签
- SVG交互动画制作
- mysql用户权限管理
- 201521123111《Java程序设计》第8周学习总结
- js定时刷新页面.
- go语言之行--golang操作redis、mysql大全
- Python判断自定义的参数格式是否正确
- 记录PHP的执行时间
- 将对象序列化成XML字符串
- grep正则表达式搜索
- WebService生成XML文档时出错。不应是类型XXXX。使用XmlInclude或SoapInclude属性静态指定非已知的类型。
- AAC-ADTS
- centos7 制作yum源