This page is still under construction.

Parts of this page are still being built. What you see may change.

Balanced Food

Time limit1sMemory limit128 MB

Summary
Given a pizza of n slices and a sector-shaped table, find the lexicographically smallest order of eating slices so the remaining slices' center of gravity always stays over the table.
Level

Medium7 of 10

Topics
Brute force, Geometry, Backtracking, Greedy
Solved
No attempts yet

Problem

Computer scientists live on pizza. To eat a little more healthily they now finish a whole pizza slice by slice, carefully making sure the part still on the table never slides off.

The pizza is a homogeneous two-dimensional disc with center p=(px,py)p=(p_x,p_y) and radius rr, cut into nn slices of identical size by nn straight cuts running from the center. One cut always runs along the positive xx-axis (towards increasing xx-values), and the slices are numbered 1,2,…,n1,2,\dots,n counter-clockwise; slice 11 lies directly above the positive xx-axis and spans the angles from 00 to 2π/n2\pi/n.

You eat the slices one at a time, in any order you like. Because all slices meet at the center, whatever remains is always treated as one connected, rigid, flat object, no matter which slices have already been eaten. A connected, rigid, flat object stays on a convex, flat surface if and only if its center of gravity lies above the surface. Hence, after each slice is eaten, the center of gravity of the remaining slices must still lie over the table, or the pizza falls.

The table is shaped like a slice of pizza (a circular sector) and is never larger than a half-circle, so it is convex. It is given by three corners tt, uu, vv in counter-clockwise order, with tt as the apex (center of the sector). The edges t ut\,u and t vt\,v have equal length — the sector's radius — apart from tiny rounding errors.

All slices are congruent and have equal mass, so the center of gravity of any set of remaining slices is the average of those slices' centroids. The centroid of one slice (a circular sector of radius rr and central angle 2π/n2\pi/n) lies on its bisector at distance 2 r sin⁡(π/n)3 (π/n)\dfrac{2\,r\,\sin(\pi/n)}{3\,(\pi/n)} from pp. In general the center of gravity of a region ss has xx-coordinate (∫sx ds)/(∫sds)\left(\int_s x\,ds\right)/\left(\int_s ds\right) and yy-coordinate (∫sy ds)/(∫sds)\left(\int_s y\,ds\right)/\left(\int_s ds\right), where the denominator is the area of ss.

Input

The input contains several test cases. Each test case is a single line

n (px,py) r (tx,ty) (ux,uy) (vx,vy)

where nn is the number of equal slices the pizza is cut into (1≤n≤91 \le n \le 9), followed by nine floating-point numbers: the center p=(px,py)p=(p_x,p_y) of the pizza, its radius rr, and the three corners t=(tx,ty)t=(t_x,t_y), u=(ux,uy)u=(u_x,u_y), v=(vx,vy)v=(v_x,v_y) of the sector-shaped table in counter-clockwise order (with tt the apex). The distances from tt to uu and from tt to vv are equal except for very small rounding errors, and the table is never larger than a half-circle.

The input is terminated by a line containing a single 00, which must not be processed.

Output

For each test case, consider every eating order (a permutation of the slice numbers) such that, at every stage from the full pizza down to the last single slice, the center of gravity of the slices still on the table lies over the table. Among all such valid orders, output the lexicographically smallest one: print the slice numbers in the order they are eaten, each followed by a single space.

If no valid order exists, print the word impossible instead.

Two orders are compared lexicographically as sequences of slice numbers. Note that the center of gravity of the whole pizza equals its center pp, so if pp does not lie over the table the answer is immediately impossible.

Examples3

  1. Example 1

    Input
    2 (-3.0,-1.0) 1.0 (-3.0,-1.1) (-1.5,0.4) (-4.5,0.4)
    9 (2.0,1.0) 1.0 (0.0,0.0) (1.0,-1.0) (-1.0,1.0)
    0
    
    Expected output
    2 1 
    impossible
    
  2. Example 2

    Input
    1 (0.0,0.0) 1.0 (0.0,-10.0) (10.0,0.0) (-10.0,0.0)
    0
    
    Expected output
    1 
    
  3. Example 3

    Input
    3 (0.0,0.0) 1.0 (0.0,-10.0) (10.0,0.0) (-10.0,0.0)
    0
    
    Expected output
    1 2 3