Poj 1844 sum

Source: Internet
Author: User
// If you carefully analyze this question, the result will come out! Question: Give a number. The number is obtained through the operation of the continuous number from 1 to n. // (the symbol between two numbers can only be + or-), and the minimum N is obtained. // Calculate the first n sum from 1 to n first. If the sum is smaller than the num number, it cannot be satisfied. Therefore, add the sum! If it can be equal to num, the result is obtained and can be output! // If the number of sums equal to num cannot be found, it must be greater than the number of num. Only by changing the symbol between the number and the number (that is, replacing it with the-number) to get this number! // You can use a specific example: num = 12. When 1 + 2 + 3 + 4 + 5 is 15, 15-12 = 3 is used, but we need to find a number * 2 is equal to 3 between 1 and 5 (Why multiply by 2? Because the number is previously added, You need to subtract this number from this number series. Of course, you need to subtract this number twice.) // if the number is not met, continue to add: 1 + 2 + 3 + 4 + 5 + 6 = 21, and 21-12 = 9, which is the same as the preceding situation, 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28, while 28-12 = 16. If yes, the output can be obtained because the minimum N value is obtained! // The following is the explanation on the Forum, which is clearer than I do:/* I also understood it after reading the discussion from all of you, I wrote it more specifically. 1. sum must be greater than or equal to the input S. (if it is equal to or equal to, the answer has been found.) If it is less than, the number of addition operations cannot reach S. 2: when the first entry is met, note that the first encounter (Sum-S) % 2 = 0 after the first entry is met here (sum = 1 + 2 + .... + I) Then I is the answer. Proof: 1: If RES is an odd number, it indicates res = (1 + 2 +... + I)-S is an odd number, that is, no matter how you change sum (sum = 1 + 2 + .... + I) expressions (of course, by changing the plus sign to minus sign) cannot make res 0. For example: S = 5, sum = 1 + 2 + 3 = 6, Res = 6-5 = 1; no matter the plus sign in (1 + 2 + 3) is changed, this is because when you change a plus sign in sum to a minus sign, its value must be reduced by an even number (which is obvious) and sum-S is still odd 2: if res = sum-S is used, Res must be 0, 2, 4, 6 .... in the following example, we can change the plus signs in the sum expression to the minus sign so that res is 0 when k = 0, suppose 2 k indicates res obviously k = 1 2 3 4... when k = 1, sum (sum = 1 + 2 +... + I) to (sum =-1 + 2 +... + I) when k = 2, sum (sum = 1 + 2 +... + I) to (sum = 1-2 +... + I) Likewise, REs can always be 0 */# include <iostream> using namespace STD; int main () {int num, I, TMP, sum = 0; cin> num; for (I = 1; I ++) {sum + = I; TMP = sum-num; if (TMP> = 0 & TMP % 2 = 0) {cout <I <Endl; break ;}} system ("pause ");}

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.