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

 
 
 
这道题是输入n,让你找从1到n这些整数组成的 相邻的数的和为素数的环有几个,从1开始顺时针输出
 
注意题目要求输出一行Case n:(看清这个冒号啊~~~~~~~~当时没看到一直wr),而且Print a blank line after each case. ,(也没看清这个,真是惨啊~~~~)
减小复杂度的技巧:题中出现的最大素数为37,所以不必每种情况都再算一次相邻的数的和是不是素数,这样可能超时,而且dfs本来复杂度就挺高的,可以用pri[i]表记i是否为素数,
 

#include <cstdio>
#include <string.h>
#include <math.h>
int num[], t, n, now; //num记录最后选中的数
bool vis[], pri[];//pri[i] = 1表示i为素数,pri[i] = 0表示i不是素数
bool prime(int y) //bool类型的函数,这样就不用定义变量作为返回值了
{
for(int i = ; i <= sqrt(y); i++)
{
if(y % i == )
return false;
}
return true;
}
void dfs(int now, int t) //已经找到t个数了,第t个数为now,也就是当前数为now,进入dfs也就是找第t+1个数
{
if(t == n) //该找第n个数时,看看第n个数和第一个数相加是否是素数,因为组成的是个环,ps:一定记得!!!
{
if(pri[now+])
{
printf("");
for(int i = ; i <= n; i++)
printf(" %d", num[i]);
printf("\n");
}
return;
} for(int i = ; i <= n; i++)
{
if(vis[i] && pri[i+now])
{
vis[i] = ;
num[t+] = i;
dfs(i, t+);
vis[i] = ;
}
} }
int main()
{
int cnt = ;
while(~scanf("%d", &n))
{
memset(vis, , sizeof(vis));
for(int i = ; i < ; i++)
{
if(prime(i))
pri[i] = ;
else
pri[i] = ;
}
printf("Case %d:\n", cnt++); //当时忘了冒号!!!!!!!!!!!!!!!!!!!!!!!!!!惨啊~~~~
num[]=; vis[] = ;
dfs(, );
printf("\n");//当时也忘了每个案例之间也要输出空行~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!!!!!!!!嗷~~~
}
return ;
}
 

A - Prime Ring Problem(素数环,深搜,打表)的更多相关文章

  1. Prime Ring Problem素数环(HDU1016)

    Prime Ring Problem 思路:先看成一条链,往里头填数,满足任意相邻两数和为质数(这可以打表预处理出40以内的所有质数,扩展的时候枚举),填完了后检查首尾是否满足条件.字典序可以采用扩展 ...

  2. hdu 1016 Prime Ring Problem (素数环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目大意:输入一个n,环从一开始到n,相邻两个数相加为素数. #include <iost ...

  3. 「UVA524」 Prime Ring Problem 质数环

    Description 输入正整数n,把整数1,2,-,n组成一个环,使得相邻两个整数之和均为素数.输出时,从整数1开始逆时针排列.同一个环恰好输出一次.n<=16. A ring is com ...

  4. HDU - 1016 Prime Ring Problem 经典素数环

    Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

  5. UVA524 素数环 Prime Ring Problem

    题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016:  https://vjudge.net/problem/HDU-10 ...

  6. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...

  7. hdu 1016 Prime Ring Problem(DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black

    Prime Ring Problem Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

  9. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  10. Prime Ring Problem

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

随机推荐

  1. CTRL+A, CTRL+C, CTRL+V

    (http://leetcode.com/2011/01/ctrla-ctrlc-ctrlv.html) Imagine you have a special keyboard with the fo ...

  2. BZOJ 2064: 分裂( 状压dp )

    n1+n2次一定可以满足..然后假如之前土地集合S1的子集subs1和之后土地集合S2的子集subs2相等的话...那么就少了2个+操作...所以最后答案就是n1+n2-少掉的最多操作数, 由状压dp ...

  3. linxu添加定时任务

    1. 在需要定时执行的php文件的第一行加 也就是文件开头加  #! /bin/local/php -q 其中 /bin/local/php 是自己的php.exe 所在的位置 2. 上传要定时执行的 ...

  4. 64位系统未注册"MSDAORA.1"提供程序

    原因:如错误,64位系统未注册"MSDAORA.1"提供程序 解决:在IIS应用程序池中找到自己的网站,打开高级设置,设置“启用32位应用程序”为“True”即可. 另外还有其他解 ...

  5. 利用bind搭建dns

    下载bind,我下载的是bind-9.3.1rc1.tar.gz 我下载的文件放在/root目录下 进入目录解压缩 [root@linux root]#tar xfz bind-9.3.1rc1.ta ...

  6. JAVA GUI学习 - JTable表格组件学习_A ***

    public class JTableKnow_A extends JFrame { public JTableKnow_A() { this.setBounds(300, 100, 400, 300 ...

  7. 一个异或加密方案--C语言实现

    核心代码: char encrypt( char f , char c) { return f^c; } int OutEncrypt( char *FilePath, char *SecretWor ...

  8. Android 建造者(Builder)模式

    关于 Builder 模式 详述:http://blog.csdn.net/jjwwmlp456/article/details/39890699 先来张图 看到 Android  中 使用了 Bui ...

  9. 不可不知的HTML优化技巧

    如何提升Web页面的性能,很多开发人员从多个方面来下手如JavaScript.图像优化.服务器配置,文件压缩或是调整CSS. 很显然HTML 已经达到了一个瓶颈,尽管它是开发Web 界面必备的核心语言 ...

  10. 纯CSS写九宫格样式,高宽自适应正方形

    <!DOCTYPE html><html>    <head>        <meta charset="utf-8">      ...