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



Sample input:
3
0 0
1 0
0 1


Sample output:
2.2761423749

Submission status: Accepted

Solution code:
import math
# 町kと町jとの距離を算出する関数
def distance(k,j):
    return math.sqrt((x[k]-x[j])**2 + (y[k]-y[j])**2)

x,y=[],[]
N = int(input())
for i in range(N):
    a, b = map(int, input().split())
    x.append(a)
    y.append(b)

sum = 0.00
for k in range(N):
    for j in range(N):
        if k != j:
            sum += distance(k,j)
print(sum/N)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
import math
sys.setrecursionlimit(200000)
input = sys.stdin.readline

N, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

l = []
def f(al):
    if len(al) == N:
        l.append(al)
    for i in range(N):
        if i not in al:
            f(al + [i])
for i in range(N):
    f([i])

ans = 0
for v in l:
    a = 0
    for i in range(len(v)-1):
        x = L[v[i]]
        y = L[v[i + 1]]
        a += ((x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2) ** 0.5

    ans += a
print(ans / len(l))

Submission status: Accepted

Solution code:
n = int(input())
points = []
for i in range(n):
  points.append(tuple(map(int, input().split())))
res = 0
for i in range(n):
  for j in range(i+1, n):
    res += ((points[i][0] - points[j][0])**2 + (points[i][1] - points[j][1])**2)**.5
print(res * 2 / n)

Submission status: Accepted

Solution code:
import itertools as itr
n = int(input())
xy = []
for i in range(n):
    x, y = map(int, input().split(" "))
    xy.append([x, y])
all = list(itr.permutations(xy))
n_n = 1
ans = []
for i in range(2,n+1):
    n_n = n_n*i
for line in all:
    r = [0] * (n+1)
    x = [0]*(n+1)
    y = [0]*(n+1)
    for i in range(n-1):
        x[i+1] = line[i+1][0] - line[i][0]
        y[i+1] = line[i+1][1] - line[i][1]
        r[i+1] = (x[i+1]**2 + y[i+1]**2)**(1/2)
    ans.append(sum(r))
print(sum(ans)/n_n)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
x=[]
for i in range(n):
	X=list(map(int,input().split()))
	x.append(X)
x=np.array(x)
ans=0
for i in range(n):
	for j in range(i+1,n):
		d=np.linalg.norm(x[i]-x[j]) #ベクトルの距離を計算
		ans+=d
#nc2=n*(n-1)/2
print('{:.8f}'.format(ans*2/n)) #桁数設定

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
p = []
for i in range(n):
    p.append(list(map(int, input().split())))
#d = [[0]*n for x in range(n)]
s = 0
m = 1
for i in range(1, n):
    m = m * i
for i in range(n):
    for j in range(i + 1, n):
        #d[i][j] = ((p[j][0] - p[i][0])**2 + (p[j][1] - p[i][1])**2)**0.5
        s += ((p[j][0] - p[i][0])**2 + (p[j][1] - p[i][1])**2)**0.5*m
        #print("i:{}, j:{} / ({},{}) - ({},{}) d:{}".format(i, j, p[i][0], p[i][1], p[j][0], p[j][1], d[i][j]))
l = m * n / 2
print(s / l)

Submission status: Accepted

Solution code:
point = []

t=int(input())

import math

def distance(x1,y1,x2,y2):
    return math.pow(math.pow(x1-x2,2)+math.pow(y1-y2, 2),0.5)

while t:
    a,b = map(int, input().split())
    point.append((a,b))
    t-=1

u = len(point)

s= 0

for i in range(u):
    for j in range(u):
        if i==j:
            continue
        s += distance(point[i][0],point[i][1],point[j][0],point[j][1])


print(s/u)

Submission status: Accepted

Solution code:
def ints():
  return [int(x) for x in input().split()]
def readi():
  return int(input())

def permutation(a, aa=[]):
  if len(a)<=1:
    return [aa+a]

  res = []
  for x in a:
    b = a.copy()
    b.remove(x)
    res.extend(permutation(b, aa+[x]))

  return res

N = readi()
P = [ints() for i in range(N)]

s = 0.0
ps = permutation(list(range(N)))
pn = len(ps)

for p in ps:
  for i in range(N-1):
    q = p[i]
    r = p[i+1]
    s += ((P[q][0]-P[r][0])**2+(P[q][1]-P[r][1])**2)**(0.5)/pn
print(s)

Submission status: Accepted

Solution code:
def dist(a, b):
  return ( (a[0]-b[0])**2 + (a[1]-b[1])**2 )**0.5

N = int(input())
xy = []

for _ in range(N):
  x, y = map(int, input().split())
  xy.append([x,y])

di2 = 0.0
for i in range(N):
  for j in range(N):
    if i >= j:
      continue
    else:
      di2 += dist(xy[i], xy[j])

print(2.0*di2/N)

Submission status: Accepted

Solution code:
import numpy as np
def dist(ax, ay, bx, by):
  ans = np.sqrt( (ax-bx)**2 +(ay-by)**2 )
  return ans

N = int(input())
xs = []
ys = []

for _ in range(N):
  x, y = map(int, input().split())
  xs.append(x)
  ys.append(y)

di2 = 0.0
for i in range(N):
  for j in range(N):
    if i >= j:
      continue
    else:
      di2 += dist(xs[i], ys[i], xs[j], ys[j])

print(2.0*di2/N)

Submission status: Accepted

Solution code:
N=int(input())
A=[]
for l in range(N):
  array = list(map(int, input().split()))
  A.append(array)
import math
import itertools as ir
x=0
y=1
#街u,vの距離
def distance(u,v):
  distance = math.sqrt( (A[u][x]-A[v][x])**2 + (A[u][y]-A[v][y])**2 )
  return distance
#街リストの生成（街0からN-1）
city_list=[]
for i in range(N):
  city_list.append(i)
#街のまわり方の並べ方
permutations=list(ir.permutations(city_list))
#距離の計算(一パターンを組み合わせ分だけ）
sum_distance=0
for j in range(math.factorial(N)):
  for k in range(N-1):
    sum_distance+=distance(permutations[j][k],permutations[j][k+1])
result=sum_distance/math.factorial(N)
print(result)

Submission status: Accepted

Solution code:
import itertools
import math

n = int(input())
x = [None for i in range(n)]
y = [None for i in range(n)]

for i in range(n):
  x[i], y[i] = [int(char) for char in input().split()]

towns = [i for i in range(n)]

sum = 0

for arranged_towns in itertools.permutations(towns):
  current_town = arranged_towns[0]

  for next_town in arranged_towns[1:]:
    sum += math.sqrt((x[current_town] - x[next_town])** 2
              + (y[current_town] - y[next_town]) ** 2)
    current_town = next_town

result = sum / math.factorial(n)

print(result)

Submission status: Accepted

Solution code:
n = int(input())
xy = []
for _ in range(n):
  x, y = map(int, input().split())
  xy.append([x, y])

sm = 0
def distance(a, b):
  return ((a[0]-b[0])**2 + (a[1]-b[1])**2) ** 0.5

for i in range(n):
  for j in range(n):
    if i == j:
      continue
    sm += distance(xy[i], xy[j])
    # print(i, xy[i], j, xy[j], distance(xy[i], xy[j]))
print(sm / n )

Submission status: Accepted

Solution code:
from itertools import permutations 
N = int(input())

point = [] 
for i in range(N):
    x,y = map(int, input().split())
    point.append([x,y])


perm = list(range(N))

count = 0
distsum = 0
for i in permutations(perm):
    for j, k in zip(i, i[1:]):
        x1, y1 = point[j]
        x2, y2 = point[k]
        distsum += ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
    count += 1

print(distsum / count)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import bisect

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(input())
xy = [[] for i in range(N)]
for i in range(N):
    xy[i] = LI()

distances = []
def distance(town, town2):
    return ((town[0] - town2[0])**2 + (town[1] - town2[1])**2)**0.5

for i in range(N):
    for j in range(i+1,N):
        distances.append(distance(xy[i], xy[j]))

print(sum(distances) / N * 2)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
X = [list(map(int, input().split())) for i in range(N)]

ans = []
for i in range(N):
    for j in range(N):
        ans.append(np.sqrt((X[i][0]-X[j][0])**2 + (X[i][1]-X[j][1])**2))

print(sum(ans) / N)

Submission status: Accepted

Solution code:
import math,itertools
n = int(input())
xy = []
L=0
for i in range(n):
    xy.append(list(map(int, input().split())))
for k in itertools.permutations(xy):
    for (x1,y1),(x2,y2) in zip(k,k[1:]):
        L+=((x1-x2)**2+(y1-y2)**2)**0.5
print(L/math.factorial(n))

Submission status: Accepted

Solution code:
import numpy as np 
import math

x_list = np.array([])
y_list = np.array([])

n = int(input())

for i in range(n):
    x,y = map(int, input().split())
    x_list = np.append(x_list, x)
    y_list = np.append(y_list, y)

X = x_list[:, None] - x_list[None, :]
Y = y_list[:, None] - y_list[None, :]

print(np.sqrt(X ** 2 + Y ** 2).sum() / n)

Submission status: Accepted

Solution code:
N=int(input())
x = []
y = []
for i in range(N):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)

