Problem:
Score : 300 points

Problem Statement

There are N towns in a coordinate plane. Town i is located at coordinates (x_i, y_i). The distance between Town i and Town j is \sqrt{\left(x_i-x_j\right)^2+\left(y_i-y_j\right)^2}.

There are N! possible paths to visit all of these towns once. Let the length of a path be the distance covered when we start at the first town in the path, visit the second, third, \dots, towns, and arrive at the last town (assume that we travel in a straight line from a town to another). Compute the average length of these N! paths.

Constraints

2 \leq N \leq 8

-1000 \leq x_i \leq 1000

-1000 \leq y_i \leq 1000

\left(x_i, y_i\right) \neq \left(x_j, y_j\right) (if i \neq j)

(Added 21:12 JST) All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the average length of the paths.
Your output will be judges as correct when the absolute difference from the judge's output is at most 10^{-6}.

Sample Input 1

3
0 0
1 0
0 1

Sample Output 1

2.2761423749

There are six paths to visit the towns: 1 → 2 → 3, 1 → 3 → 2, 2 → 1 → 3, 2 → 3 → 1, 3 → 1 → 2, and 3 → 2 → 1.

The length of the path 1 → 2 → 3 is \sqrt{\left(0-1\right)^2+\left(0-0\right)^2} + \sqrt{\left(1-0\right)^2+\left(0-1\right)^2} = 1+\sqrt{2}.

By calculating the lengths of the other paths in this way, we see that the average length of all routes is:

\frac{\left(1+\sqrt{2}\right)+\left(1+\sqrt{2}\right)+\left(2\right)+\left(1+\sqrt{2}\right)+\left(2\right)+\left(1+\sqrt{2}\right)}{6} = 2.276142...

Sample Input 2

2
-879 981
-866 890

Sample Output 2

91.9238815543

There are two paths to visit the towns: 1 → 2 and 2 → 1. These paths have the same length.

Sample Input 3

8
-406 10
512 859
494 362
-955 -475
128 553
-986 -885
763 77
449 310

Sample Output 3

7641.9817824387

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::iter::FromIterator;
fn dist(a: (f64, f64), b: (f64, f64)) -> f64{
    ((a.0 - b.0).powi(2) + (a.1 - b.1).powi(2)).sqrt()
}

fn rra(pos: Vec<(f64, f64)>, s: (f64, f64)) -> f64{
    if pos.len() == 0{
        return 0f64;
    }

    let mut sum = 0f64;
    for &n in &pos{
        sum += dist(s, n);
        let left = Vec::from_iter(
            pos.iter().filter_map(|&x| if x == n{None}else{Some(x)}));
        sum += rra(left, n);
    }
    sum / (pos.len() as f64)
}

fn main(){
    let n: usize = linp();
    let mut pos: Vec<(f64, f64)> = Vec::new();
    for _ in 0 .. n{
        pos.push(linp());
    }
    let mut sum = 0f64;
    for &s in &pos{
        let left = Vec::from_iter(
            pos.iter().filter_map(|&x| if x == s{None}else{Some(x)}));
        sum += rra(left, s);
    }
    println!("{}", sum / (pos.len() as f64));
}

