Problem:
Problem B: How I Mathematician Wonder What You Are!

After counting so many stars in the sky in his childhood, Isaac, now an astronomer and a
mathematician, uses a big astronomical telescope and lets his image processing program count
stars. The hardest part of the program is to judge if a shining object in the sky is really a star.
As a mathematician, the only way he knows is to apply a mathematical definition of stars.

The mathematical defiition of a star shape is as follows: A planar shape F is star-shaped if and
only if there is a point C ∈ F such that, for any point P ∈ F, the line segment CP is contained
in F. Such a point C is called a center of F. To get accustomed to the definition, let's see some
examples below.

Figure 2: Star shapes (the first row) and non-star shapes (the second row)

The firrst two are what you would normally call stars. According to the above definition, however,
all shapes in the first row are star-shaped. The two in the second row are not. For each star
shape, a center is indicated with a dot. Note that a star shape in general has infinitely many
centers. For example, for the third quadrangular shape, all points in it are centers.

Your job is to write a program that tells whether a given polygonal shape is star-shaped or not.

Input

The input is a sequence of datasets followed by a line containing a single zero. Each dataset
specifies a polygon, and is formatted as follows.

n
x1 y1
x2 y2
...
xn yn

The first line is the number of vertices, n, which satisfies 4 ≤ n ≤ 50. Subsequent n lines are the x- and y-coordinates of the n vertices. They are integers and satisfy 0 ≤ xi ≤ 10000 and 0 ≤ yi ≤ 10000 (i = 1, ..., n). Line segments (xi, yi)-(xi+1, yi+1) (i = 1, ..., n - 1) and the line segment (xn, yn)-(x1, y1) form the border of the polygon in the counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions.

You may assume that the polygon is simple, that is, its border never crosses or touches itself.
You may also assume that no three edges of the polygon meet at a single point even when they
are infinitely extended.

Output

For each dataset, output "1" if the polygon is star-shaped and "0" otherwise. Each number must be in a separate line and the line should not contain any other characters.

Sample Input

6
66 13
96 61
76 98
13 94
4 0
45 68
8
27 21
55 14
93 12
56 95
15 48
38 46
51 65
64 31
0

Output for the Sample Input

1
0

Solution in Ruby (judge verdict: Memory Limit Exceeded)

### classes

class Pt
  attr_accessor :x, :y

  def initialize(x, y)
    @x = x
    @y = y
  end

  def +(pt)
    Pt.new(@x + pt.x, @y + pt.y)
  end

  def -(pt)
    Pt.new(@x - pt.x, @y - pt.y)
  end

  def -@
    Pt.new(-@x, -@y)
  end
  
  def *(t)
    Pt.new(@x * t, @y * t)
  end
  
  def /(t)
    Pt.new(@x / t, @y / t)
  end

  def dot(v)
    @x * v.x + @y * v.y
  end
  
  def cross(v)
    @x * v.y - @y * v.x
  end

  def to_s
    "(#{@x},#{@y})"
  end
end

### subroutines

def cross_lines(a0, a1, b0, b1)
  da = a1 - a0
  db = b1 - b0

  op01 = da.cross(db)
  return nil if op01 == 0.0

  v = b0 - a0
  op0 = v.cross(da)
  op1 = v.cross(db)

  t0 = op1 / op01
  t1 = op0 / op01

  [db * t1 + b0, t0, t1]
end

def cross_segs?(a0, a1, b0, b1)
  cl = cross_lines(a0, a1, b0, b1)
  return false if cl.nil?

  pt, t0, t1 = cl
  t0 > 0.0 && t0 < 1.0 && t1 > 0.0 && t1 < 1.0
end

def cross_segs_extended(a0, a1, b0, b1)
  cl = cross_lines(a0, a1, b0, b1)
  return nil if cl.nil?

  pt, t0, t1 = cl
  return nil if t0 <= 1.0 || t1 <= 0.0 || t1 >= 1.0

  [pt, t1]
end

def make_convex(pts)
  changed = true

  while pts.length > 3 && changed
    changed = false

    n = pts.length
    for i in (0...n)
      pt = pts[i]
      pt0 = pts[(i + n - 1) % n]
      pt1 = pts[(i + 1) % n]

      v0 = pt - pt0
      v1 = pt - pt1
      #p [v0.x, v0.y, v1.x, v1.y, v0.cross(v1)]
      next if v0.cross(v1) < 0

      i0 = i1 = nil
      ti0 = ti1 = nil
      ptc0 = ptc1 = nil
      
      for k in (2...(n - 2))
        ei0 = (i + k) % n
        ei1 = (ei0 + 1) % n
        ept0 = pts[ei0]
        ept1 = pts[ei1]
        
        if i0.nil?
          cs = cross_segs_extended(pt0, pt, ept0, ept1)
          if ! cs.nil?
            ptc0, ti0 = cs
            i0 = ei0
          end
        end

        if i1.nil?
          cs = cross_segs_extended(pt1, pt, ept0, ept1)
          if ! cs.nil?
            ptc1, ti1 = cs
            i1 = ei0
          end
        end

        break if i0 && i1
      end

      #p [i, i - 1, i0, ti0, ptc0.x, ptc0.y]
      #p [i, i + 1, i1, ti1, ptc1.x, ptc1.y]
      #exit

      pts0 = [pt, ptc0]
      j = i0
      while j != i1
        j = (j + 1) % n
        pts0 << pts[j]
      end
      pts0 << ptc1

      pts = pts0
      changed = true
      break
    end
  end

  pts
end

### main

loop do
  n = gets.to_i
  break if n == 0

  pts = n.times.map{Pt.new(*gets.split.map(&:to_f))}
  #p pts

  ptsc = make_convex(pts.clone)
  #p ptsc.map{|pt| pt.to_s}

  c = ptsc.inject(:+) / ptsc.length
  #p c.to_s

  star = true
  for i in (0...n)
    pt = pts[i]
    for j in (1...(n - 1))
      i0 = (i + j) % n
      i1 = (i0 + 1) % n

      if cross_segs?(c, pt, pts[i0], pts[i1])
        star = false
        break
      end
    end
    break if ! star
  end

  puts (star ? 1 : 0)
end