Buffon's Needle
Time limit4sMemory limit1024 MB
Given N unit-length needle segments, count how many cross a vertical line x = k, then output 2 divided by that fraction.
- Level
Easy2 of 10
- Topics
- Geometry, Implementation, Math
- Solved
- No attempts yet
Problem
For millennia, people have been interested in approximating . One famous method is Buffon's Needle: draw a vertical line at every integer coordinate in the plane (so there are lines , , , and so on) and drop a bunch of needles of length on it. Each needle either intersects a vertical line or lies entirely between two vertical lines. It turns out that if the needles are dropped randomly, the fraction of needles that intersect a vertical line can be used to approximate . (The specific assumptions about exactly how the needles are dropped in the experiment are not important for this problem.)
In particular, let be the fraction of needles that intersect a vertical line. Then
You are given the positions of needles (not necessarily random), and at least one of them intersects a vertical line. Using the formula above, find the corresponding approximation of .
Input
The first line of input contains a single integer (), the number of needles. lines follow.
The -th such line describes the -th needle. It contains four real numbers: . This means that the -th needle has one endpoint at in the plane and the other endpoint at .
All real numbers in the input are given with exactly digits after the decimal point and have absolute value at most . Furthermore, no coordinate in the input is within of an integer.
Since all the needles are of length , for each it is guaranteed that
Output
Output a single real number, the approximation of described above. Your answer is considered correct if its absolute or relative error is at most .
Hint
In the first sample, the first needle intersects a vertical line (), but the second does not. So the fraction of needles that intersect vertical lines is and the corresponding approximation of is .
In the second sample, both needles intersect vertical lines ( and ), so the fraction of needles that intersect vertical lines is and the corresponding approximation of is .