This page is still under construction.

Parts of this page are still being built. What you see may change.

Buffon's Needle

Time limit4sMemory limit1024 MB

Summary
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 π\pi. One famous method is Buffon's Needle: draw a vertical line at every integer xx coordinate in the plane (so there are lines x=0x = 0, x=1x = 1, x=−1x = -1, and so on) and drop a bunch of needles of length 11 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 π\pi. (The specific assumptions about exactly how the needles are dropped in the experiment are not important for this problem.)

In particular, let xx be the fraction of needles that intersect a vertical line. Then

π≈2x.\pi \approx \frac{2}{x}.

You are given the positions of NN needles (not necessarily random), and at least one of them intersects a vertical line. Using the formula above, find the corresponding approximation of π\pi.

Input

The first line of input contains a single integer NN (1≤N≤1041 \leq N \leq 10^4), the number of needles. NN lines follow.

The ii-th such line describes the ii-th needle. It contains four real numbers: xi1,yi1,xi2,yi2x_{i1}, y_{i1}, x_{i2}, y_{i2}. This means that the ii-th needle has one endpoint at (xi1,yi1)(x_{i1}, y_{i1}) in the plane and the other endpoint at (xi2,yi2)(x_{i2}, y_{i2}).

All real numbers in the input are given with exactly 66 digits after the decimal point and have absolute value at most 1010. Furthermore, no xx coordinate in the input is within 10−610^{-6} of an integer.

Since all the needles are of length 11, for each ii it is guaranteed that

∣(xi1−xi2)2+(yi1−yi2)2−1∣<10−3.|(x_{i1} - x_{i2})^2 + (y_{i1} - y_{i2})^2 - 1| < 10^{-3}.

Output

Output a single real number, the approximation of π\pi described above. Your answer is considered correct if its absolute or relative error is at most 10−610^{-6}.

Hint

In the first sample, the first needle intersects a vertical line (x=1x = 1), but the second does not. So the fraction of needles that intersect vertical lines is 1/21/2 and the corresponding approximation of π\pi is 44.

In the second sample, both needles intersect vertical lines (x=1x = 1 and x=0x = 0), so the fraction of needles that intersect vertical lines is 11 and the corresponding approximation of π\pi is 22.

Examples2

  1. Example 1

    Input
    2
    0.500000 0.500000 1.500000 0.500000
    0.400000 -0.200000 0.600000 0.779796
    
    Expected output
    4.000000000000
    
  2. Example 2

    Input
    2
    0.500000 0.500000 1.500000 0.500000
    2.250000 0.000000 1.542893 0.707107
    
    Expected output
    2.000000000000