import itertools
import math
l=range(0,N)
distance=[]
for v in itertools.permutations(l,N):
    summary=0
    for w in range(len(v)-1):
        tmp=math.sqrt((x[v[w]]-x[v[w+1]])**2+(y[v[w]]-y[v[w+1]])**2)
        summary+=tmp
    distance.append(summary)

print('{:.7f}'.format(sum(distance)/math.factorial(N)))

Submission status: Accepted

Solution code:
from sys import stdin
import math

N = int(input())
xy = []
for _ in range(N):
    xy.append(list( map(int,input().split())))

sum = 0
for i in range(N-1):
    for j in range(i+1,N):
        sum += math.sqrt((xy[i][0] - xy[j][0])**2 + (xy[i][1] - xy[j][1])**2)

print(sum * 2 / N)

Submission status: Accepted

Solution code:
import math

N = int(input())
D = []

for k in range(N):
    x,y = map(int,input().split())
    D.append([x,y])

ans = 0

for i in range(N):
    for j in range(N):
        ans += math.sqrt((D[i][0]-D[j][0])**2 +(D[i][1]-D[j][1])**2)

print(ans/N)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
arr = []

for i in range(n):
    x,y = map(int, input().split(' '))
    arr.append([x,y])

l=0
for i in range(n-1):
    for j in range(i+1, n):
        x_i,y_i = arr[i][0],arr[i][1]
        x_j,y_j = arr[j][0],arr[j][1]
        l += ((x_i-x_j)**2 + (y_i-y_j)**2)**0.5

