A house will have maximum signal strength if the signal doesn’t have to travel through another house, that is, if we draw a straight line from the town hall to that house, the line doesn’t go through another line. Now let the coordinate of the town-hall be (x,y)(x,y) and the coordinate of the candidate house be (x1,y1)(x_1,y_1), then dx=abs(xx1)dx=abs(x-x_1) and dy=abs(yy1)dy=abs(y-y_1), the house will have maximum signal iff GCD(dx,dy)==1GCD(dx,dy)==1, here GCD represents the greatest common divisor of two numbers.

Using inclusion-exclusion and bit-masking we can calculate the number of coordinates with GCD(dx,dy)>1GCD(dx,dy)>1 in O(nlog(n))O(n*log(n) )and using that we can find the other coordinates.

Statistics

50% Solution Ratio
aritra741Earliest, Feb '22
Kuddus.6068Fastest, 0.1s
tanvirtareqLightest, 5.5 MB
tanvirtareqShortest, 927B
Toph uses cookies. By continuing you agree to our Cookie Policy.