Codeforces: B. Jumping Jack


The problem is from codeforces, http://www.codeforces.com/problemset/problem/11/B

11B Codeforces: B. Jumping Jack

Jack is originally standing at zero point. He can walk left or right at an increasing step. We suppose Jack always walks towards a direction. Therefore, with n steps, he can reach tex_6bab56ec1763fd1fbe6b267a9ee0f355 Codeforces: B. Jumping Jack

We denote the tex_77fba0109516dcfa4c8e4dd7c7d404e0 Codeforces: B. Jumping Jack. Replacing tex_965521b32fbf06b10d3a21ce6d9320dc Codeforces: B. Jumping Jack with tex_b2ceef57e01f1a3fba9db534d972421a Codeforces: B. Jumping Jack, so Jack can reach a new location of tex_af4b41db28f688ceefbbf1427a87f18a Codeforces: B. Jumping Jack, similarly tex_15fd3fb86d466a25b8194a401b84e954 Codeforces: B. Jumping Jack, tex_70a4b08796bf13d57b546827bfa5d301 Codeforces: B. Jumping Jack … can also be reached. An exhaustive search will be timed out in the test cases. The negative tests can be mirrored to the positive ones because the tests are symmetrical.

Therefore, the Python solution is given below.

#!/usr/bin/env python
# helloacm.com
n = int(raw_input())

def sum(x):
    return x * (x + 1) / 2

if n < 0:
    n = -n

ans = 0
while 1:
    s = sum(ans)
    if (s - n) % 2 > 0 or (s < n):
        ans += 1
    else:
        break

print ans

The sum() can also be defined as a lambda function in python as follows.

sum = lambda x: x * (x + 1) / 2

–EOF (The Ultimate Computing & Technology Blog) —

453 words
Last Post: Hello, world!
Next Post: Codeforces: 131A - cAPS lOCK

The Permanent URL is: Codeforces: B. Jumping Jack (AMP Version)

Leave a Reply