<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

283. Mechanics
time limit per test: 0.25 sec.
memory limit per test: 65536 KB
input: standard
output: standard



On a flat table there are two circles having centers A and B, radii r_A and r_B, masses m_A and m_B and speeds v_A and v_B respectively:


You are given the initial positions, speeds, radii and masses of the circles. You have to find their positions and speeds after time t. It is guaranteed that the circles do not have any common points in the beginning and after time t.
You may use the following facts:
- The speeds of the circles change only if they hit each other.
- During a hit, the speeds may change only by vectors parallel to vector AB.
- The values P=m_A*v_A+m_B*v_B and 2E=m_A*|v_A|^2+m_B*|v_B|^2 do not change all the time, including hits.

Input
The first two lines of the input contain the descriptions of circles A and B respectively. The description of a circle consists of the coordinates of its center, and its speed, its radius and mass. The coordinates are integer and do not exceed 10^4 by absolute value. The radius is a positive integer and does not exceed 10^4. The mass is a positive integer and does not exceed 100. The third line of the input contains single integer t (0 <= t <= 100).

Output
Output the information about the circles after time t has passed in the same format as in the input, excluding the radius and the mass. The numbers must have at least 3 digits after the decimal point.

Sample test(s)

Input
0 1 1 0 2 1
5 0 0 0 2 1
5

Output
1.369 1.938 0.062 0.242
8.631 -0.938 0.938 -0.242
Author:	
Resource:	Novosibirsk SU Contest #2, by Novosibirsk Team #1
Date:

<|response|>
## 1. Abridged Problem Statement

Two circular disks move on a plane. For each disk, we know:

- initial center position,
- velocity vector,
- radius,
- mass.

The disks move with constant velocity unless they collide. During a collision, only velocity components along the line connecting their centers can change, and total momentum and kinetic energy are conserved.

Given a time `t`, find the positions and velocities of both disks after `t` seconds.

It is guaranteed that the disks do not intersect or touch initially and also do not intersect or touch after time `t`.

---

## 2. Key Observations

### Observation 1: There can be at most one collision

There are only two disks and no walls or external forces.

If they collide elastically, after the collision their relative velocity along the line of centers is reversed, so they start moving away from each other. Therefore, no second collision can happen.

---

### Observation 2: Use relative motion to find collision time

Let:

```text
p = posA - posB
v = velA - velB
R = rA + rB
```

In the reference frame of disk `B`, disk `A` moves from relative position `p` with relative velocity `v`.

The disks touch when the distance between centers becomes equal to `R`:

```text
|p + v*s| = R
```

Squaring both sides:

```text
|p + v*s|² = R²
```

Expanding:

```text
(v · v)s² + 2(p · v)s + (p · p - R²) = 0
```

This is a quadratic equation in time `s`.

---

### Observation 3: If no collision occurs before `t`, motion is simple

If the quadratic has no valid positive root in the interval `(0, t]`, then both disks simply move uniformly:

```text
posA += velA * t
posB += velB * t
```

Velocities do not change.

---

### Observation 4: Elastic collision affects only the normal component

At collision time, let:

```text
n = unit(posA - posB)
```

This is the unit vector along the line of centers.

Only velocity components along `n` change. The tangential components stay unchanged.

Let:

```text
rel = (velA - velB) · n
```

Then the elastic collision update is:

```text
velA -= n * (2 * mB / (mA + mB) * rel)
velB += n * (2 * mA / (mA + mB) * rel)
```

These formulas conserve both total momentum and kinetic energy.

---

## 3. Full Solution Approach

### Step 1: Read input

Each disk is described by:

```text
x y vx vy radius mass
```

The final output should contain only:

```text
x y vx vy
```

for each disk.

---

### Step 2: Compute possible collision time

Define:

```text
p = posA - posB
v = velA - velB
R = rA + rB
```

We solve:

```text
|p + v*s|² = R²
```

which gives:

```text
a = v · v
b = 2 * (p · v)
c = p · p - R²
```

So:

```text
a*s² + b*s + c = 0
```

Since the disks are initially separate, `c > 0`.

If `a = 0`, the relative velocity is zero, so the distance between disks never changes and there is no collision.

Otherwise, compute the discriminant:

```text
D = b² - 4ac
```

