1653: [usaco Feb] Backward digit sumstime limit: 5 sec memory limit: 64 MB
Submit: 207 solved: 161
[Submit] [Status] [discuss] Descriptionfj 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
Output details:
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4
Is the lexicographically smallest.
Hint Source
Silver
Question:
It is necessary to arrange the brute-force enumeration, so let's see if we can check whether an arrangement after the operations by question and whether it is a final number within the O (n) time.
There should be a relationship between the number of times each number is used, as shown in the following example:
16
7 9
4 3 6
3 1 2 4
Their number triangles are:
1
1 1
1 2 1
1 3 3 1
This should be obvious. The usage of the following numbers is equal to the sum of the usage of the two numbers in their oblique region. Isn't this the favorite Yang Hui triangle? The combination of the numbers is done.
Code:
Waiting for UPD
Bzoj1653: [usaco 2006 Feb] Backward digit sums