poj1844

maksyuki 发表于 oj 分类,标签: ,
0

Sum

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 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

 

题目类型:线性DP

算法分析:dp[i][j]表示前1~i的数字通过加减运算是否能够表示成数字j,由于j可能会有负值,所以需要先加上一个偏移量,初始化dp[1][“0”+-1] = true,状态转移方程为dp[i][j] = dp[i-1][j-i] + dp[i-1][j+i],可以使用奇偶滚动数组来优化空间,写成“我为人人型”会TLE

 

题目类型:同余计算

算法分析:若所有的操作都是累加操作则最后的和是(1 + n) * n / 2,若还存在减法操作则记累减的和为s,则易知有(1 + n) * n / 2 - 2 * s = S,则可推得(1 + n) * n / 2和S关于2同余,则最后枚举n并判断即可