If `D < 0`, there is no collision.

Otherwise, the first contact time is:

```text
s = (-b - sqrt(D)) / (2a)
```

If:

```text
0 < s <= t
```

then a collision happens before or at time `t`.

Otherwise, no collision affects the answer.

---

### Step 3: Handle the no-collision case

If no collision happens in the required interval:

```text
posA = posA + velA * t
posB = posB + velB * t
```

The velocities remain unchanged.

---

### Step 4: Handle the collision case

Suppose the collision time is `hit`.

First, move both disks to the collision moment:

```text
posA += velA * hit
posB += velB * hit
```

Then compute the collision normal:

```text
n = unit(posA - posB)
```

Compute relative speed along this normal:

```text
rel = (velA - velB) · n
```

Apply the elastic collision formulas:

```text
velA -= n * (2 * mB / (mA + mB) * rel)
velB += n * (2 * mA / (mA + mB) * rel)
```

Finally, move both disks for the remaining time:

```text
remaining = t - hit

posA += velA * remaining
posB += velB * remaining
```

---

### Complexity

Only a constant number of arithmetic operations is needed.

```text
Time complexity:  O(1)
Memory complexity: O(1)
```

---

## 4. C++ Implementation with Detailed Comments

```cpp
#include <bits/stdc++.h>
using namespace std;

const double EPS = 1e-9;

struct Vec {
    double x, y;

    Vec(double x = 0.0, double y = 0.0) : x(x), y(y) {}

    Vec operator + (const Vec& other) const {
        return Vec(x + other.x, y + other.y);
    }

    Vec operator - (const Vec& other) const {
        return Vec(x - other.x, y - other.y);
    }

    Vec operator * (double k) const {
        return Vec(x * k, y * k);
    }

    Vec operator / (double k) const {
        return Vec(x / k, y / k);
    }

    // Dot product
    double dot(const Vec& other) const {
        return x * other.x + y * other.y;
    }

    // Squared length
    double norm2() const {
        return x * x + y * y;
    }

    // Length
    double norm() const {
        return sqrt(norm2());
    }

    // Unit vector in the same direction
    Vec unit() const {
        return *this / norm();
    }
};

struct Circle {
    Vec pos;
    Vec vel;
    double r;
    double m;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    Circle A, B;
    double t;

    // Input:
    // x y vx vy radius mass
    cin >> A.pos.x >> A.pos.y >> A.vel.x >> A.vel.y >> A.r >> A.m;
    cin >> B.pos.x >> B.pos.y >> B.vel.x >> B.vel.y >> B.r >> B.m;
    cin >> t;

    // Sum of radii. Collision happens when distance between centers equals R.
    double R = A.r + B.r;

    // Relative position and velocity of A with respect to B.
    Vec p = A.pos - B.pos;
    Vec v = A.vel - B.vel;

    // Solve |p + v*s|^2 = R^2.
    double a = v.dot(v);
    double b = 2.0 * p.dot(v);
    double c = p.dot(p) - R * R;

    // Collision time. -1 means no collision before time t.
    double hit = -1.0;

    // If relative velocity is zero, distance never changes.
    if (a > EPS) {
        double disc = b * b - 4.0 * a * c;

        // Allow a tiny negative value due to floating-point precision.
        if (disc >= -EPS) {
            if (disc < 0.0) {
                disc = 0.0;
            }

            // The smaller root is the first contact time.
            double s = (-b - sqrt(disc)) / (2.0 * a);

            // Collision must happen after time 0 and before or at time t.
            if (s > EPS && s <= t + EPS) {
                hit = min(s, t);
            }
        }
    }

    if (hit < 0.0) {
        // No collision: both disks move uniformly for the whole time.
        A.pos = A.pos + A.vel * t;
        B.pos = B.pos + B.vel * t;
    } else {
        // Move both disks to the collision moment.
        A.pos = A.pos + A.vel * hit;
        B.pos = B.pos + B.vel * hit;

        // Unit normal vector from B to A.
        Vec n = (A.pos - B.pos).unit();

        // Relative velocity along the normal.
        double rel = (A.vel - B.vel).dot(n);

        // Elastic collision update.
        A.vel = A.vel - n * (2.0 * B.m / (A.m + B.m) * rel);
        B.vel = B.vel + n * (2.0 * A.m / (A.m + B.m) * rel);

        // Move for the remaining time after the collision.
        double remaining = t - hit;

        A.pos = A.pos + A.vel * remaining;
        B.pos = B.pos + B.vel * remaining;
    }

    // At least 3 digits after decimal are required.
    // Printing 6 is safe.
    cout << fixed << setprecision(6);

    cout << A.pos.x << ' ' << A.pos.y << ' '
         << A.vel.x << ' ' << A.vel.y << '\n';

    cout << B.pos.x << ' ' << B.pos.y << ' '
         << B.vel.x << ' ' << B.vel.y << '\n';

    return 0;
}
```