// easy IOs
trait SimpleParse{
    fn simple_parse(s: &str) -> Self;
    fn get_slice_index(s: &str) -> Option<usize>{
        s.find(|c|{c == ' ' || c == '\t' || c == '\n'})
    }
    fn next_vec_item<'a>(s: &'a str) -> Option<&'a str>{
        match s.find(|c|{!(c == ' ' || c == '\t' || c == '\n')}){
            Some(i) => Some(&s[i..]),
            None => None
        }
    }
}
macro_rules! new_parse{
    ( $t:ty ) => {
        impl SimpleParse for $t{
            fn simple_parse(s: &str) -> Self{
                s.trim().parse().expect("input parse error")
            }
        }
    }
}
macro_rules! new_tparse{
    ( $a:ident: $A:ident; $($x:ident: $X:ident, $y:ident: $Y:ident);* ) => {
        impl<$A, $($Y),*> SimpleParse for ($A, $($Y),*)
        where $A: SimpleParse, $($Y: SimpleParse),* {
            fn simple_parse($a: &str) -> Self{
                $(
                    let $x = $x.trim();
                    let ($x, $y) = $x.split_at(
                            $X::get_slice_index($x)
                            .expect("not enough inputs")
                    );
                )*
                ($A::simple_parse($a), $($Y::simple_parse($y)),*)
            }
            fn get_slice_index(s: &str) -> Option<usize>{
                let ret = $A::get_slice_index(s);
                $(
                    let ret = ret.expect("not enough inputs");
                    let ret = (&s[ret..])
                        .find(|c|{!(c == ' ' || c == '\t' || c == '\n')})
                        .expect("not enough inputs");
                    let ret = $Y::get_slice_index(&s[ret..]);
                )*
                ret
            }
        }
    }
}
impl SimpleParse for String{
    fn simple_parse(s: &str) -> String{
        String::from(s.trim())
    }
}
impl SimpleParse for char{
    fn simple_parse(s: &str) -> char{
        if s.len() != 1{
            panic!("char input error");
        }
        s.chars().nth(0).unwrap()
    }
    fn get_slice_index(s: &str) -> Option<usize>{
        if s.len() < 1{
            None
        }else{
            Some(1)
        }
    }
    fn next_vec_item<'a>(s: &'a str) -> Option<&'a str>{
        if s.len() == 0{
            None
        }else{
            Some(s)
        }
    }
}
impl<T> SimpleParse for Vec<T>
where T: SimpleParse{
    fn simple_parse(s: &str) -> Vec<T>{
        let mut ret: Vec<T> = Vec::new();
        let mut s = s.trim();
        if s.len() == 0{
            return ret;
        }
        loop{
            match T::get_slice_index(s){
                Some(i) => {
                    let (a, b) = s.split_at(i);
                    ret.push(T::simple_parse(a));
                    match T::next_vec_item(b){
                        Some(ss) => {s = ss;}
                        None => {return ret;}
                    }
                }
                None => {
                    ret.push(T::simple_parse(s));
                    return ret;
                }
            }
        }
    }
    fn get_slice_index(_: &str) -> Option<usize>{
        None
    }
}
impl<A> SimpleParse for (A,)
where A: SimpleParse {
    fn simple_parse(s: &str) -> Self{
        (A::simple_parse(s),)
    }
}
new_parse!(i8);
new_parse!(u8);
new_parse!(i16);
new_parse!(u16);
new_parse!(i32);
new_parse!(u32);
new_parse!(i64);
new_parse!(u64);
new_parse!(f32);
new_parse!(f64);
new_parse!(isize);
new_parse!(usize);
new_tparse!(a:A; a:A,b:B);
new_tparse!(a:A; a:A,b:B; b:B,c:C);
new_tparse!(a:A; a:A,b:B; b:B,c:C; c:C,d:D);
new_tparse!(a:A; a:A,b:B; b:B,c:C; c:C,d:D; d:D,e:E);
new_tparse!(a:A; a:A,b:B; b:B,c:C; c:C,d:D; d:D,e:E; e:E,f:F);
new_tparse!(a:A; a:A,b:B; b:B,c:C; c:C,d:D; d:D,e:E; e:E,f:F; f:F,g:G);
new_tparse!(a:A; a:A,b:B; b:B,c:C; c:C,d:D; d:D,e:E; e:E,f:F; f:F,g:G; g:G,h:H);
#[allow(dead_code)]
fn lin() -> String{
    let mut input = String::new();
    let _ = std::io::stdin().read_line(&mut input).expect("input error");
    input
}
#[allow(dead_code)]
fn linp<A: SimpleParse>() -> A{
    A::simple_parse(lin().trim())
}