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