Codeforces: A. Funky Numbers


The problem is from codeforces: http://www.codeforces.com/problemset/problem/192/A

192A Codeforces: A. Funky Numbers beginner brute force codeforces implementation math python

The given input to this problem can be held by a four-byte integer (32-bit). It is to search for two numbers tex_b654ca431b2a3d18daeaa53847edcec0 Codeforces: A. Funky Numbers beginner brute force codeforces implementation math python and tex_824050b200fea5fc271b9f63ce2baed2 Codeforces: A. Funky Numbers beginner brute force codeforces implementation math python such that tex_9470b323afc46c59bee00dbdd9c240e0 Codeforces: A. Funky Numbers beginner brute force codeforces implementation math python .

The straightforward solution in Python is given below, which however will exceed the time limit: 2 seconds.

from math import sqrt

n = int(raw_input()) * 2

k = int(round(sqrt(n)))
found = False

for i in range(1, k):
    if found:
        break
    for j in range(1, i):
        if i * i + j * j + i + j == n:
            print "YES"
            found = True
            break

if not found:
    print "NO"

The above algorithm has the complexity of tex_aed117e9177cc2fbc78362e53a89e0bc Codeforces: A. Funky Numbers beginner brute force codeforces implementation math python . However, there is a better algorithm, which runs in complexity of tex_91ba59ea4191e794ea43b27b5cb207f9 Codeforces: A. Funky Numbers beginner brute force codeforces implementation math python . The maximum input is tex_bd476d396103d03da143d369ef63bc3e Codeforces: A. Funky Numbers beginner brute force codeforces implementation math python therefore tex_19b251d821a86f3f4a8d7bafcb7199d3 Codeforces: A. Funky Numbers beginner brute force codeforces implementation math python , which can be solved in bruteforce very quickly. This improved algorithm bruteforces the integers up to tex_8129dc0bbe049ec747b0420e59c59697 Codeforces: A. Funky Numbers beginner brute force codeforces implementation math python and computes the remainder. It checks if the remainder is the multiplication of tex_fe39b03907e6871015dc608003cf3d05 Codeforces: A. Funky Numbers beginner brute force codeforces implementation math python .

from math import *

n = int(raw_input()) * 2

k = int(round(sqrt(n)))

found = False

for i in range(1, k):
    r = n - i * i - i
    x = int(floor(sqrt(r)))
    if x * (x + 1) == r:
        found = True
        break

print "YES" if found else "NO"

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
550 words
Last Post: Using Tkinter in Python GUI programming
Next Post: Bogo Sort Algorithm

The Permanent URL is: Codeforces: A. Funky Numbers

Leave a Reply