print((2*l)/n)

Submission status: Accepted

Solution code:
n = int(input())

C = []
for _ in range(n):
    x, y = map(int, input().split())
    C.append((x, y))

l = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        xi, yi = C[i][0], C[i][1]
        xj, yj = C[j][0], C[j][1]
        l += ((xi - xj)**2 + (yi - yj)**2)**0.5

print((2 * l) / n)

Submission status: Accepted

Solution code:
import sys
import math

def main():
    N = int(input())
    xy=[]
    for _ in range(N):
        xy.append(list(map(int,input().split())))

    sqs=[]
    for i in range(0,len(xy)):
        for j in range(i+1,len(xy)):
            sqs.append(math.sqrt((xy[i][0]-xy[j][0])**2 + (xy[i][1]-xy[j][1])**2))

    print(sum(sqs) * 2 / N)

main()

Submission status: Accepted

Solution code:
import math
def length(list1,list2):
    sum = math.sqrt((list1[0] - list2[0])**2 + (list1[1] - list2[1])**2)
    return(sum)


num = int(input())
data = ['' for i in range(num)]
for i in range(num):
    input_data = input().split(' ')
    int_data = [int(input_data[l]) for l in range(2)]
    data[i] = int_data

result = 0
for i in range(num):
    for l in range(num - i - 1):
        result += length(data[i],data[l + 1 + i])

result = result * 2 / num
print(result)

Submission status: Accepted

Solution code:
import math
import itertools

def distance(p1, p2):
    return math.sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)

n = int(input())
points = {i: list(map(int,  input().split())) for i in range(n)}

total_distance = 0

for cource in itertools.permutations(range(n)):
    for i in range(1, n):
        total_distance += distance(points[cource[i-1]], points[cource[i]])

print(total_distance/math.factorial(n))

