Elastic Collisions
Time limit3sMemory limit256 MB
Two bodies on a line, mass 1 at rest and mass N^2 approaching, bounce elastically between each other and a wall; count the total collisions before both move right forever.
- Level
Medium7 of 10
- Topics
- Simulation, Math, Implementation, Number theory
- Solved
- No attempts yet
Problem
An elastic collision is a collision in which kinetic energy is conserved. On a line, two bodies with masses and move with velocities (here velocity includes direction) and and collide elastically; after the collision their velocities are
For example, if a body of mass approaches from the left with velocity and a body of mass approaches from the right with velocity , then after the elastic collision their velocities are and . Here the sign of a velocity is positive when the body moves from left to right. When a body collides elastically with a stationary wall, the body's speed stays the same and only its direction reverses, so only the sign of the velocity changes.
Now consider two bodies and moving on a line whose left end is blocked by a wall, as in the figure below. The mass of is 1 and it is initially at rest. The mass of is and it initially approaches from the right of . How many collisions occur in total (counting both collisions between and and collisions between the wall and )? Ignore all friction, and all collisions are elastic.

Input
The first line contains a positive integer . (1 ≤ ≤ 500)
Output
Print the total number of collisions.
Hint
Using Python's fractions module is strongly recommended.