2015 寒假搜索专题 K - Backward Digit Sums

来源:互联网 发布:最好的mac软件下载网站 编辑:程序博客网 时间:2024/05/01 15:54
K - Backward Digit Sums

Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

Description

FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4) might go like this:

    3   1   2   4  4   3   6  7   9  16
Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities.

Write a program to help FJ play the game and keep up with the cows.

Input

Line 1: Two space-separated integers: N and the final sum.

Output

Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.

Sample Input

4 16

Sample Output

3 1 2 4

Hint

Explanation of the sample:

There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

首先想到的是给出数字N(N <= 10)与它到1的排列组合。
用dfs把这些数字排列组合保存在数组a中。
每一次的排列组合,用这些数据进行题目意思的相加,得出结果相同就保存,输出这个数组a。

一开始的dfs是从大往下搜,导致后面的数据不符合题目要求。
后来想用个if把整个数组镜像反过来,以为可以水过去,但是结果是又WA了
一次。。。这种思想以后要努力改正。。。
后面是把dfs改成从小往大搜,这样就保证了符合题意的情况下在数组前面的都是较小的那个数。然后就AC了。。。

------------------------------------------------

code:

 
0 0
原创粉丝点击