Description

 

A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 1,2,3,...,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 <= 16)

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.

You are to write a program that completes above process.

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 【代码】:
/*素数环*/
#include <bits/stdc++.h>
using namespace std;
int n,prime[]={},v[],a[]; void get_prime()
{
int m=sqrt(*n+0.5);//环-倍增
memset(prime,,sizeof(prime));//0为素数
prime[]=prime[]=;//非素数
for(int i=;i<=m;i++)
{
if(prime[i])//素数
{
for(int j=i+i;j<=*n;j+=i)
{
prime[j]=;//非素数
}
}
}
} void dfs(int cur)
{
if(cur == n && prime[ a[]+a[n-] ])
{
for(int i=; i<n; i++)
{
printf("%d%c", a[i], i == n - ? '\n' : ' ');
}
// printf("\n");
}
else
{
for(int i=; i<=n; i++)
{
if( !v[i] && prime[ i + a[cur-] ] )
{
a[cur] = i;
v[i]=;
dfs(cur+);
v[i]=;
}
}
}
}
int main()
{
scanf("%d",&n);
//初始化
memset(v,,sizeof(v));
a[]=; get_prime();
dfs();
}

最新文章

  1. jQuery将悬停效果加到菜单项
  2. Python入门笔记(11):集合
  3. php 验证格式的函数总结
  4. zabbix自定义键值原理
  5. Vue.js之初印象
  6. Demo学习: Closable Tabs
  7. Extjs4.2布局——Ext.container.ViewportView
  8. HDU 5044 TREE
  9. Java提高学习之Object(3)
  10. AJAX JQuery 调用后台方法返回值(不刷新页面)
  11. 平均时间复杂度为O(nlogn)的排序算法
  12. asp.net2.0安全性(1)--用户角色篇(类)--转载来自车老师
  13. hibernate得知——Set设置配置
  14. 高晓松脱口秀--晓说(第一季&amp;第二季)mp3下载
  15. 以pfile或者spfile启动时show parameter pfile的不同结果
  16. SQL之LIMIT ,OFFSET
  17. Android APK反编译(二)
  18. iOS--LaunchImage启动页设置及问题解决
  19. 1900型USB接口扫描枪设置虚拟串口模式提升扫描速度
  20. position:fix相对父元素定位

热门文章

  1. git初次建立远程仓库问题
  2. 图学java基础篇之IO
  3. MySQL之Schema与数据类型优化
  4. Java Integer于Int 进行==双等于的内存比较时的一些问题说明
  5. IOS开发学习笔记012-核心语法
  6. spaCy 并行分词
  7. springcloud 高可用分布式配置中心
  8. 数组线性表ArrayList 和链表类LinkedList
  9. Spring MVC与jQuery结合使用Ajax技术
  10. Mysql实战之高可用HMA