ZOJ-1457 prime ring problem 素数环 题解

ZOJ Problem Set - 1457
Prime Ring Problem
Time Limit: 10 Seconds      Memory Limit: 32768 KB

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)
还是DFS的问题,不过需要剪枝,否则TLE。。。说实话,当16输入进去,我自己数了十多个数,程序还没完成输出,到了OJ那里竟然AC!
当输入为奇数时是无解的。WHY?因为当输入n为奇数,若素数环存在,那么素数环上的所有素数(1也算上,虽然它不是,就当广义的==!)之和和必为奇数(奇数*奇数=奇数)。然而素数环上所有素数的和是相邻各个结点和数的二倍,也就是素数环上所有素数之和是偶数=》矛盾=》素数环不存在!
BESIDES,当前一个数为奇数时,他的下一个数只能是偶数。根据奇偶性,又剪掉一些枝条。
LAST BUT NOT LEAST,其实我们只求一般的解就可以,当我们顺时针求出一个解,将它逆时针输出便得到另一个解。不过随之而来的问题是排序。如果这样,要用基数排序(我没说错吧。。。)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值