[This is a rule?] Sequence 1

Source: Internet
Author: User

[Question]

If the difference between any two adjacent items of an integer Sequence starting with 0 is 1 or-1, then we say this integer sequence is 'sequence 1 '. More clearly, if the sequence [a1, a2 ,...... An] is a sequence that meets the following conditions.

1. For any k, 1 <= k <n, yes | Ak-ak + 1 | = 1

2. A1 = 0.

1. Read the length and total number of sequences from the input file.

2. Find a sequence with a given length so that its sum is equal to the given value.

3. Write the result to the output file.

[Input]

The first line of the input file is an integer N, 1 <= n <= 100,0000, indicating the length of the sequence. The second line contains an integer S, | S | <= 50000000, represents the sum of sequences.

[Output]

If such a sequence does not exist, 'ni' is output '. Otherwise, the number of N is output, indicating the sequence of n. The sum of N is equal to or equal to S.

[Example input]

8

4

[Sample output]

0

1

2

1

0

-1

0

1

[Analysis]

First, it is explained that there are multiple solutions to this question.

Very lazy ~ Stick to others' questions.

It is not difficult to think that the maximum and minimum values of N numbers are N * (n-1) Div 2 and-N * (n-1) div2 respectively. If max is set to the maximum value, the minimum value is-max. Obviously, if S is not within the range of-Max to Max, this question cannot be solved. And because the difference between two adjacent numbers is 1, the series should be even, odd, even, Odd ,...... In this arrangement, the parity of the sum of N numbers should be certain. If S and Max are not the same or odd, the problem cannot be solved.

Next we should consider how to construct a series. When N = 4, all the columns are arranged in the ascending order of sum as follows:

(01 2 3 ),

(01 2 1 ),

(01 0 1 ),

(0-1 0 1 ),

(0-1 0-1 ),

(0-1-2-1)

(0-1-2-3)

We can see that there are some rules between these series. Assume that (01 23) is the initial series, starting from the last number, and moving forward from the beginning to the end, the number is reduced to 2nd in the series, then, the number is reduced from the last number to 3rd in the number series. At last, the number of N in the sequence is reduced from the last number. In this way, the sum of values minus two at a time can generate all columns. This method can also be generalized to the general situation. Suppose there are n numbers in the series and the initial series is (, 2 ...... N), each round starts from the nth number, and each time a number is reduced by two to generate a new series. The I round is reduced to the I + 1 number in the series, n-1-1 round is reduced in total, so that all the series can be generated in the order of sum from large to small. After analysis, we can also prove that if the input integer S is within the range of-Max to Max and S and Max are of the same parity, this problem must be solved.

The problem is solved. Code or something, well. Great implementation ...... Ignore (I did not say nothing !!!).


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.