Submission status: Accepted

Solution code:
N = int(input())
X = []
Y = []
for i in range(N):
    x,y = map(int,input().split())
    X += [x]
    Y += [y]
L = 0 
for i in range(N-1):
    for j in range(i+1,N):
        L += (((X[i] - X[j]) ** 2 ) + ((Y[i] - Y[j] ) ** 2 ) ) ** 0.5 
        
print(L*2/N)

Submission status: Accepted

Solution code:
N=int(input())
X=[]
Y=[]
for i in range(N):
  x,y=map(int,input().split())
  X +=[x]
  Y +=[y]
L=0#全点同士の距離の合計
for i in range(N-1):
  for j in range(i+1,N):
    L+= (((X[i]-X[j])**2)+((Y[i]-Y[j])**2))**0.5
    
print(L*2/N)

Submission status: Accepted

Solution code:
n = int(input())
x = []
y = []
ans = 0

for _ in range(n):
    a, b = map(int, input().split())
    x.append(a)
    y.append(b)

for i in range(n):
    for j in range(1, n-i):
        ans += ((x[i]-x[i+j])**2 + (y[i]-y[i+j])**2)**0.5
      
print(ans*2/n)

Submission status: Accepted

Solution code:
N = int(input())
XY = [list(map(int, input().split())) for _ in range(N)]

def distance(x1, y1, x2, y2):
    dist = ((x1-x2)**2 + (y1-y2)**2) ** (1/2)
    return dist

def factorial(n):
    if n == 1:
        return n
    else:
        return n * factorial(n-1)

ans = 0
for i in range(N):
    for j in range(N):
        if i == j:
            continue
        else:
            x1 = XY[i][0]
            y1 = XY[i][1]
            x2 = XY[j][0]
            y2 = XY[j][1]
            ans += distance(x1, y1, x2, y2)
#            print('============')
#            print(x1, y1, x2, y2)
#            print(distance(x1, y1, x2, y2))

# xxxx
ans /= 2
ans *= 2/N
print(ans)

Submission status: Accepted

Solution code:
import numpy as np


def main():
    N = int(input())
    towns = [input().split() for _ in range(N)]
    # 8 * 2
    towns = np.array(towns).astype(np.int64)
    # 8 * 1 * 2 - 1 * 8 * 2 = 8 * 8 * 2
    diffs = np.expand_dims(towns, axis=1) - np.expand_dims(towns, axis=0)
    # 8 * 8
    distances = np.sqrt(np.sum(diffs ** 2, axis=-1))
    # distance.mean() * N でもよさそう
    ans = distances.sum(axis=1).mean()
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np


def main():
    N = int(input())
    towns = [input().split() for _ in range(N)]
    towns = np.array(towns).astype(np.int64)
    diffs = np.expand_dims(towns, axis=1) - np.expand_dims(towns, axis=0)
    distances = np.sqrt(np.sum(diffs ** 2, axis=-1))
    ans = distances.sum(axis=1).mean()
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

n = int(input())
xy = []
for i in range(n):
  l = list(map(int, input().split()))
  xy.append(l)
d = 0
  
for i in range(n-1):
  for j in range(i+1, n):
    d += ((xy[i][0]-xy[j][0])**2+(xy[i][1]-xy[j][1])**2)**0.5

print(d*2*math.factorial(n-1)/math.factorial(n))

Submission status: Accepted

Solution code:
import math

n = int(input())
x, y = [0] * n, [0] * n
for i in range(n):
  x[i], y[i] = map(int, input().split())
s = 0
for i in range(n):
  for j in range(n):
    s += math.sqrt((x[i] - x[j]) ** 2 + (y[i] - y[j]) ** 2)
print(s / n)

Submission status: Accepted

Solution code:
import math
N = int(input())
x = [0] * N
y = [0] * N
for i in range(N):
    x[i], y[i] = map(int,input().split())
nkai = 1
#N!の値を求める
for i in range(1,N+1):
    nkai *= i
sum_dict = 0

#同じ辺が登場する回数N!*(N-1)/(len(branch))
#全ての返間の距離 全ての辺の数はNC2(len(branch))

branch = list()
for i in range(N):
    for j in range(i+1,N):
        a = (x[i] - x[j])**2 + (y[i]-y[j])**2
        a = math.sqrt(a)
        branch.append(a)