---

## 5. Python Implementation with Detailed Comments

```python
import sys
import math

EPS = 1e-9


class Vec:
    def __init__(self, x=0.0, y=0.0):
        self.x = float(x)
        self.y = float(y)

    def __add__(self, other):
        return Vec(self.x + other.x, self.y + other.y)

    def __sub__(self, other):
        return Vec(self.x - other.x, self.y - other.y)

    def __mul__(self, k):
        return Vec(self.x * k, self.y * k)

    def __truediv__(self, k):
        return Vec(self.x / k, self.y / k)

    def dot(self, other):
        return self.x * other.x + self.y * other.y

    def norm2(self):
        return self.x * self.x + self.y * self.y

    def norm(self):
        return math.sqrt(self.norm2())

    def unit(self):
        return self / self.norm()


def main():
    data = sys.stdin.read().strip().split()

    if not data:
        return

    data = list(map(float, data))

    idx = 0

    # Read disk A.
    pos_a = Vec(data[idx], data[idx + 1])
    idx += 2

    vel_a = Vec(data[idx], data[idx + 1])
    idx += 2

    r_a = data[idx]
    idx += 1

    m_a = data[idx]
    idx += 1

    # Read disk B.
    pos_b = Vec(data[idx], data[idx + 1])
    idx += 2

    vel_b = Vec(data[idx], data[idx + 1])
    idx += 2

    r_b = data[idx]
    idx += 1

    m_b = data[idx]
    idx += 1

    # Read required time.
    total_time = data[idx]

    # Collision distance between centers.
    R = r_a + r_b

    # Relative position and relative velocity.
    p = pos_a - pos_b
    v = vel_a - vel_b

    # Quadratic equation:
    # |p + v*s|^2 = R^2
    a = v.dot(v)
    b = 2.0 * p.dot(v)
    c = p.dot(p) - R * R

    hit = None

    # If relative velocity is not zero, solve the quadratic.
    if a > EPS:
        disc = b * b - 4.0 * a * c

        # Small negative values may appear due to floating-point error.
        if disc >= -EPS:
            if disc < 0.0:
                disc = 0.0

            # First possible contact time.
            s = (-b - math.sqrt(disc)) / (2.0 * a)

            if s > EPS and s <= total_time + EPS:
                hit = min(s, total_time)

    if hit is None:
        # No collision before target time.
        pos_a = pos_a + vel_a * total_time
        pos_b = pos_b + vel_b * total_time
    else:
        # Move to collision time.
        pos_a = pos_a + vel_a * hit
        pos_b = pos_b + vel_b * hit

        # Normal vector along the line of centers.
        n = (pos_a - pos_b).unit()

        # Relative velocity projected onto the normal.
        rel = (vel_a - vel_b).dot(n)

        # Elastic collision update.
        vel_a = vel_a - n * (2.0 * m_b / (m_a + m_b) * rel)
        vel_b = vel_b + n * (2.0 * m_a / (m_a + m_b) * rel)

        # Move for the rest of the time.
        remaining = total_time - hit

        pos_a = pos_a + vel_a * remaining
        pos_b = pos_b + vel_b * remaining

    # Output final position and velocity of A and B.
    # The problem requires at least 3 digits after the decimal point.
    print(f"{pos_a.x:.6f} {pos_a.y:.6f} {vel_a.x:.6f} {vel_a.y:.6f}")
    print(f"{pos_b.x:.6f} {pos_b.y:.6f} {vel_b.x:.6f} {vel_b.y:.6f}")


if __name__ == "__main__":
    main()
```