POJ 1844 Sum [simple mathematics]

Source: Internet
Author: User

Time Limit: 1000 MS Memory Limit: 30000 K
Total Submissions: 9795 Accepted: 6406

Description

Consider the natural numbers from 1 to N. by associating to each number a sign (+ or-) and calculating the value of this expression we obtain a sum S. the problem is to determine for a given sum S the minimum number N for which we can obtain S by associating signs for all numbers between 1 to N.

For a given S, find out the minimum value N in order to obtain S according to the conditions of the problem.

Input

The only line contains in the first line a positive integer S (0 <S <= 100000) which represents the sum to be obtained.
Output

The output will contain in the minimum number N for which the sum S can be obtained.
Sample Input

12 Sample Output

7 Hint

The sum 12 can be obtained from at least 7 terms in the following way: 12 =-1 + 2 + 3 + 4 + 5 + 6-7.
Source

Romania OI 2002


Ideas:

It's a very simple mathematical question, but last night, even though it was almost a rule, it was not too weak.

Then, after the game, I spoke to TeilWall in the group and ran into tears...

 


Solution:

 


First define sum (I) = 1 + 2 +... + I = (I + 1) * I/2 [elementary school mathematics... (First item + last item) * Number of items/2 Understanding Orz]

Sum (I) is the maximum number I can determine.

 


1. For the number S of each input, we can find the I that meets the condition. It is certainly possible to enumerate I from sum (I)> = S.

Analysis: If sum (I) <S is connected, I is definitely not qualified.

PS: I used reverse push on the draft last night. The result was very complicated and I didn't notice that enumeration started from sum (I)> = S...

 


2. When we confirm I in step 1, there are two situations:

(1) Sum (I) = S, then obviously I is the answer, just output it directly.

(2) Sum (I)> S, starting from I, and going to the plus 1 in sequence, as long as (Sum (I)-S) % 2 = 0.

In fact, 0% 2 = 0, so the first type of situation can actually be classified into the second type.

Analysis:

Situation 1 is already very obvious. The following is mainly an analysis of situation 2...

Number that can be determined for each I:

Obviously, Sum (I ),

Then, replace + in sequence 1, 2, 3,... I with-, then the next number that can be determined must be smaller than the previous one.

For example, if I = 3, the number that can be determined is 6, 4, 2, (1)

I = 4, the number that can be determined is 10, 8, (6)

I = 5, the number that can be determined is 15, 13, 11, 9, 7, 5, (3)

Here is a question: how can we determine where the last number can end? If the number to be reached has been determined by the previous number, we don't need to move back.

This is because we have mentioned that we enumerate the I from the front to the back, so we don't have to consider where the deadline is.

Here we can see that:

For a number I, the maximum number it can determine is Sum (I ),

If the current I can determine the current S, Sum (I)-S must be a multiple of two. (Sum (I)-S) % 2 = 0]

 


Because we start to enumerate from the smallest possible I that meets the condition, there will be no earlier I that meets the condition but is less than the current I.

That is to say, once (Sum (I)-S) % 2 = 0, the output I can be used.

 


My thoughts during the competition:

 


At that time, I had drawn the following stuff on the draft paper...

 

 

 

 


But I did not think about enumeration... In addition, I did not see such an obvious conclusion, and then I introduced the following complex and silly formula...

 


If the number N at the end is an even number:

Then N can determine the number from (1 + N) * N/2 in turn minus two until greater than (1 + N-1) * (N-1)/2

If the number N at the end is an odd number:

Then the number of N can be determined from (1 + N) * N/2 in turn minus two knows greater than (1 + N-2) * (N-2)/2

....

Then, it was impossible to roll out a direct formula. I also thought of enumeration, but I didn't expect the start of the enumeration interval.

Then I think that this question is hopeless, and it has been more than half of the time, so I decided to write a simple greedy question... I have always thought that the search rule is still very good, and the result is stuck in this question.

 

Code:

# Include <stdio. h> const int maxn = 100000; int main () {int s; while (scanf ("% d", & s )! = EOF) {int sum = 0; int I; for (I = 1; I <maxn; I ++) // first find and <= your own minimum number {sum = (1 + I) * I/2; if (sum> = s) break;} int index = I; // record for (;) {sum = (1 + index) * index/2; if (sum-s) % 2 = 0) // search for it later, output {printf ("% d \ n", index); break;} index ++;} return 0 ;}

 

Related Article

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.