#普遍性なし?
count = (nkai*(N-1))/(len(branch))
for i in range(len(branch)):
    sum_dict += branch[i]*(count)


#平均を求める
ave = sum_dict / nkai
print(ave)

Submission status: Accepted

Solution code:
import itertools


n = int(input())

grid = [list(map(int,input().split())) for i in range(n)]

permutations = list(itertools.permutations(range(1,n+1),n))
permutations_n = len(permutations)

distances = []


for i in range(permutations_n):
    distance = 0
    
    for j in range(n-1):
        distance += (((grid[permutations[i][j]-1][0])-(grid[permutations[i][j+1]-1][0]))**2 + ((grid[permutations[i][j]-1][1])-(grid[permutations[i][j+1]-1][1]))**2)**(1/2)

    distances.append(distance)

print(sum(distances)/permutations_n)

Submission status: Accepted

Solution code:
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
ans = 0
import itertools
for path in itertools.permutations([i for i in range(n)]):
  for i in range(n-1):
    next = a[path[i+1]]
    now = a[path[i]]
    ans += ((next[1]-now[1])**2 + (next[0]-now[0])**2)**(1/2)

for i in range(n, 0, -1):
  ans /= i
print(ans)

Submission status: Accepted

Solution code:
import itertools
import math

N = int(input())
xbox = [list(map(int,input().split())) for _ in range(N)]
lst = list(itertools.permutations(xbox))

num = 0
for i in lst:
    for j in range(N-1):
        line = ((i[j][0]-i[j+1][0])**2 +  (i[j][1]-i[j+1][1])**2)**0.5
        num += line
print(num/len(lst))

Submission status: Accepted

Solution code:
import numpy as np
import itertools
from itertools import permutations

N = int(input())
XY = [list(map(int, input().split())) for i in range(N)]
Distance = []
for v in itertools.permutations(XY, N):
  distance_each = [np.sqrt((v[i][0] - v[i + 1][0])**2 + (v[i][1] - v[i + 1][1])**2) for i in range(N - 1)]
  distance = np.sum(distance_each)
  Distance.append(distance)
ans = np.average(Distance)
print(ans)

Submission status: Accepted

Solution code:
from math import sqrt
N = int(input())
A = []
for i in range(N):
  A.append(list(map(int, input().split())))
  
avg = 0
for i in range(N):
  for j in range(i+1,N):
    avg += sqrt( (A[i][0]-A[j][0]) ** 2 + (A[i][1]-A[j][1]) ** 2) * 2 / N
    #print(i,j)
    
print(avg)

Submission status: Accepted

Solution code:
n=int(input())
a=[tuple(map(int,input().split())) for i in range(n)]
ans=0
for i in range(n):
  for j in range(i+1,n):
    ans += ((a[i][0] - a[j][0])**2 +(a[i][1] - a[j][1])**2 )**0.5
    
print(ans / n*2)

Submission status: Accepted

Solution code:
from itertools import combinations, permutations


n = int(input())
xy = [[int(i) for i in input().split()] for _ in range(n)]

edges = [[0] * n for _ in range(n)]
for i, j in combinations(range(n), 2):
    xi, yi = xy[i]
    xj, yj = xy[j]
    distance = ((xi - xj)**2 + (yi - yj)**2)**0.5
    edges[i][j] = edges[j][i] = distance

totals = []
for node in permutations(range(n)):
    total = 0
    for i in range(1, n):
        total += edges[node[i-1]][node[i]]
    totals.append(total)

print(sum(totals) / len(totals))

Submission status: Accepted

Solution code:
N = int(input())
XY = [list(map(int,input().split())) for _ in range(N)]

dist = 0
for x1,y1 in XY:
    for x2,y2 in XY:
        dist += ((x1-x2)**2+(y1-y2)**2)**(1/2)
print(dist/N)

Submission status: Accepted

Solution code:
import sys
import itertools
import bisect
import math
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
li = [na() for _ in range(n)]
tmp = list(itertools.permutations(li))
l = len(tmp)
s = 0
for rt in tmp:
    for i in range(n-1):
        s += ((rt[i+1][0] - rt[i][0]) ** 2 + (rt[i+1][1] - rt[i][1]) ** 2) ** 0.5

