uva10025 the? 1? 2? ... ? n = k problem

Source: Internet
Author: User

10025 the? 1? 2? ... ? n = k 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 obtain k = A, the expression to be used'll be:
-1 + 2 + 3 + 4 + 5 + 6-7 = 12
with n = 7
Input
The first line was the number of test cases, followed by a blank line. Each test case of the input contains an integer k (0≤|k|≤1000000000). Each test case is separated by a.
Output for each test case, your program should print the minimal possible n (1≤n) to obtain K with the above formula. Print a blank line between the outputs for the consecutive test cases.
Sample Input
2
12
-3646397
Sample Output
7
2701
First preprocessing, and then find the problem given in the calculation results in the interval, because the use of a minus sign in the sequence can be combined with any number of even, so find the answer with the difference between the value of the smallest

#include <iostream>#include <algorithm>#include <map>#include <cstdio>#include <cstdlib>#include <vector>#include <cmath>#include <cstring>#include <string>using namespace STD;Const intmaxn=54723;typedef Long LongLL; LL SUM[MAXN];voidWork (LL N) {if(n==0){printf("3\n");return;}if(n<0) N=-n;intHigh=lower_bound (sum,sum+maxn,n)-sum;intAns=high; while((sum[ans]-n)%2!=0) ans++;printf("%d\n", ans);}intMain () {//#ifndef Online_judge//Freopen ("Text//in.txt", "R", stdin);//#endif//Online_judgesum[0]=0; for(intI=1; i<maxn;i++) {sum[i]=sum[i-1]+ (LL) i; }intTscanf("%d", &t); while(t--) {LL n;scanf("%lld", &n); Work (n);if(t>0)puts(""); }return 0;}/*sample input212-3646397sample output72701*/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

uva10025 the? 1? 2? ... ? n = k problem

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.