Problem C: The Natural Series
The natural series, from 1 to n and rewinding, has a special character. If you add two values whose distance is 2, the sum can be divided by the middle value exactly. Let’s check a example “1 2 3 4 5 6” (and rewinding to 1): (1+3)%2==0; (2+4)%3==0; … ;(5+1)%6==0, and (6+2)%1==0. This character is unusual. You can’t find another series when n is even, and only one different series essentially when n is odd. For example, when n is 7, the other one is “1 2 3 7 4 5 6”. Here we relax the constraints. If the sum of two numbers is less than the middle value, this series can be accepted also. For example, “1 2 7 4 9 5 11 3 10 6 8” is true where 5+3<11.
Input
Each line of input has two numbers, the first is n (4<=n<=16). The next positive integer m (1<m<=n) is a reference that you must use it to be the second number in result series.
Output
Print each series under above conditions in one line in sequence. The first number must be 1, and second is the given reference. There is a blank between adjacent numbers in one line and no blank after the last number. Print a blank between each case and also no blank line after last case.
Sample Input
10 4
7 2
Sample Output
1 4 2 6 10 3 8 5 7 9
1 4 2 8 6 10 3 5 7 9
1 2 3 4 5 6 7
1 2 3 7 4 5 6
这是一道很简单的DFS,类似于JOJ的那道素数环的题。题目大意是,找到序列S(1,2,...,N)的一个排列S',使得S'[i]+S'[i+2] < S'[i+1] 或者 (S'[i] + S'[i+2] % S'[i+1]) == 0,因为N最大也就16,所以暴力枚举每一种情况,输出合理的情况即可。不过得注意这道题的输出格式,在每两个元素之间有空格,但是最后一个元素之后不能有空格,在最后一个CASE之后也不能输出空行。
(好久没有写这种DFS了...写得很丑)