We need to understand the problem. We have: origin (0,0) is the location of "my plum". There are other trees: U other plum trees, M peach trees, S cherry trees. They each have a scent spread pattern: given an angle (du, dm, ds) and the wind direction w and strength a on a day, the scent of a flower spreads as a sector (fan shape) from the tree's position, with central direction w (wind direction), angular half-width? Actually "The sector spreads symmetrically around the direction w of the wind, and has a region whose radius is the strength of the wind a. The angle d at which the scent spreads is determined by the type of flower". So for a flower type, the sector angle is d (so total angle spread is d? It says "the angle d at which the scent spreads". Typically it means the sector spans an angle d, symmetric about the wind direction, so half-angle d/2. But careful: "The sector spreads symmetrically around the direction w of the wind, and has a region whose radius is the strength of the wind a. The angle d at which the scent spreads is determined by the type of flower." So the region is a sector of a circle radius a, centered at the tree, with central angle d, oriented such that the bisector of the sector is the wind direction. So the sector covers all points whose distance from tree ≤ a and whose direction from tree relative to wind direction is within ±(d/2).
Thus, for a given day (wind w, strength a), each tree (type-specific d) emits scent to its sector. A house (candidate location) can receive scent from multiple trees, including my plum at origin. We are to count, for each house, the number of days (R days) on which the house receives scent ONLY from my plum (i.e., no other plum, peach, or cherry tree's scent reaches that house on that day). But careful: The problem states: "find the house with the most days when only my plum scent arrives." So we need days where the house receives the scent of my plum, and does not receive scent from any other tree (plum, peach, cherry). However, does the house receive my plum scent on every day? Not necessarily: if the house is outside the sector of my plum for that day (distance > a or angle not within du/2), then it's not receiving my plum scent. But we only consider days when only my plum scent arrives. That implicitly requires that my plum scent arrives; otherwise, it would be "no scent" or "other scent only". But the phrase "only my plum scent arrives" means the only scent that arrives is my plum's. So it must be that my plum's scent reaches, and no other tree's scent reaches. So we count days where house is in my plum's sector, and not in any other tree's sector.
But what about the case when house is within multiple trees including my plum? We need only my plum. So we need inclusion in my plum's sector and exclusion from all other sectors.
We are given that all coordinates are distinct, and there is nothing but my plum at the origin. Also, "For any flower, there is no house within 0.001 distance from the boundary of the area where the scent of the flower reaches." So we can treat boundaries as not ambiguous; points are either clearly inside or outside.
We need to output house numbers (1-indexed from the order they appear in input) with the maximum count of such days. If for all houses count=0, output NA.
Multiple houses may tie for max.
Input format: multiple datasets. End with two zeros? Actually they said "The end of the input is indicated by two lines of zeros." So after last dataset, there is a line with "0 0". But the example shows last dataset: "2 1 ... 0 0"? Actually example input:
6 3
... (first dataset)
2 1
1 3
5 2
0 1 1 90 30 45
-3 0
2 -2
45 6
0 0
So after first dataset, there is a second dataset starting with "2 1", then after its data, we have "0 0" indicating end. So yes, final line is two zeros.
Now we need to parse each dataset.
We need to compute for each house h (position hx, hy) and each day (wind direction w, strength a), determine:
-
Is the house inside the sector of my plum (origin, type plum with angle du)? (We have du given for plum). Note: My plum is at (0,0). But wait: The problem says "the positions of plums, peaches, and cherry trees other than my plums are shown in coordinates with the position of my plums as the origin." So origin is my plum. So we treat my plum as a plum tree at (0,0) with angle du. However, note that du is given in the line "U M S du dm ds". So du is the angle for plum trees. That includes my plum? It says "the angle d at which the scent spreads is determined by the type of flower". So my plum is also plum, so it uses du. Yes.
-
For each other tree (U other plums, M peach, S cherry) we need to check if the house lies inside that tree's sector for that day. If any does, then condition fails.
We need to be careful: The wind direction and strength are same for all trees on a given day. So each tree has its own sector with the same a and w, but the sector is centered at the tree, oriented to w, and radius a. So check inclusion relative to each tree's coordinates.
Now, geometry: For a tree at (tx, ty), given wind direction w (angle measured from +x axis counterclockwise), and strength a (radius). The sector is all points (x,y) such that distance from (tx,ty) ≤ a, and the angle of vector (x - tx, y - ty) relative to +x axis is within [w - d/2, w + d/2] modulo 360. Since it's symmetric. But careful: The angle spread d is the total width? The description: "The sector spreads symmetrically around the direction w of the wind, and has a region whose radius is the strength of the wind a. The angle d at which the scent spreads is determined by the type of flower." Typically, the scent spreads in a fan shape, and d is the angle of the fan. So it's total angle, symmetrical about w. So half-angle = d/2.
Thus condition: distance <= a, and angle_diff modulo 360 <= d/2. But we have to handle wrap-around (0-360). Usually we compute the absolute smallest difference between the vector angle and w, then compare to d/2.
But note: The input says "The direction of the wind is expressed as an angle measured counterclockwise from the positive direction of the x-axis, and the unit is degrees." So w is between 0 and 360 (including 0, excluding 360). The tree's sector: "The sector spreads symmetrically around the direction w of the wind". So yes.
Also "The angle d at which the scent spreads is determined by the type of flower." d is given 1 ≤ d < 180, so half-angle < 90.
Now, we need to treat "no house within 0.001 distance from the boundary". So we can use strict inequalities: distance < a, and angle difference < d/2? But due to boundary avoidance, they guarantee that no house lies exactly on boundary (within 0.001). So we can use <= or <, it's fine because they are not ambiguous. However, to be safe, we can use <= with a small epsilon, but the guarantee ensures we can use direct floating point comparisons if we compute exactly? But using floating point may introduce errors. The guarantee says "no house within 0.001 distance from the boundary", so there is a gap. So we can compute with double and check with tolerance maybe 1e-6. But easier: we can use rational/comparisons using squared distances, and for angle we can use dot products and cross products to check if point lies inside the sector.
Since we need to check many times: H up to 100, number of trees total up to U+M+S ≤ 30 (each max 10, but three types so total max 30). R up to 100. So at most 10010030 = 300,000 checks. That's fine.
We'll compute for each day, for each house:
-
Compute if inside my plum sector.
-
For each other tree, compute if inside that tree's sector; if any, break and mark as not exclusive.
-
If inside my plum and none others, increment count for that house.
After all days, find max count and output houses with that count.
Now careful: There are also other plum trees. They are also plum, with same angle du. So they have the same d as my plum. Also peach has dm, cherry ds.
Now we need to consider that the house might be inside multiple trees' sectors. But we only care if any other tree's scent reaches.
Edge case: What if there is a tree of same type as my plum, but it's also at origin? But given no other at origin, okay.
Now implementation details:
Define a function inside_sector(px, py, tree_x, tree_y, wind_dir_deg, wind_strength, d_deg) returns true if point (px,py) is inside the sector from tree.
Compute vector from tree to point: vx = px - tx, vy = py - ty.
Check distance squared: (vxvx + vyvy) <= a^2? Since no house near boundary, we can use <=.
Then compute angle difference: compute the direction angle of v (theta_v) in degrees in [0,360). Then compute absolute minimal difference to wind_dir: diff = min(|theta_v - w|, 360 - |theta_v - w|). Then check diff <= d/2.
But careful: The sector is symmetric about w. So yes, diff <= d/2.
Alternatively, we can use dot product: cos(d/2) * |v| <= (v · wind_unit_vector) and also the angle diff from w is within d/2, meaning that the absolute angle difference is <= d/2, which is equivalent to (v · u) >= |v| * cos(d/2) and also the cross product sign? Actually if we only care about absolute angle difference, we only need dot product condition because it ensures the angle <= d/2 (since cos is decreasing on [0,180]). But also we need to ensure that point is within the full cone, not just the opposite direction? Actually if the angle difference is > 90 deg, dot product becomes negative, but that's still < cos(d/2) if d/2 < 90 (d<180 => d/2<90). But if d/2 > 90? d < 180 so d/2 < 90. So cos(d/2) > 0. So condition dot >= |v| * cos(d/2) ensures angle <= d/2 because cos is decreasing from 0 to 180. For angles > 90, dot negative which is < positive threshold, so fails. So it's sufficient. But also we need to ensure that the point is not in the opposite cone? Actually if the point's angle differs by, say, 120 deg, dot = |v|*cos(120) = -0.5|v|, which is less than |v|*cos(d/2) because cos(d/2) >0. So it fails. So just dot >= |v|*cos(d/2) works. However, note that if d/2 > 90 (but d<180 so not), then cos(d/2) might be negative, and dot condition would be >= negative, so it would include points with angle > 90? But d/2 < 90, so okay.
Thus we can use: Let ux = cos(w), uy = sin(w). Compute dot = vxux + vyuy. Then condition: dot >= 0? Actually we already have cos(d/2) >0, so dot >= |v|*cos(d/2) implies dot>0. So it's fine.
But also need to check that the point is within the wedge, not just the forward half-cone; but since d/2 < 90, the wedge is within ± < 90, so forward direction. So dot condition is sufficient. But we need to ensure the point lies within the symmetric sector. For symmetric sector, the condition is simply |angle(v)-w| <= d/2 (mod 360). Using dot and cross product we could also compute absolute angle difference via arccos(dot/|v|) (if we have u unit). However, using arccos is slower and less precise. But we can use dot and cross product to compute squared sine etc. The condition |angle difference| <= d/2 is equivalent to dot >= |v|*cos(d/2) and (v cross u) magnitude? Actually if dot >= |v|*cos(d/2) ensures angle <= d/2 only if the angle difference is between 0 and 180? But angle difference considered is the minimal one (<=180). Since dot >= |v|*cos(d/2) ensures that the absolute angle difference (the acute angle? Actually arccos(dot/|v|) gives an angle in [0,π]. That angle is the absolute difference modulo 360? For two vectors, the angle between them is always between 0 and π (180°). That angle is the minimal rotation to align them, but it doesn't distinguish between + and - direction. For a symmetric sector, we want the absolute difference to be <= d/2, where d/2 <= 90 < 180. So the angle between the vectors is exactly the absolute difference if that difference <=180; but if the difference >180, the angle between them would be 360-diff, which is <=180. So the angle between the vectors as computed by dot product gives the minimal angle (0 to 180). And since we want the point to be inside if the minimal difference <= d/2, that's exactly the condition: the angle between v and u (the wind direction vector) <= d/2. Because the minimal difference is the angle between them (0-180). And since d/2 < 180, it's fine. So we can simply check that the angle between v and u <= d/2. So condition: dot >= |v| * cos(d/2). So we don't need to compute the actual signed angle or handle modulo. Good.
Thus we can precompute cos_half_angle for each flower type (my plum, other plum, peach, cherry). Since d differs per type, we compute cos(d/2) in radians.
But careful: The angle d is given in degrees. Convert to radians: d_rad = d * π / 180, half = d_rad/2, cos_half = cos(half). That's constant per dataset per type.
Now we need to compute for each tree: we have coordinates (tx, ty) and d depending on type. For each day: wind direction w (degrees), strength a.
Compute wind unit vector: ux = cos(w_rad), uy = sin(w_rad). (w_rad = w * π / 180).
Now for a house at (hx, hy), compute vector v from tree to house: vx = hx - tx, vy = hy - ty.
Check: distance squared <= a^2? Actually condition: distance <= a. Since a is integer up to 100, we can compute squared distance and compare with a^2.
Then compute dot = vxux + vyuy.
Compute |v| = sqrt(vx^2+vy^2). Then check if dot >= |v| * cos_half.
But we can avoid sqrt by using squared comparison after rearranging? Condition dot >= |v| * cos_half => dot^2 >= |v|^2 * cos_half^2, provided dot >= 0? Actually if dot is negative, then dot^2 is positive, but the inequality dot >= |v|*cos_half cannot hold if cos_half>0 and dot negative, because RHS positive. So we need to also check dot >= 0? Actually dot >= |v|*cos_half implies dot>0 since RHS>0. So we can first check dot >0 (or >=0) to skip sqrt? But we can compute sqrt. With 300k checks, sqrt is okay. But we can optimize: compute |v|, then compare dot/|v| >= cos_half. Since sqrt is a bit heavy but fine.
Given constraints small, we can do floating point.
However, need to be careful about precision. They guarantee no points near boundary. So using double with a small epsilon (like 1e-9) should be fine.
But to be safe, we could compute using squared: Condition is equivalent to (dot)^2 >= (vx^2+vy^2) * (cos_half)^2 AND dot >= 0 (since cos_half>0). That avoids sqrt. That's even better. But we must ensure that the comparison is not strict because of boundaries? Since boundaries are avoided, we can use either. We'll use direct floating point with epsilon.
Simplify: For each check: compute dist2 = vx^2+vy^2; if dist2 > aa + eps: outside. Else compute dot; if dot < 0: then automatically fails because cos_half >0. else compute angle condition: dotdot >= dist2 * cos_half^2? Actually we need dot >= sqrt(dist2)*cos_half. Square both sides if dot >=0: dot^2 >= dist2 * cos_half^2. So we can compute that.
Thus we need cos_half^2 precomputed. However, be careful: if cos_half is computed from d/2, we might have floating rounding errors. Use epsilon.
Set epsilon = 1e-9.
We'll implement inside = (dist2 <= aa + eps) && (dot >= 0) && (dotdot >= dist2 * cos_half_sq - eps). Actually to be safe, we can do dotdot > dist2cos_half_sq - something. Because boundaries are away, we can be exact.
Alternatively, use trig directly: compute angle between vectors using arccos(dot/sqrt(dist2)) and compare to half_angle. But avoid.
Thus we need to compute for each house, each day, check my plum first: if inside, then check other trees; if any other inside, then skip increment.
Note: My plum is at origin (0,0). So vector v = (hx, hy). And we need to use du. So compute condition with tree=(0,0) and d=du.
Now, also note: The number of other trees could be zero. Then just need to check my plum.
Now also note: The house might be inside my plum sector on some days and also inside other tree's sector; then not count. Or not inside my plum sector, then doesn't count, regardless of others.
Thus algorithm:
For each dataset:
-
Read H,R.
-
Read H house coordinates.
-
Read U,M,S,du,dm,ds.
-
Read U plum tree coordinates (other).
-
Read M peach tree coordinates.
-
Read S cherry tree coordinates.
-
Read R wind records (w,a).
-
Preprocess:
- Create list of trees: each tree has (tx,ty, d_angle_deg, cos_half_sq). But we need different d for each type. So we can store type with d.
- There are three types: other plum (use du), peach (dm), cherry (ds).
- For my plum, we have special case.
-
For each day i (wind w, a):
- Precompute wind unit vector components: ux = cos(w_rad), uy = sin(w_rad).
- Also precompute a_sq = a*a.
- For each house j:
- Check inside my plum: using origin, du.
- if not inside: continue to next house (no increment).
- Then for each other tree:
- if inside that tree's sector: mark as not exclusive, break.
- If none inside, increment count[j] by 1.
-
After all days, find max count among houses.
-
If max == 0: print "NA".
-
Else: print indices (starting from 1) of houses with count == max, separated by space.
Note: The example output: "5 6" and "NA". So they number houses in order given. In first dataset, H=6. They output 5 6. So they consider houses 5 and 6 as having the most days where only my plum scent arrives.
Let's verify quickly with example to ensure interpretation.
First dataset:
6 3
2 1
1 2
5 2
1 3
1 5
-2 3
1 1 1 90 30 45
3 -4
-3 0
2 -2
45 6
90 6
135 6
So H=6, R=3. Houses coordinates: (2,1), (1,2), (5,2), (1,3), (1,5), (-2,3).
Then U=1, M=1, S=1, du=90, dm=30, ds=45.
Plum other: (3,-4)
Peach: (-3,0)
Cherry: (2,-2)
Then wind records: (45,6), (90,6), (135,6)
We need to compute for each house on each day.
We need to compute for each day:
- Wind direction w=45°, a=6.
Unit vector u = (cos45, sin45) = (√2/2, √2/2) ≈ (0.70710678, 0.70710678)
My plum sector: center (0,0), radius 6, angle du=90°, half=45°. So sector is a 90° wedge (from 0° to 90°? Actually symmetric about 45°, so it spans from 0° to 90°? 45±45 => 0 to 90). So points with angle between 0° and 90°, distance <=6.
Check houses:
1: (2,1) -> distance sqrt(4+1)=√5≈2.236 <6, angle = arctan2(1,2)= about 26.565°, inside 0-90 => yes.
2: (1,2) -> distance √5≈2.236, angle arctan2(2,1)=63.435°, inside => yes.
3: (5,2) -> distance √(25+4)=√29≈5.385<6, angle= arctan2(2,5)=21.8°, inside => yes.
4: (1,3) -> distance √10≈3.162, angle=71.565°, inside => yes.
5: (1,5) -> distance √26≈5.099, angle=78.69°, inside => yes.
6: (-2,3) -> distance √13≈3.606, angle= arctan2(3,-2)= 123.69°, which is not between 0-90 => outside? Actually we need symmetric around 45°, so allowed angles are 0 to 90. 123.69 is outside. So house6 not inside my plum.
So for day1, only houses 1-5 are inside my plum. House6 not.
Now check other trees for houses 1-5.
Tree1: other plum at (3,-4) with d=90°, half=45°. Wind direction 45°, radius 6.
We need to check each house relative to this tree.
First compute vector from tree to house.
House1 (2,1): v = (2-3, 1-(-4)) = (-1,5). distance sqrt(1+25)=√26≈5.099 <6? yes. angle of v = arctan2(5,-1)= about 101.31° (since vector points left and up). Wind direction 45°. minimal angle difference = min(|101.31-45|=56.31, 360-56.31=303.69) =>56.31°, half=45°, 56.31 >45 so outside sector. So house1 not inside other plum.
But need also check peach and cherry.
Peach at (-3,0) with d=30°, half=15°, radius6.
House1: v = (2-(-3),1-0) = (5,1). distance sqrt(25+1)=√26≈5.099<6. angle = arctan2(1,5)=11.31°. wind 45°, diff = 33.69°, >15? Actually 33.69 >15 => outside.
Cherry at (2,-2) d=45°, half=22.5°.
House1: v = (2-2,1-(-2)) = (0,3). distance 3<6. angle = 90°. diff from 45 =45°, half=22.5 =>45>22.5 => outside.
Thus house1 not inside any other tree on day1? So only my plum? Actually we need to check all three trees. None are inside. So house1 receives only my plum.
House2 (1,2): distance from other plum: (1-3,2-(-4)) = (-2,6) => dist sqrt(4+36)=√40≈6.3249 >6? Actually 6.3249 >6, so outside (since radius 6). So no.
Peach: (1-(-3),2-0) = (4,2) dist sqrt(20)=4.472 <6; angle= arctan2(2,4)=26.565°, diff from 45 =18.435°, half=15 => 18.435>15, so outside.
Cherry: (1-2,2-(-2)) = (-1,4) dist sqrt(1+16)=√17≈4.123<6; angle= arctan2(4,-1)=104.04°, diff min= min(104.04-45=59.04, 360-59.04=300.96)=59.04 >22.5 => outside.
So house2 only my plum.
House3 (5,2): other plum: (5-3,2-(-4)) = (2,6) dist sqrt(4+36)=√40≈6.3249>6 => out.
Peach: (5-(-3),2-0) = (8,2) dist sqrt(68)=8.246>6 => out.
Cherry: (5-2,2-(-2)) = (3,4) dist sqrt(9+16)=5 <6; angle= arctan2(4,3)=53.13°, diff from 45=8.13°, half=22.5 => 8.13 <=22.5 => inside! So house3 is inside cherry tree sector. So house3 receives both my plum and cherry => not exclusive.
House4 (1,3): other plum: (1-3,3-(-4)) = (-2,7) dist sqrt(4+49)=√53≈7.28>6 => out.
Peach: (1-(-3),3-0) = (4,3) dist 5 <6; angle= arctan2(3,4)=36.87°, diff=45-36.87=8.13°, half=15 => inside? 8.13 <=15 => yes, inside peach sector. So house4 gets peach => not exclusive.
House5 (1,5): other plum: (1-3,5-(-4)) = (-2,9) dist sqrt(4+81)=√85≈9.22>6 => out.
Peach: (1-(-3),5-0) = (4,5) dist sqrt(41)=6.403>6? 6.403>6 => out.
Cherry: (1-2,5-(-2)) = (-1,7) dist sqrt(1+49)=√50≈7.07>6 => out.
So house5 only my plum.
Thus day1: houses 1,2,5 count++? Also maybe house? Actually we need to check house6 not in my plum so not count.
So counts after day1: h1:1, h2:1, h3:0, h4:0, h5:1, h6:0.
Day2: w=90°, a=6.
My plum sector: direction 90°, half=45°, spans 45° to 135°? Actually symmetric: 90±45 => 45 to 135. So angles between 45 and 135 inclusive? Actually boundaries excluded but fine.
Check houses:
1: (2,1) angle=26.565°, not between 45-135 => out. So h1 not in my plum.
2: (1,2) angle=63.435°, inside? 63.435 between 45 and 135 => yes.
3: (5,2) angle=21.8°, out.
4: (1,3) angle=71.565°, in.
5: (1,5) angle=78.69°, in.
6: (-2,3) angle=123.69°, in? 123.69 between 45-135 => yes.
So h2, h4, h5, h6 inside my plum.
Now check other trees.
Wind direction 90°, unit vector (0,1).
First other plum at (3,-4), d=90°, half=45°.
Check houses inside my plum: h2(1,2), h4(1,3), h5(1,5), h6(-2,3).
Compute for each:
h2: vector from plum: (1-3,2-(-4)) = (-2,6). distance sqrt(40)=6.3249>6? >6, so out.
h4: (1-3,3-(-4)) = (-2,7) dist sqrt(53)=7.28>6 => out.
h5: (1-3,5-(-4)) = (-2,9) dist sqrt(85)=9.22>6 => out.
h6: (-2-3,3-(-4)) = (-5,7) dist sqrt(25+49)=√74=8.6>6 => out.
So no plum.
Peach at (-3,0) d=30°, half=15°.
Check:
h2: vector (1-(-3),2-0) = (4,2). distance sqrt(20)=4.472<6; angle = arctan2(2,4)=26.565°, wind direction 90°, angle difference = 90-26.565=63.435°, minimal diff? Since diff 63.435, half=15, not inside (63.435>15). So out.
h4: (1-(-3),3-0) = (4,3) dist 5; angle= arctan2(3,4)=36.87°, diff=53.13° >15 => out.
h5: (1-(-3),5-0) = (4,5) dist sqrt(41)=6.403>6? >6 => out.
h6: (-2-(-3),3-0) = (1,3) dist sqrt(10)=3.162<6; angle= arctan2(3,1)=71.565°, diff = |90-71.565|=18.435°, >15 => out.
Cherry at (2,-2) d=45°, half=22.5°.
Check:
h2: (1-2,2-(-2)) = (-1,4) dist sqrt(17)=4.123<6; angle = arctan2(4,-1)=104.04°, diff from 90 = min(|104.04-90|=14.04, 360-14.04=345.96)=14.04°, half=22.5 => 14.04 <=22.5 => inside! So h2 is inside cherry. So h2 not exclusive.
h4: (1-2,3-(-2)) = (-1,5) dist sqrt(26)=5.099<6; angle= arctan2(5,-1)=101.31°, diff = 11.31°, <=22.5 => inside? 11.31<=22.5 => yes. So h4 inside cherry -> not exclusive.
h5: (1-2,5-(-2)) = (-1,7) dist sqrt(50)=7.07>6 => out.
h6: (-2-2,3-(-2)) = (-4,5) dist sqrt(41)=6.403>6? >6 => out.
So only h5 and h6 remain? Actually h2 and h4 disqualified. h5 and h6 are inside my plum and not inside any other. Check h5: we already saw cherry out, peach out, plum out. So exclusive. h6: peach out, cherry out, plum out. So exclusive.
Thus day2: h5++, h6++.
Current counts: h1:1, h2:1, h3:0, h4:0, h5:2, h6:1.
Day3: w=135°, a=6.
My plum sector: direction 135°, half=45°, spans 90° to 180°? Actually 135±45 => 90 to 180. So angles between 90 and 180.
Check houses:
1: (2,1) angle=26.565° => out.
2: (1,2) angle=63.435° => out.
3: (5,2) angle=21.8° => out.
4: (1,3) angle=71.565° => out.
5: (1,5) angle=78.69° => out.
6: (-2,3) angle=123.69° => inside (since between 90-180). So only h6 inside my plum.
Now check other trees for h6.
Plum other: (3,-4) to h6: vector = (-2-3,3-(-4)) = (-5,7) as before. distance sqrt(74)=8.6>6 => out.
Peach: (-3,0) to h6: (1,3) distance sqrt(10)=3.162<6; angle = arctan2(3,1)=71.565°, wind direction 135°, diff = min(|135-71.565|=63.435, 360-63.435=296.565) =63.435°, half=15 => out.
Cherry: (2,-2) to h6: (-4,5) distance sqrt(41)=6.403>6 => out.
So h6 exclusive. So h6 count++.
Final counts: h1:1, h2:1, h3:0, h4:0, h5:2, h6:2.
Maximum count is 2, achieved by houses 5 and 6. So output "5 6". That matches.
Thus interpretation correct.
Second dataset in example: H=2, R=1. Houses? Actually dataset:
2 1
1 3
5 2
0 1 1 90 30 45
-3 0
2 -2
45 6
0 0
So H=2, R=1. Houses: (1,3) and (5,2). U=0, M=1, S=1? Wait line: "0 1 1 90 30 45". So U=0, M=1, S=1. So there is one peach at (-3,0) and one cherry at (2,-2). No other plum.
Then wind: 45 6 (only day).
We compute:
My plum du=90°, half=45°, sector 0-90.
House1 (1,3): distance sqrt(10)=3.162, angle=71.565°, inside (0-90). So inside my plum.
Now check peach: (-3,0) to (1,3) => (4,3) distance 5<6; angle= arctan2(3,4)=36.87°, diff from 45=8.13°, half=15 => inside (since 8.13<=15). So house1 receives peach scent -> not exclusive.
House2 (5,2): angle= arctan2(2,5)=21.8°, inside my plum (0-90). Check peach: (5-(-3),2-0)=(8,2) distance sqrt(68)=8.246>6 => out. Check cherry: (5-2,2-(-2))=(3,4) distance 5 <6; angle=53.13°, diff from 45=8.13°, half=22.5 => inside. So house2 receives cherry -> not exclusive. So both houses receive other scents. Thus counts: h1=0, h2=0, max=0 => NA. Matches.
So the algorithm works.
Now need to be careful about input parsing: There are multiple datasets. The line "U M S du dm ds" where U, M, S can be zero. Then following lines: if U>0, U lines of other plum; then M lines of peach; then S lines of cherry. So we need to read accordingly.
Also note: the input includes coordinates for houses and trees, possibly negative.
Now we need to compute for each day, for each house, check my plum first. If fails, skip.
Also we need to treat other trees list: combine all types, but note they have different d values. So we store for each tree its d and cos_half_sq (or half_angle rad). But easier: store the half-angle in rad and cos_half for each tree, but we can compute per check with squared method.
We can compute for each tree: (tx, ty, cos_half, half? Actually we need cos_half for dot condition and a^2 for distance.
But we need per type d given. So compute for each tree:
- if plum (other): d = du; else if peach: d = dm; else if cherry: d = ds.
Compute half_rad = d * π / 360 (since d/2). Then cos_half = cos(half_rad). Then cos_half_sq = cos_half * cos_half.
But note: For my plum we have same du.
We will precompute for each day: wind direction rad = w * π / 180, unit vector (ux, uy). Also a_sq = a*a.
Now for each house, compute vector from my plum (0,0) to house: v0 = (hx, hy). Check inside my plum: dist2 = hx^2+hy^2; if <= a_sq, then compute dot0 = hxux + hyuy. Then check dot0 >= 0? Actually dot0 could be negative but if cos_half>0, we need dot0 >= sqrt(dist2) * cos_half_my. But use squared: dot0^2 >= dist2 * cos_half_my_sq and dot0 >= 0? Actually if dot0 is negative, dot0^2 positive but dot0 is negative, and sqrt(dist2)*cos_half positive, so inequality dot0 >= sqrt(dist2)*cos_half would be false. But if we square both sides, we need to be careful: dot0^2 >= dist2 * cos_half_sq does not imply dot0 >= sqrt(dist2)*cos_half because dot0 could be negative and its square is large. For example, dist2=4, cos_half=0.5 => sqrt(dist2)*cos_half = 1.0. dot0 = -2 gives dot0^2=4 >= 1, but dot0 = -2 < 1. So we need to enforce dot0 >= 0 separately. So condition: dot0 >= 0 AND dot0^2 >= dist2 * cos_half_sq.
Better: if dot0 < 0, then false.
Alternatively, we can compute angle difference directly via arccos, but that's heavy.
So we'll do:
if dist2 > a_sq: false.
else:
if dot0 < 0: false
else: if dot0*dot0 < dist2 * cos_half_sq - eps: false else true.
But maybe cos_half_sq could be exactly zero? d<180 => half<90, cos_half>0 => cos_half_sq >0.
So that works.
Now for other trees: same check.
But note: for each tree, the same a and wind vector.
Now we need to be careful about precision: The problem guarantees no point within 0.001 of boundary, so using strict inequality is okay but we can use <= for distance because point exactly on radius is not present; likewise angle exactly on border not present. So we can use <= with epsilon.
But if we use squared and dot conditions with an epsilon margin, we can treat as:
inside = (dist2 <= a_sq + eps) && (dot >= 0) && (dot*dot + eps >= dist2 * cos_half_sq).
But to avoid issues, we can just use < for strict? However, since boundaries are avoided, we can use <= without epsilon, and if a point happens to be extremely close but not within 0.001, floating rounding could misclassify. So using epsilon like 1e-9 or 1e-6 should be safe.
We'll use 1e-9.
But careful: dot^2 and dist2cos_half_sq might be subject to floating errors. So add a small epsilon: if dotdot + 1e-9 >= dist2 * cos_half_sq then considered >=.
Similarly for distance: if dist2 <= a_sq + 1e-9.
But we can also use double and not worry? The coordinates are integers, a and w integers. Computation with trig may produce small errors. So adding epsilon is safe.
Now the algorithm straightforward.
Now we need to output house indices (1-indexed) in ascending order with spaces.
Edge Cases:
-
If there are no other trees (U=M=S=0), then condition is simply if house is inside my plum sector. Then counts for each house are number of days it is inside my plum sector. Then max count could be >0. We output houses with that max.
-
If there are trees but maybe some days, house is inside my plum but also inside other tree; then not counted.
-
If for all houses count=0, output "NA".
Now what about ties? Output all indices in ascending order.
Now about input reading: The end is "0 0". But note that the first line of a dataset is H R. If H=0 and R=0, that's termination. There could be more lines after? Not.
Now we need to parse until we see "0 0". After reading H,R, if both zero, exit.
Otherwise, read H lines for houses. Then read a line for "U M S du dm ds". Then read U lines for plum trees, then M lines for peach, then S lines for cherry. Then read R lines for wind.
Now note: In the dataset after that, the next dataset starts. So we need to read sequentially.
We'll implement solve() that reads until 0 0.
Complexities fine.
Now I think about potential pitfalls.
-
The wind direction w is given as integer 0 ≤ w < 360. We'll convert to radians: w_rad = w * math.pi / 180.0.
-
For each day, compute cos_w = cos(w_rad), sin_w = sin(w_rad).
-
For my plum: du is given. Compute half_my = du/2 degrees -> rad = du/2 * pi/180 = du * pi/360. cos_half_my = cos(half_my). cos_half_my_sq = cos_half_my * cos_half_my.
-
For each other tree, compute its half based on its type.
-
However, we can compute per type cos_half_sq: for plum: use du; peach: dm; cherry: ds.
Now check for inside sector:
Define function is_inside(tx, ty, d_deg, hx, hy, ux, uy, a, cos_half_sq) where a is integer, a_sq precomputed, but we also need a_sq? Actually we can pass a_sq.
Pseudo:
def inside(tx, ty, d_half_cos_sq, hx, hy, ux, uy, a_sq):
dx = hx - tx
dy = hy - ty
dist2 = dxdx + dydy
if dist2 > a_sq + EPS: return False
dot = dxux + dyuy
if dot < -EPS: return False
# dot >= 0
if dot*dot + EPS < dist2 * d_half_cos_sq:
return False
return True
But careful: The condition dot^2 >= dist2 * cos_half_sq. We'll add EPS to dot*dot for >=.
Better: if dot*dot < dist2 * cos_half_sq - EPS: return False else True.
We'll set EPS = 1e-9.
But what about dot being zero? That's fine, but then dot^2 = 0. If dist2 * cos_half_sq > 0, then it's false. That's correct because if dot=0, angle=90°, which is > half if half<90. So false.
Now we also need to ensure dot is >=0; if dot is negative, the angle > 90°, which is > half (since half<90) so false. So we can incorporate: if dot < -EPS: return False.
Now note: The dot product is computed using the unit vector (ux, uy). So ux = cos(w_rad), uy = sin(w_rad). This is correct.
But is the sector defined such that the direction is exactly the wind direction? Yes.
Now also need to handle the case of d=180? It says d < 180, so maximum 179. So half < 89.5, always less than 90, so cos_half > 0. So okay.
Now test with example to ensure it matches manually computed results.
Now we should also consider the possibility that the house is exactly at the tree location? But they said all coordinates are distinct and nothing else at origin except my plum. For other trees, house could be at the tree? They said all coordinates entered shall be different. So no house coincides with any tree. So we won't have zero distance causing division by zero.
Now about boundaries: guarantee no house within 0.001 of boundary of any flower's scent region. So we can treat as strict; using epsilon ensures correct classification.
Now we need to be careful: For each day, the wind direction is given, but the strength a may be zero? It says 0 < a ≤ 100, so a>0. So no zero radius.
Now implementation steps:
- While true:
- read H,R
- if H==0 and R==0: break
- houses = [] list of (x,y)
- for i in range(H): read hx, hy; add to houses.
- read line U,M,S,du,dm,ds (floats? integers). All ints.
- other_trees = [] list of (tx,ty, cos_half_sq) (float)
- for i in range(U): read ux, uy; compute cos_half_sq for plum: cos_half = cos(du * math.pi / 360); cos_half_sq = cos_half * cos_half; add (ux,uy, cos_half_sq)
- for i in range(M): read mx, my; compute cos_half for peach: cos_half = cos(dm * math.pi / 360); cos_half_sq = cos_half * cos_half; add (mx,my, cos_half_sq)
- for i in range(S): read sx, sy; compute cos_half for cherry: cos_half = cos(ds * math.pi / 360); cos_half_sq = cos_half * cos_half; add (sx,sy, cos_half_sq)
- read wind records: list of (w,a)
- precompute my_plum_cos_half_sq = cos(du * math.pi / 360)**2
- counts = [0]*H
- For each wind (w,a):
- w_rad = w * math.pi / 180.0
- ux = cos(w_rad); uy = sin(w_rad)
- a_sq = a * a
- for each house index j, (hx, hy):
-
check my plum
- if not inside(0,0, my_plum_cos_half_sq, hx, hy, ux, uy, a_sq): continue
-
check other trees
- exclusive = True
- for each (tx,ty, cos_half_sq) in other_trees:
- if inside(tx, ty, cos_half_sq, hx, hy, ux, uy, a_sq):
exclusive = False
break
- if exclusive: counts[j] += 1
- after loops, find max_count = max(counts)
- if max_count == 0: print("NA")
- else: collect indices (starting from 1) where counts[i]==max_count, join with space, print.
Now we need to ensure that the inside function uses the same cos_half_sq for each tree.
But we also need to pass the d? Actually we only need cos_half_sq because we use the squared condition.
But note: For the condition, we compare dot^2 >= dist2 * cos_half_sq. However, this condition is derived from dot >= sqrt(dist2)*cos_half, which is equivalent if dot >= 0. So we need cos_half. Using cos_half_sq works.
But we also need to consider that the angle threshold is d/2. Our condition uses cos_half. That's fine.
But what about the case where d is very small? Works.
Thus we need to compute cos_half and square it.
Now test with example again manually using code later.
Now think about potential floating point issues: cos(90/2)=cos(45°) = sqrt(2)/2 ≈0.70710678. Square = 0.5. So for my plum with du=90, we have cos_half_sq=0.5. For other trees, compute accordingly.
Now check house 3 day1: we computed cherry inside. Let's test condition for cherry d=45 => half=22.5°, cos_half = cos(22.5°) ≈ 0.9238795, cos_half_sq ≈0.853553. a=6, a_sq=36. House3 (5,2) vs cherry (2,-2): dx=3, dy=4, dist2=25, <=36. dot = v · u = (3,4)·(cos45, sin45)=30.7071+40.7071=70.7071=4.9497. sqrt(dist2)=5. cos_half=0.9239 => sqrt(dist2)cos_half = 50.9239=4.6195. dot=4.9497 >= 4.6195 => inside. Our squared: dot^2=24.5, dist2cos_half_sq=25*0.85355=21.3388, dot^2 >=, so inside.
Now house2 day2 cherry: we got inside. Let's verify: d=45, half=22.5. House2 (1,2) vs cherry (2,-2): dx=-1, dy=4, dist2=17, sqrt=4.123. dot = (-1,4)·(0,1)=4. So dot=4, sqrtdot? dot >= sqrtcos_half? sqrtcos_half=4.1230.9239=3.809. dot=4>=3.809 => inside. dot^2=16, dist2cos_half_sq=170.85355=14.510, 16>=14.51 => inside.
Now house2 day2 cherry: our earlier manual said inside. Good.
Now house4 day2 cherry: we also inside. Good.
Now house2 day2 peach we computed not inside: d=30 half=15, cos_half=cos15≈0.9659, cos_half_sq≈0.933. House2 vs peach ( -3,0 ) to (1,2): dx=4, dy=2, dist2=20, sqrt=4.472, dot = (4,2)·(0,1)=2. dot=2. sqrtcos_half=4.4720.9659=4.321, so dot< required => not inside. Squared: dot^2=4, dist2cos_half_sq=200.933=18.66, so not.
Now we need to confirm that the condition dot>=0 is necessary. For example, if the point is exactly behind the tree relative to wind, dot negative. In such case even if dot^2 might be large, we shouldn't count. Our code will check dot<0 and return false.
Now check if any case where dot is positive but the angle difference is >90? Since cos_half>0, if dot>0 but angle >90? Not possible because dot>0 implies angle <90 (since cos positive for angle <90). So dot>0 ensures angle between -90 and 90 actually (since cos positive for |angle|<90). But could there be angle difference >90 but dot still positive? No, if the angle between vectors is >90, cos is negative, so dot negative. So dot>=0 ensures angle difference <=90, which is always true for half<90? Actually half may be up to <90, so possible that half could be e.g., 80°, then angle <=80 still possible with dot positive; but dot positive also allows angle 89°, which would be >80? cos(89)=0.017, so dot small. But the condition dot >= |v|cos_half ensures angle <= half. So we need both: dot>=0 and dot^2 >= dist2cos_half_sq. But dot>=0 may be redundant if we use squared because if dot is negative, dot^2 might be large but dot negative, so angle >90, which is definitely > half (since half<90). So we can simply check dot < 0 => false. So it's needed.
Now one more nuance: the sector is symmetric about w, so the condition angle <= d/2 is exactly dot >= |v| cos(d/2). This already implies dot >=0 when cos(d/2)>0 because RHS>0. So we can skip the explicit dot>=0 check if we also enforce dot >= sqrt(dist2)*cos_half. However, when using squared, we might need to enforce dot>=0 separately.
Thus final.
Now about multiple datasets, we need to reset variables.
Now edge case: U, M, S can be 0, so other_trees list may be empty.
Now verify second dataset: my plum du=90; but we have U=0 so no other plum. M=1, S=1. So other_trees have one peach (d=30) and one cherry (d=45). We computed.
Now note: In the example, they had "0 1 1 90 30 45" then lines "-3 0" for peach? Actually after reading U M S, we read U lines (0), then M lines (1) which is peach: "-3 0", then S lines (1) which is cherry: "2 -2". So correct.
Now let's test with the sample input programmatically to ensure output matches.
Now we need to be careful about reading: The input lines may have leading/trailing spaces. We'll use split.
Now also note: The input may have multiple spaces, but we use split.
Now we need to produce output for each dataset on a separate line.
Now let's think about possible pitfalls: The angle d is given in degrees and could be integer up to 179. So half could be not integer but we compute rad.
Now what about wind direction w=0? Then unit vector (1,0). Works.
Now is there any special case where the sector includes the opposite direction? The problem says the sector spreads symmetrically around the direction w, so it's exactly the set of points whose direction from the tree is within d/2 of w (mod 360). So we are correct.
Now potential issues: Using squared condition with cos_half_sq may cause precision issues if cos_half_sq is very small? cos_half for d close to 180? d<180, so half<90, cos_half > 0, smallest cos_half when half close to 90? e.g., d=179 => half=89.5°, cos ≈ 0.0087, square ~7.6e-5. Still okay.
Now we should consider using math.isclose? Not needed.
Now check if any house could be inside a tree's sector but also exactly at tree? They said coordinates distinct, so not.
Now about large distances: coordinates up to 1000, distances up to ~1414, but wind strength a up to 100. So many points outside just by distance.
Now Implementation details:
We'll use Python.
Pseudo-code:
import sys
import math
EPS = 1e-9
def inside(tx, ty, cos_half_sq, hx, hy, ux, uy, a_sq):
dx = hx - tx
dy = hy - ty
dist2 = dxdx + dydy
if dist2 > a_sq + EPS:
return False
dot = dxux + dyuy
if dot < -EPS:
return False
# dot >= 0
return dot*dot + EPS >= dist2 * cos_half_s