print(s/l)

Submission status: Accepted

Solution code:
N = int(input())
li = [list(map(int, input().split())) for x in range(N)]
length = 0
for i in range(len(li)):
	for j in range(len(li)):
		if i != j:
			length += ((li[i][0]-li[j][0])**2+(li[i][1]-li[j][1])**2)**0.5
print(length/N)

Submission status: Accepted

Solution code:
import math
N = int(input())
li = [list(map(int, input().split())) for x in range(N)]
length = 0
for i in range(len(li)):
	for j in range(len(li)):
		if i != j:
			length += ((li[i][0]-li[j][0])**2+(li[i][1]-li[j][1])**2)**0.5
n = math.factorial(N-1)
ans = (length*n)/math.factorial(N)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = []
for i in range(n):
    A.append(list(map(int, input().split())))
s = 0
for i in range(n):
    for j in range(n):
        s += ((A[i][0]-A[j][0])**2 + (A[i][1]-A[j][1])**2)**0.5
print(s/n)

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]

def distance(a, b):
    x1, y1 = a[0], a[1]
    x2, y2 = b[0], b[1]
    dis = ((x1-x2)**2 + (y1-y2)**2)**0.5
    return dis


paths = list(permutations(a, len(a)))

distances = []
for p in paths:
    dis_path = []
    for i in range(len(a)-1):
        dis_path.append(distance(p[i], p[i+1]))
    distances.append(sum(dis_path))

print(sum(distances) / len(distances))

Submission status: Accepted

Solution code:

from math import sqrt 
N = int(input())
k = [list(map(int, input().split())) for _ in range(N)]
result = 0
for i in range(N):
    for j in range(N):
        if i != j:
            result += sqrt((k[i][0] - k[j][0]) * (k[i][0] - k[j][0]) + (k[i][1] - k[j][1]) * (k[i][1] - k[j][1]))
print(result / N)

Submission status: Accepted

Solution code:
def run():
  N = int(input())
  xy_li = [list(map(int, input().split())) for i in range(N)]
  d_sum = 0
  for i in range(N):
    for j in range(i+1, N):
      d_sum += ((xy_li[i][0]-xy_li[j][0])**2+(xy_li[i][1]-xy_li[j][1])**2)**(1/2)
      #print(i, j, d_sum)
  print(2*d_sum/N)
  
if __name__ == '__main__':
  run()

Submission status: Accepted

Solution code:
def main():
	import itertools
	n = int(input())
	l = [list(map(int,input().split())) for i in range(n)]
	l_jun = []
	dis_l = []
	ans_pre = []

	# print(l)

	for i in itertools.permutations(range(n),n):
		l_jun.append(i)
	# print(l_jun)

	for i in range(len(l_jun)):
		# print("組み合わせ番号 = {}".format(i))
		#順列の数だけ回す
		# print("--- i = {} ---".format(i))
		# print("l_jun[i] = {}".format(l_jun[i]))
		for t in range(n-1):
			# print("l_jun[i][t] = {}".format(l_jun[i][t]))
			# print("l_jun[i][t+1] = {}".format(l_jun[i][t+1]))
			# print("l[ l_jun[i][t] ] = {}".format(l[ l_jun[i][t] ]))
			# print("l[ l_jun[i][t+1] ] = {}".format(l[ l_jun[i][t+1] ]))
			dis_x = (( int(l[ l_jun[i][t] ][0])-int(l[ l_jun[i][t+1] ][0]) )**2)
			dis_y = (( int(l[ l_jun[i][t] ][1]) - int(l[ l_jun[i][t+1] ][1]) )**2)
			# print('dis_x = {}'.format(dis_x))
			# print("dis_y = {}".format(dis_y))
			dis = (dis_x+dis_y)**.5
			ans_pre.append(dis)
			# print(ans_pre)
			# x_dis = ( (int(l[ l_jun[i][t][0] ])- int(l[l_jun[i][t+1][0]]))**2 + (int(l[ l_jun[i][t][1] ]) - int(l[ l_jun[i][t+1][1] ]) )**2)**0.5
			# print("dis = {}".format(dis))
	print(sum(ans_pre)/len(l_jun))




