Let be the number of ordered pairs such that,
You are given that and that
Find
EDIT : Your code should run in 20-40s
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Let a 2 + b 2 = α
⟹ c 2 + d 2 = X − α
Now, we will create a hash-table d such that,
d = {A particular Value of α : Number of ways it can be represented as sum of two perfect squares}
So for example, d [ α = 5 0 ] = 2 , as 50 can be represented in two ways as sum of two squares, 5 0 = 1 2 + 7 2 = 5 2 + 5 2
Now, all we have to do is generate all possible values of α and store it in d .
Now, we iterate through each and every element in d , and check if X − α is there in the table(checking existence of an element in a hash table takes O(1) time)
If X − α doesn't exist, then X cannot be represented as sum of four squares for that particular value of α . If X − α does exist, then number of ways to write X as sum of four squares becomes,
d [ α ] ∗ d [ X − α ]
Using this logic, and little bit of optimization, your code can run in less than 20s