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

Problem Description
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.



Note: the number of first circle should always be 1.




 
Input
n (0 < n < 20).
 
Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in
lexicographical order.



You are to write a program that completes above process.



Print a blank line after each case.
 
Sample Input
6
8
 
Sample Output
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
 
Source

Asia 1996, Shanghai (Mainland China)

Problem : 1016 ( Prime Ring Problem )     Judge Status : Accepted

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");
}
}

最新文章

  1. Android技术积累:开发规范
  2. Android 抽屉效果的导航菜单实现
  3. 使用t-sql从身份证号中提取生日
  4. (十一)学习CSS之float属性
  5. C# 枚举
  6. [置顶] Objective-C ,ios,iphone开发基础:在UITextField输入完以后,隐藏键盘,
  7. mysql数据库学习(一)--基础
  8. 写移动端必备的meta标签
  9. SVG交互动画制作
  10. mysql用户权限管理
  11. 201521123111《Java程序设计》第8周学习总结
  12. js定时刷新页面.
  13. go语言之行--golang操作redis、mysql大全
  14. Python判断自定义的参数格式是否正确
  15. 记录PHP的执行时间
  16. 将对象序列化成XML字符串
  17. grep正则表达式搜索
  18. WebService生成XML文档时出错。不应是类型XXXX。使用XmlInclude或SoapInclude属性静态指定非已知的类型。
  19. AAC-ADTS
  20. centos7 制作yum源

热门文章

  1. Windows 网络监测ping IP输出时间
  2. python中的类,对象,实例,继承,多态
  3. 这个 &#39;ip&#39; 竟然把我搞蒙圈了……
  4. select客户端模型封装——回调方式快速建立客户端
  5. codevs 1255 搭积木 x
  6. Linux vi/vim and linux yum 命令
  7. gitblit 数据迁移(复制)
  8. php缓冲区
  9. ACM ICPC 2011-2012 Northeastern European Regional Contest(NEERC)K Kingdom Roadmap
  10. 使用Map接收返回数据库的数据