if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
N=int(input())
town=[]
for i in range(N):
    x,y=map(int,input().split())
    town.append([x,y])
total=0
for i in range(N):
    for j in range(N):
        total+=((town[j][0]-town[i][0])**2+(town[j][1]-town[i][1])**2)**0.5
a=0
for i in range(N):
    a+=i
ave=total/a
print((ave*N-ave)/2)

Submission status: Accepted

Solution code:
from math import sqrt
n = int(raw_input())
coords = [map(int, raw_input().split()) for i in range(n)]
dist = 0
for i in range(n):
    for j in range(i+1, n):
        if i != j:
            dist += sqrt((coords[i][0]-coords[j][0])**2 + (coords[i][1] - coords[j][1])**2)

def factorial(n):
    res = n
    for i in range(1, n):
        res *= i
    return res
            
print dist * 2 / n

Submission status: Accepted

Solution code:
N = int(input())

coordinates = []
for i in range(N):
    x, y = map(int, input().split())
    coordinates.append((x, y))

import itertools
paths = list(itertools.permutations(range(N)))

total_length = 0
for i in paths:
    for j in range(N - 1):
        x1, y1 = coordinates[i[j]]
        x2, y2 = coordinates[i[j + 1]]
        total_length += ((x1 - x2)**2 + (y1 - y2)**2)**0.5

average_length = total_length / len(paths)
print(average_length)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
xy=np.zeros((n,2))
for i in range(n):
    xy[i][0],xy[i][1]=map(int,input().split())

ans=0
for i in range(n-1):
    for j in range(i+1,n):
        ans+=np.sqrt(np.square(xy[i][0]-xy[j][0])+np.square(xy[i][1]-xy[j][1]))
ans/=(n/2)
print(ans)

Submission status: Accepted

Solution code:
import itertools
import math

n = int(input())
cities = []
for _ in range(n):
    x, y = map(int, input().split())
    cities.append([x - 1, y - 1])

ave = 0
count = 0
for path in itertools.permutations(cities):
    length = 0
    for i in range(len(path))[1:]:
        length += math.sqrt((path[i][0] - path[i-1][0]) ** 2 +
                            (path[i][1] - path[i-1][1]) ** 2)
    ave += length
    count += 1

print(ave / count)

Submission status: Accepted

Solution code:
import itertools
import math

def rt(x1, x2, y1, y2):
    cal = (x2-x1)**2 + (y2-y1)**2
    return math.sqrt(cal)

n = int(input())
patern = list(itertools.permutations(range(1,n+1), n))

input_list = [[0, 0]]
for _ in range(1, n+1):
    x, y = map(int, input().split())
    input_list.append([x, y])

cal = 0
for i in range(len(patern)):
    for j in range(n-1):
        index = patern[i][j]
        index_ad = patern[i][j+1]
        x1 = input_list[index][0]
        x2 = input_list[index_ad][0]
        y1 = input_list[index][1]
        y2 = input_list[index_ad][1]

        cal += rt(x1, x2, y1, y2)

print(cal/len(patern))

Submission status: Accepted

Solution code:
from itertools import combinations
from itertools import permutations
import math

n = int(input())

loc = [None]*n
for i in range(0, n):
	x, y = [int(el) for el in input().split(' ')]
	loc[i] = [x, y]



# compute all distance
comb = combinations([i for i in range(0, n)], 2)

dist = dict()
for c in comb:
	start = c[0]
	goal = c[1]

	d = math.sqrt(pow(loc[start][0] - loc[goal][0], 2) \
		+ pow(loc[start][1] - loc[goal][1], 2))
	dist[c] = d

# all possible ways to go around the cities
pattern = permutations([i for i in range(0, n)])

total_d = 0
for pat in pattern:
	sub_total_d = 0
	for j in range(0, n-1):
		start = min(pat[j], pat[j+1])
		goal = max(pat[j], pat[j+1])

		sub_total_d += dist[(start, goal)]
	total_d += sub_total_d
print(total_d / math.factorial(n))

Submission status: Accepted

Solution code:
import itertools

def distance(a, b):
    return ( (a[0] - b[0])**2 + (a[1] - b[1])**2 )**0.5


N = int(input())
z = []

