Under construction

Still being built. Things may change

Celestial Map

Time limit2sMemory limit512 MB

Summary
Count stars that, d years ago, lay in a plane through the origin and were exactly d from the origin.
Level

Medium4 of 10

Topics
Geometry, Math, Implementation
Solved
No attempts yet

Problem

Bob works at a center for interplanetary communication. Today a signal from an unknown civilization reached his station. Panel 42 should show the coordinates of the point the signal came from, but instead it shows two vectors, one number, and the warning code 54816. The manual explains the code: computing the exact coordinates failed, so the station computed the plane the signal came from together with the distance to its origin.

Bob sits at the origin (0,0,0)(0, 0, 0). The two given vectors start there and span a plane. The number dd is the distance from Bob to the point the signal came from. A signal travels at the speed of light, so a signal arriving now was sent dd years ago, and at that moment the star that sent it was inside the plane and at distance exactly dd from the origin.

Humanity has a complete map of every star: the current position of each star and the constant velocity vector along which it travels. Bob wants to plan his afternoon, so he first wants to know how many stars he has to check by hand. Count the stars that were inside the given plane and at distance exactly dd from the origin exactly dd years ago.

Input

The first line contains an integer nn and a real number dd (1≤n≤1041 \le n \le 10^4, 1.0≤d≤1051.0 \le d \le 10^5), where nn is the number of stars in the universe and dd is the distance from Bob to the point the signal came from.

Each of the next two lines contains three real numbers pxp_x, pyp_y, and pzp_z (0.0≤px,py,pz≤10.00.0 \le p_x, p_y, p_z \le 10.0, ∣(px,py,pz)∣≥0.1|(p_x, p_y, p_z)| \ge 0.1). The two vectors (px,py,pz)(p_x, p_y, p_z) are not parallel and they span a plane through Bob's location. The point the signal came from was inside that plane when the signal was sent.

The next 2n2n lines describe the stars, two lines per star.

  • The first line contains three real numbers sxs_x, sys_y, and szs_z (−106≤sx,sy,sz≤106-10^6 \le s_x, s_y, s_z \le 10^6). (sx,sy,sz)(s_x, s_y, s_z) is the current position of the star.
  • The second line contains three real numbers txt_x, tyt_y, and tzt_z (0.1≤∣(tx,ty,tz)∣≤0.950.1 \le |(t_x, t_y, t_z)| \le 0.95). (tx,ty,tz)(t_x, t_y, t_z) is the trajectory of the star, and ∣(tx,ty,tz)∣|(t_x, t_y, t_z)| is the speed at which it travels.

You can assume the following.

  • The other civilization's signal travels at the speed of light.
  • All speeds are given in lightyears per year, and all distances are in lightyears.
  • Bob's location is fixed at (0,0,0)(0, 0, 0).
  • Stars never collide.
  • Among the points where the given plane meets a star trajectory, none is at distance d±0.1d \pm 0.1 lightyears from the origin except for points at distance exactly dd lightyears.
  • A star that was not inside the plane at the time the signal was sent was at least 0.1 lightyears away from the plane.
  • Every trajectory meets the given plane at an angle between 10 and 90 degrees.

Output

Print the number of stars that fit the description.

Note

The first example has 2 stars. The current star positions, the given plane, and the two trajectories are shown in figure 1. The plane the signal came from is spanned by the vectors (1.0,0.0,0.0)(1.0, 0.0, 0.0) and (0.0,1.0,0.0)(0.0, 1.0, 0.0), and the distance is 2.02.0. The first star is currently at (2.0,0.0,1.0)(2.0, 0.0, 1.0) and moves in direction (0.0,0.0,1.0)(0.0, 0.0, 1.0) at speed 0.50.5. The second star is currently at (0.0,2.0,1.0)(0.0, 2.0, 1.0) and moves in the same direction at speed 0.70.7. Moving both stars back by two years gives the positions in figure 2. Star 1 is inside the plane and its distance to Bob is 2.02.0, so it fits the description. Star 2 is not inside the plane, so it does not fit. The answer for the first example is 1.

Figure 1: star positions in the present for the first example.

Figure 2: star positions two years ago for the first example.

Examples5

  1. Example 1

    Input
    2 2.0
    1.0 0.0 0.0
    0.0 1.0 0.0
    2.0 0.0 1.0
    0.0 0.0 0.5
    0.0 2.0 1.0
    0.0 0.0 0.7
    
    Expected output
    1
    
  2. Example 2

    Input
    1 2.0
    1.0 0.0 0.0
    0.0 1.0 0.0
    2.0 0.0 2.0
    0.0 0.0 0.5
    
    Expected output
    0
    
  3. Example 3

    Input
    1 3
    1 0 0
    0 1 1
    1.6 2.3 2.6
    0.2 0.1 0.2
    
    Expected output
    1
    
  4. Example 4

    Input
    4 5
    1 0 0
    0 1 0
    3 4 2.5
    0 0 0.5
    0.25 0 6.5
    0.05 0 0.3
    1 0.5 1.5
    0 0.1 0.3
    -4 0 -2
    0.2 0 -0.4
    
    Expected output
    2
    
  5. Example 5

    Input
    1 1
    1 0 0
    0 1 0
    0.6 0.8 0.9
    0 0 0.9
    
    Expected output
    1