Suppose we have four numbers a, b, c and d and We have to find number of pairs (x, y) can be found such that that follows the equation: x^2 + y^2 = (x*a) + (y*b) where x in range [1, c] and y in range [1, d]
So, if the input is like a = 2 b = 3 c = 2 d = 4, then the output will be 1 because one pair is (1, 1).
To solve this, we will follow these steps −
- ans := 0
- for x in range 1 to c, do
- l := x*(x-a)
- det2 := b*b - 4*l
- if det2 is same as 0 and b is even and 1 <= floor of (b/2) <= d, then
- ans := ans + 1
- go for next iteration
- if det2 > 0, then
- det := integer part of the square root of det2
- if det^2 is same as det2 and (b+det) is even, then
- if 1 <= floor of (b+det)/2 <= d, then
- ans := ans + 1
- if 1 <= floor of (b-det)/2 <= d, then
- ans := ans + 1
- if 1 <= floor of (b+det)/2 <= d, then
- return ans
Example
Let us see the following implementation to get better understanding −
def solve(a, b, c, d): ans = 0 for x in range(1,c+1): l = x*(x-a) det2 = b*b - 4*l if det2 == 0 and b%2 == 0 and 1 <= b//2 <= d: ans += 1 continue if det2 > 0: det = int(round(det2**0.5)) if det*det == det2 and (b+det) % 2 == 0: if 1 <= (b+det)//2 <= d: ans += 1 if 1 <= (b-det)//2 <= d: ans += 1 return ans a = 2 b = 3 c = 2 d = 4 print(solve(a, b, c, d))
Input
2, 3, 2, 4
Output
1