The problem is from codeforces: http://www.codeforces.com/problemset/problem/214/A
Simple as that, the brute-force can pass the tests. The given ranges for n and m are acceptable for the simple brute-force algorithm. is straightforward i.e. two for-loops for a and b ranging from 0 to 1000. Since both numbers are non-negative, only the numbers up to and need to be checked. Also, this can be simplified to via checking either a or b, and based on the other equation, computing directly the other value. The following Python code illustrates this.
#!/usr/bin/env python # https://helloacm.com n, m = map(int, raw_input().split(' ')) x = 0 for a in xrange(0, int(n ** 0.5) + 1): b = n - a * a if b >= 0 and a + b * b == m: x += 1 print x
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
362 wordsloading...
Last Post: How Many Zeros at the end of 1024!?
Next Post: A Javascript PC Emulator