Блог пользователя aajjbb

Автор aajjbb, 14 лет назад, По-английски

Hi, Today I've faced an interesting geometry problem, which I don't know yet how to solve without TLE.

I'm not posting the link because it's in portuguese, but summarising, there's two points in the cartesian plan, the statement gives it's cordinates, how many 'crossing' (integer cordinates) this segments passes through ?

My approach (TLE) O(N) being N the number of integer x cordinates. I check the slope the line, and iterate over the integer point in the x axis and go checking if the current y value for the actual x is also an integer.

The correct solution is probably an formula which easiely check this points in O(1), there's exists this formula ? what's the correct approach ?

Thanks in advance.

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
14 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Consider the points (x1, y1) and (x2, y2). Let a = |x1 - x2|, b = |y1 - y2|. Then the number of points with integer coordinates on the segment is gcd(a, b) + 1, including points (x1, y1) and (x2, y2).

And one problem: http://acm.timus.ru/problem.aspx?space=1&num=1139

»
9 лет назад, скрыть # |
← Rev. 4  
Проголосовать: нравится -9 Проголосовать: не нравится

Is there any way to find an integral point on a line segment between two given integral points?