for i in range(0, N):
    z.append(list(map(int, input().split())))
    
d = [ [] for k in range(0, N) ]
for i in range(0, N):
    for j in range(0, N):
        d[i].append( distance(z[i], z[j]) )

seq = tuple( (k for k in range(0, N)) )
per = list(itertools.permutations(seq))
result = 0

for n in range(0, len(per)):
    summation = 0
    for l in range(0, N-1):
        summation = summation + d[per[n][l]][per[n][l+1]]
    result = result + summation

s = 1
for j in range(1, N+1):
    s = s * j

print(result/s)

Submission status: Accepted

Solution code:
import itertools
import math

n=int(input())
x=[list(map(int,input().split())) for i in range(n)]
y=list(itertools.permutations(x))


def dist(g):
    global n
    c=0
    for i in range(n-1):
        c+=math.sqrt((g[i+1][0]-g[i][0])**2+(g[i+1][1]-g[i][1])**2)
    return c

m=0
k=0
for i in y:
    k+=1
    m+=dist(i)
print(m/k)

Submission status: Accepted

Solution code:
import itertools
from math import sqrt, pow

N = int(input())
lst = []
for _ in range(N):
    lst.append([int(x) for x in input().split()])

ret_lst = []
for l in itertools.permutations(lst):
    ret = 0
    for idx, (x1, y1) in enumerate(l[:-1]):
        x2 = l[idx + 1][0]
        y2 = l[idx + 1][1]
        ret += sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2))
    ret_lst.append(ret)

print(sum(ret_lst) / len(ret_lst))

Submission status: Accepted

Solution code:
n = int(input())
xy = []
for i in range(n):
    x, y = map(int, input().split())
    xy.append([x, y])

res = 0
for i in range(n):
    for j in range(i+1, n):
        dist = ((xy[i][0] - xy[j][0]) ** 2 + (xy[i][1] - xy[j][1]) ** 2) ** (1/2)
        res += dist

print(res*2/n)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from itertools import permutations
from math import sqrt
n = int(input())
xy = [list(map(int, input().split())) for _ in range(n)]

def d(p1,p2):
    return sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)
    

res = 0.0
cnt = 0
for p in permutations(range(n), n):
    cnt += 1
    for i in range(n-1):
        res += d(xy[p[i]],xy[p[i+1]])

print(res/cnt)

Submission status: Accepted

Solution code:
from itertools import permutations
from math import sqrt, factorial

N = int(input())
P = [list(map(int, input().split())) for _ in range(N)]
ans = 0
for p in permutations(range(N),N):
    for i in range(N-1):
        x1, y1 = P[p[i]]
        x2, y2 = P[p[i+1]] 
        ans += sqrt((x1-x2)**2+(y1-y2)**2)
ans /= factorial(N)
print(ans)

Submission status: Accepted

Solution code:
import itertools
import math
L=0
M=0
N=int(input())
p=[list(map(int,input().split())) for i in range(N)]
for seq in itertools.permutations(range(N),N):
    tmp=0
    for i in range(N-1):
        x1,y1=p[seq[i]]
        x2,y2=p[seq[i+1]]
        tmp+=math.sqrt((x1-x2)**2+(y1-y2)**2)
    L+=tmp
    M+=1
print(L/M)

Submission status: Accepted

Solution code:
N=int(input())
points=[input().split() for i in range(N)]
import math
def calc_dist(start, end):
    return math.sqrt((int(start[0]) - int(end[0])) ** 2 + (int(start[1]) - int(end[1])) ** 2)

from itertools import combinations, permutations
total_dist = []
for root in permutations(range(N), N):
    dist = 0
    for num in range(len(root)-1):
        dist += calc_dist(points[root[num]], points[root[num+1]])
    total_dist.append(dist)
from statistics import mean
print(mean(total_dist))

Submission status: Accepted

Solution code:
N = int(input())
town_list = [list(map(int, input().split())) for i in range(N)]
import itertools
import math

path = []
for p in itertools.permutations(town_list):
    lengh = 0
    for i in range(N - 1):
        lengh += math.sqrt((p[i][0] - p[i + 1][0]) ** 2 + (p[i][1] - p[i + 1][1]) ** 2)
    path.append(lengh)

print(sum(path) / len(path))