Codeforces: B. Jumping Jack


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

11B Codeforces: B. Jumping Jack algorithms beginner brute force code code library codeforces implementation math programming languages python tricks

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 algorithms beginner brute force code code library codeforces implementation math programming languages python tricks

We denote the tex_77fba0109516dcfa4c8e4dd7c7d404e0 Codeforces: B. Jumping Jack algorithms beginner brute force code code library codeforces implementation math programming languages python tricks . Replacing tex_965521b32fbf06b10d3a21ce6d9320dc Codeforces: B. Jumping Jack algorithms beginner brute force code code library codeforces implementation math programming languages python tricks with tex_b2ceef57e01f1a3fba9db534d972421a Codeforces: B. Jumping Jack algorithms beginner brute force code code library codeforces implementation math programming languages python tricks , so Jack can reach a new location of tex_af4b41db28f688ceefbbf1427a87f18a Codeforces: B. Jumping Jack algorithms beginner brute force code code library codeforces implementation math programming languages python tricks , similarly tex_15fd3fb86d466a25b8194a401b84e954 Codeforces: B. Jumping Jack algorithms beginner brute force code code library codeforces implementation math programming languages python tricks , tex_70a4b08796bf13d57b546827bfa5d301 Codeforces: B. Jumping Jack algorithms beginner brute force code code library codeforces implementation math programming languages python tricks … 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) —

GD Star Rating
loading...
470 words
Last Post: Hello, world!
Next Post: Codeforces: 131A - cAPS lOCK

The Permanent URL is: Codeforces: B. Jumping Jack

Leave a Reply