[Original] uvaoj water question 10025 solution report

Source: Internet
Author: User

First, the original question is changed from uvaoj.

The? 1? 2? ...? N = K Problem
The Problem

Given the following formula, one can set operators '+' or '-' instead of each '? ', In order to obtain a given K
? 1? 2? ...? N = K

For example: To obtainK = 12, The expression to be used will be:
-1 + 2 + 3 + 4 + 5 + 6-7 = 12 
WithN = 7

The input

The first line is the number of test cases, followed by a blank line.

Each test case of the input contains integer K (0 <=| K |<= 1000000000 ).

Each test case will be separated by a single line.

The output

For each test case, your program shocould print the minimal possible N (1 <= N) to obtain K with the above formula.

Print a blank line between the outputs for two consecutive test cases.

Sample Input

212-3646397

Sample output

72701

Alex gevak
September 15,200 0 (Revised 4-10-00, Antonio Sanchez)

 

 

Simply put, it is a natural number sequence from 1 to n. Each number can be filled with-and + before, and the final result must be equal to another given number K. The minimum n that can be reached to satisfy K is required here.

 

Analysis:

Of course, this question can be exhaustive, but n numbers need to be exhaustive for 2 ^ n times, that is, from 1 to n, which requires the number of geometric series to be exhaustive;

But it is easy to know that when the numbers 1 to N1 are all "+" and the sum is less than K, there is no solution. Therefore, it is necessary to start judgment when sum> K is connected.

If sum> K is set to Q = sum-K, K = sum-Q-> K = (1 + 2 + 3 .. + n)-Q. Here, we need to convert these formulas into the form of 1 + 2-3-4... + N. For 1 to n, if you want to change the positive number of a specific digit as the symbol, You need to subtract two times the number, and the number subtracted must be decomposed from Q. Therefore, there are the following inferences:

1. Q must be an even number.

2. When q/2 is greater than N, You need to determine whether Q is decomposed into the sum of X different even numbers.

In other words, when n is large enough, since any even number greater than 4 can be decomposed into the sum of X different even numbers, 2 corresponds to 1 and 4 corresponds to 2, we can ensure that, when q is an even number, n is the minimum value. When q is an odd number, Q cannot be split into the original natural number sequence, so n is not the solution at this time.

I want to prove the above assertions, But it proves that it has failed. Therefore, we can only return and find the maximum even number in the continuous addition of different even numbers with the result of Q.

For example, 16 = 2 + 4 + 10, and the maximum number of non-repeating even numbers is 10.

The Code is as follows:

Int fun (int x)

{

Int left = 2;
Int result = 0;
While (result = 0)
{
While (X-left <left)
{
If (left> = X)
{
Result = left;
Break;
}
Left + = 2;
}
X-= left;
Left + = 2;

}
Return result;

}

This function returns the maximum even number, which is equal to n. If it is less than N, the N of the even Q is the solution.

 

Below is all the code

# Include <stdio. h>
Int main ()
{
Int N;
Long long K;
Int I;
Int sum = 0;
Int result = 0;
Int flag = 0;
Int temp;
Int left;
Int right;
Scanf ("% d", & N );
While (n --)
{

Scanf ("% LLD", & K );
Sum = 0;
Flag = 0;
If (k <0)
K =-1 * K;
For (I = 1; sum <K; I ++)
Sum + = I;
I --;
If (sum = K)
{
Result = I;
}
If (k = 0)
Result = 3;
Else
{
While (1)
{
Temp = sum-K;
If (TEMP % 2! = 0)
{
I ++;
Sum + = I;
Continue;
}
Else
{

Int max = fun (k );

If (max <= N)
Break;
}
}
If (k) Result = I;
}
Printf ("% d \ n", result );
If (n) printf ("\ n ");
}
Return 0;
}

 

Previously, the condition in my while is n! = 0, there are n -- in the loop body, But wr cannot pass, and it will pass when it is changed to while (n --). It is a bit confusing...

For this blessing, three wrong answer were continuously refreshed...

 

Legacy problems:

Intuitively, Q is an even number, but there is no strict mathematical proof. Therefore, you can only select a scheme to determine the maximum number of even numbers and the number of numbers. First, 1 is added to N minus an even Q greater than 0. Therefore, different even numbers and all even numbers in a continuous and medium are decomposed to be less than N. At the same time, the maximum value is used to compare with N. If the number is greater than N, no matter how it is decomposed, it cannot be 1 ~ The corresponding value is found in the natural number sequence of N. The current N is the solution. If it is less than N, the current N is the solution.

[Original] uvaoj water question 10025 solution report

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.