rime Ring Problem

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.

输入

n (0 < n < 20).

输出

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.

样例输入

6
8

样例输出

Case 1:
1 4 3 2 5 6
1 6 5 2 3 4Case 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

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int prime[40]={0};
int dir[21],ring[21];
void pri()
{int i,j;prime[0]=prime[1]=1;for(i=2;i<=6;++i)for(j=i*i;j<40;j+=i)prime[j]=1;
}
int DFS(int x,int y)
{int i;if(x==y+1&&prime[ring[y]+ring[1]]==0){printf("1 ");for(i=2;i<y;++i)printf("%d ",ring[i]);printf("%d\n",ring[y]);return 0;}for(i=2;i<=y;++i){if(!dir[i]&&!prime[i+ring[x-1]]){dir[i]=1;ring[x]=i;DFS(x+1,y);dir[i]=0;}}
}int main()
{int T=1,n;pri();while(~scanf("%d",&n)){printf("Case %d:\n",T++);if(n==1)printf("1\n");else if(n&1)continue;else {memset(dir,0,sizeof(dir));dir[1]=ring[1]=1;DFS(2,n);}printf("\n");}return 0;
}

nyist 132Prime Ring Problem相关推荐

  1. HDOJ 1016 HDU 1016 Prime Ring Problem ACM 1016 IN HDU

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目描述: Prime Ring Problem Time Limit: 4000/2000 ...

  2. HDU1016 Prime Ring Problem dfs+回溯

    点击打开链接 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  3. UVA524 素数环 Prime Ring Problem

    UVA524 素数环 Prime Ring Problem - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/UVA52 ...

  4. UVA - 524:Prime Ring Problem

    Prime Ring Problem 来源:UVA 题目 A ring is composed of n (even number) circles as shown in diagram. Put ...

  5. UVA - 524 Prime Ring Problem

    题目链接: UVA - 524 Prime Ring Problem Description(素数环) A ring is composed of n (even number) circles as ...

  6. [HDOJ1016]Prime Ring Problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 原题: A ring is compose of n circles as shown in d ...

  7. 独木舟上的旅行http://acm.nyist.net/JudgeOnline/problem.php?pid=71

    独木舟上的旅行 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客的总 ...

  8. hdu 1016 Prime Ring Problem(DFS)

    本题链接:点击打开链接 本题大意: 输入一个数n表示须要查找的数有n个,而且这些数连成一个环,随意两个相邻的数之和都为素数. 解题思路: 就是从1開始对每一个点进行查找,符合条件的点就存到一个数组中, ...

  9. Triangular Sums http://acm.nyist.net/JudgeOnline/problem.php?pid=122

    Triangular Sums 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 The nth Triangular number, T(n) = 1 + - + n, ...

最新文章

  1. android4.3.0 camera,4.3、高通camera驱动简析
  2. 快上车,带你了解HiLens Studio六大优势
  3. maven中net.sf.json报错的解决方法
  4. 电子围栏判断_电子围栏大用处:进出罐箱指定区域自动通知amp;罐箱库存和使用率自动计算...
  5. Men and women can't be 'just friends
  6. Linux 系统安装配置PHP服务(源码安装)
  7. thinkpad扩展坞怎么用_顶级雷电3显卡扩展坞到底好用吗?技嘉 RTX2080Ti GAMING BOX测评...
  8. Centos 7 修改时区
  9. Linux下安装python及MySQLdb
  10. libFetion『Linux下都飞信』
  11. 如何看待网络直播中的弹幕文化?
  12. Python自动连接网络(自动登录网络准入系统)
  13. ▶ajax 完整参数
  14. 别傻傻分不清docker run 和 start 的区别了
  15. 数字先锋 | 天翼云牵手中能融合
  16. 查看oracle11g的企业管理器(OEM)
  17. qt 引发了异常: 写入访问权限冲突。this 是 0x7FF700000000。
  18. C 语言的fseek()
  19. S7-200SMART案例分析——运动控制编程
  20. [内核安全4]内核态Rootkit之IDT Hook

热门文章

  1. 这个中秋,来点不一样~
  2. 喜茶获美团点评旗下基金4亿元投资!它离行业独角兽还差多远?
  3. 【Maven实战】之生成项目站点
  4. 最常用最好记Linux命令
  5. SystemCenterOperationsManager2012 SP1系列文章
  6. 『mcse 2008基础架构』Chapter 01 IP协议及配置方法 第1课网络连接及配置方法
  7. Apache Log4j 学习笔记
  8. JAVA基础知识(3)
  9. a high quality start up vc in uk
  10. Research on NFT