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 (Python):
import math

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

dist = 0
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        dist += math.sqrt((town[i][0] - town[j][0]) **2 + (town[i][1] - town[j][1]) **2)
ans = dist / n
print(ans)

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

dist=[[0]*N for _ in range(N)]
ans=0
for i in range(N):
  for j in range(N):
    if i==j:
      continue
    ans+=((AB[i][0]-AB[j][0])**2+(AB[i][1]-AB[j][1])**2)**0.5
ans/=N
print(ans)

Solution (Python):
N=int(input())
X=[list(map(int,input().split())) for i in range(N)]

from math import factorial as fac

S=0
for i in range(N):
    for j in range(N):
        S+=((X[i][0]-X[j][0])**2+(X[i][1]-X[j][1])**2)**(1/2)

total=fac(N)//fac(2)//fac(N-2)
print(((N-1)/(total))*S/2)

Solution (Python):
import math
from itertools import permutations

N = int(input())
points = [tuple(map(int, input().split(' '))) for _ in range(N)]

s = 0
count = 0
for routes in permutations(points):
    count += 1
    for p1, p2 in zip(routes, routes[1:]):
        x1, y1 = p1
        x2, y2 = p2
        s += math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)

print(s / count)

Solution (Python):
from math import sqrt
n = int(input())
p = []

for i in range(n):
    x,y = map(int, input().split())
    p.append((x,y))
ans=0
for i in range(n-1):
    for j in range(i+1,n):
        x1,y1=p[i]
        x2,y2=p[j]
        d=sqrt((x1-x2)**2+(y1-y2)**2)
        ans+=d
ans/=n/2
print(ans)

Solution (Python):
from itertools import permutations
import math
N = int(input())
loc = [tuple(map(int, input().split())) for _ in range(N)]


def calc_dist(x1, y1, x2, y2):
    return ((x1 - x2)**2 + (y1 - y2)**2) ** 0.5


City = []
for i in range(N):
    City.append(i)
ans = 0
for root in permutations(City):
    st = -1
    for i, r in enumerate(root):
        if i == 0:
            stx, sty = loc[r]
        else:
            nx, ny = loc[r]
            ans += calc_dist(stx, sty, nx, ny)
            stx = nx
            sty = ny
print(ans/math.factorial(N))

Solution (Python):
import sys
input = sys.stdin.readline
import math
import itertools

def kyori(s,g):
    return math.sqrt( (s[0] - g[0])**2 + (s[1] - g[1])**2)

def average_length(pos,n):
    amount = 0
    count = 0
    for i in itertools.permutations(pos,n):
        for j in range(len(i)-1):
            amount += kyori(i[j],i[j+1])
        count += 1
    return amount/count

if __name__ == "__main__":
    n = int(input())
    pos = []

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

    print(average_length(pos,n))

Solution (Python):
import itertools
import math

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

dist_list = []
for one_case in permutations_lis:
  dist = 0
  
  for i, cur in enumerate(one_case):
    if i == 0:
      prev_city = cities[cur]
    else:
      cur_city = cities[cur]
      
      dist += math.sqrt((cur_city[0] - prev_city[0])**2 + (cur_city[1] - prev_city[1])**2)
      
      prev_city = cur_city
  dist_list.append(dist)
    
print(sum(dist_list) / len(dist_list))

Solution (Python):
from itertools import permutations
import math


N = int(input())
town = [tuple(map(int, input().split())) for _ in range(N)]
total_dist = 0

for perm in permutations(town):
    for i in range(N-1):
        total_dist += math.sqrt(
            (perm[i][0] - perm[i+1][0])**2 + 
            (perm[i][1] - perm[i+1][1])**2)


print(total_dist / math.factorial(N))

Solution (Python):
from itertools import permutations as per
from math import sqrt, factorial

n = int(input())
A = [list(map(int, input().split())) for _ in range(n)]
p = [i for i in range(n)]
ans = 0
for pp in per(p, n):
    r = 0
    for j in range(n-1):
        r += sqrt((A[pp[j]][0] - A[pp[j+1]][0])**2 + (A[pp[j]][1] - A[pp[j+1]][1])**2)
    ans += r
print(ans/factorial(n))

Solution (Python):
import math

ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n = ni()

x = []
y = []

for i in range(n):
  a,b = nm()
  x.append(a)
  y.append(b)

cnt = 0

def dist(xi,yi,xj,yj):
  return math.sqrt((xi-xj)**2+(yi-yj)**2)

def search(route):
  global cnt
  s = 0
  if len(route) == n:
    cnt += 1
    for i in range(n-1):
      s += dist(x[route[i]],y[route[i]],x[route[i+1]],y[route[i+1]])
    return s
  for i in range(n):
    if i not in route:
      s+=search(route+[i])
  return s

ans = search([])/cnt
print(ans)

Solution (Python):
import math
N = int(input())
city = [[] for _ in range(N)]
for i in range(N):
    a,b = map(int,input().split())
    city[i] = [a,b]
rout = [[0]*N for _ in range(N)]
total = 0
for i in range(N):
    for j in range(N):
        if city[i][0] == city[j][0] and city[i][1]==city[j][1]:
            continue
        else :
            sq = math.pow(city[i][0]-city[j][0],2) + math.pow(city[i][1]-city[j][1],2)
            total  += math.sqrt(sq)
print(total/N)

Solution (Python):
from itertools import permutations
from math import sqrt
n = int(input())
xy = [list(map(int, input().split())) for i in range(n)]
l = []
for i in permutations(xy):
    m = 0
    for j in range(n - 1):
        m += sqrt((i[j + 1][0] - i[j][0]) ** 2 + (i[j + 1][1] - i[j][1]) ** 2)
    l.append(m)
print(sum(l) / len(l))

Solution (Python):
import math
import itertools


N = int(input())

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

permutation_list = itertools.permutations([x for x in range(N)])

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

result = 0
count = 0
for data in permutation_list:
    count += 1
    for i in range(1, len(data)):
        result += calc[data[i - 1]][data[i]]
print(result / count)

Solution (Python):
from itertools import permutations

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

M = list(permutations(list(range(N))))
ans = 0
for m in M:
    for i in range(N-1):
        a, b = m[i], m[i+1]
        x_a, y_a = XY[a]
        x_b, y_b = XY[b]
        d = ((x_a - x_b) ** 2 + (y_a - y_b) ** 2) ** 0.5
        ans += d

print(ans / len(M))

Solution (Python):
import math
import itertools
N = int(input())
A = []
for i in range(N):
  a = list(map(int,input().split()))
  A.append(a)
K = 0
dp = [[0]*(N+1) for _ in range(N+1)]
for i in range(N-1):
  for j in range(i+1,N):
    xi = A[i][0]
    yi = A[i][1]
    xj = A[j][0]
    yj = A[j][1]
    t = ((xi-xj)**2+(yi-yj)**2)**(1/2)
    dp[i+1][j+1] = t 
    dp[j+1][i+1] = t
l= []
for i in range(1,N+1):
  l.append(i)
for v in itertools.permutations(l):
  for j in range(N-1):
    K += dp[v[j]][v[j+1]]
print(K/(math.factorial(N)))

Solution (Python):
from itertools import permutations
def cal(a, b):
    x, y = a
    x1, y1 = b
    dis = ((x - x1) ** 2 + (y - y1) ** 2)**0.5
    return dis

n = int(input())
xy = [list(map(int,input().split())) for i in range(n)]
cnt = 0
dis = 0
for l in permutations(range(n), n):
    cnt += 1
    for i in range(n - 1):
        dis += cal(xy[l[i]], xy[l[i + 1]])
print(dis / cnt)

Solution (Python):
import math
from itertools import permutations

N = int(input())
positions = [tuple(map(int, input().split(' '))) for _ in range(N)]


def distance(p1, p2):
    x1, y1 = p1
    x2, y2 = p2
    return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)


s_distance = 0
n_routes = 0
for route in permutations(positions):
    s_distance += sum([distance(p1, p2) for p1, p2 in zip(route, route[1:])])
    n_routes += 1

print(s_distance / n_routes)

Solution (Python):
import itertools
from statistics import mean, median,variance,stdev

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

XY_perm = list(itertools.permutations(XY))

L = []
set_length = len(XY_perm[0])

for i in range(len(XY_perm)):
    L_set =[]
    for j in range(set_length-1):
        st_x = XY_perm[i][j][0]
        st_y = XY_perm[i][j][1]

        ed_x = XY_perm[i][j+1][0]
        ed_y = XY_perm[i][j+1][1]

        length = ((ed_x-st_x)**2 + (ed_y-st_y)**2)**0.5
        L_set.append(length)
    L.append(sum(L_set))

print(mean(L))

Solution (Python):
import math
import itertools
def dist (s,t):
  return ((s[0]-t[0])**2+(s[1]-t[1])**2)**0.5

n = int(input())
l = []
for _ in range(n):
  l.append(tuple(map(int,input().split())))

candidates = []
for l_i in itertools.permutations(l):
  lgh = 0
  for i in range(n-1):
    lgh += dist(l_i[i],l_i[i+1])
  candidates.append(lgh)
print(sum(candidates)/len(candidates))

Solution (Python):
import sys
from itertools import combinations
n = int(sys.stdin.readline())
lst = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]


def dist(a, b):
  x = a[0] - b[0]
  y = a[1] - b[1]
  dist = (x ** 2 + y ** 2) ** 0.5
  return dist

dist_lst = [dist(i[0], i[1]) for i in combinations(lst, 2)]
print(2 * sum(dist_lst) / n)

Solution (Python):
import itertools

N = int(input())

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


junretsu = list(itertools.permutations(xy))
kyori_list = []
for temp in junretsu:
    total_kyori = 0
    for i in range(0,N-1):
        temp1 = temp[i+1][0] - temp[i][0]
        temp2 = temp[i+1][1] - temp[i][1]
        kyori = (temp1**2 + temp2**2) **0.5
        total_kyori += kyori
    
    kyori_list.append(total_kyori)


print(sum(kyori_list)/len(kyori_list))

Solution (Python):
import itertools
import numpy as np
import math

N = int(input())
X = [[] for _ in range(N)]
for i in range(N):
    X[i] = list(map(int, input().split()))
permutations_list = list(itertools.permutations([i for i in range(N)]))

ans = 0
for i in range(len(permutations_list)):
    x = np.argsort(permutations_list[i])
    for j in range(N-1):
        x_i = X[x[j]][0]
        y_i = X[x[j]][1]
        x_j = X[x[j+1]][0]
        y_j = X[x[j+1]][1]
        ans += math.sqrt(pow(x_i - x_j, 2) + pow(y_i - y_j, 2))
print(ans/len(permutations_list))

Solution (Python):
import itertools
import math
N = int(input())
X = []
Y = []
for i in range(N):
  x, y = map(float,input().split())
  X.append(x)
  Y.append(y)

D = list(itertools.permutations(range(N)))
d = 0

for i in D:  
  for j in range(len(i) - 1):
    d += ((X[i[j + 1]] - X[i[j]]) ** 2 + (Y[i[j + 1]] - Y[i[j]]) ** 2) ** (1/2)
    
print(d / math.factorial(N))

Solution (Python):
n = int(input())
xy = list()
for i in range(n):
    xy.append(list(map(int,input().split())))
 
dsum = 0
dist_l = [[0] * n for k in range(n)]
for i in range(n-1):
    for j in range(i+1, n):
        dist = ((xy[i][0] - xy[j][0]) ** 2 + (xy[i][1] - xy[j][1]) ** 2) ** (1/2)
        dist_l[i][j] = dist
        dist_l[j][i] = dist
        dsum = dsum + dist
 
ans = dsum * (n-1) / (n * (n-1) / 2)
 
print(ans)

Solution (Python):




n = int(input())
xy = list()
for i in range(n):
    xy.append(list(map(int,input().split())))

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

ans = dsum * (n-1) / (n * (n-1) / 2)


print(ans)

Solution (Python):
import itertools
import math
n=int(input())
xy=[tuple(map(int,input().split())) for _ in range(n)]
path_length=0
for p in itertools.permutations(xy):
    for (x1,y1),(x2,y2) in zip(p,p[1:]):
        path_length+=math.sqrt((x1-x2)**2+(y1-y2)**2)
print(path_length/len(list((itertools.permutations(xy)))))

Solution (Python):
import itertools
import math

N = int(input())

iter = []
for i in range(N):
    iter.append(i)
    
city = list(itertools.permutations(iter))

x=[0]*N
y =[0]*N

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

for i in (city):
    road = 0
    
    for j in range(N-1):
        road += math.sqrt((x[i[j]] -x[i[j+1]])**2 + (y[i[j]]-y[i[j+1]])**2)

    
    sum += road
    
    
print(sum/len(city))

Solution (Python):
n = int(input())
xy = [list(map(int, input().split())) for _ in range(n)]

def distance(i1, i2):
    p1 = xy[i1]
    p2 = xy[i2]
    return ((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) ** 0.5

dp = [None] * (1 << n)
def dfs(state=0):
    if dp[state] is not None:
        return dp[state]

    ans = []
    s = 0
    for i in range(n):
        target = 1 << i
        if not state & target:
            res = dfs(state | target)
            ans.append(i)
            for v in res[0]:
                s += distance(i, v)
    dp[state] = ans, s
    return dp[state]

print(dfs()[1] / n)

Solution (Python):
import itertools
import math

n = int(input())
town = [[int(i)  for i in input().split()] for j in range(n)]

lis =[i for i in range(n)]
permutations_lis = itertools.permutations(lis)

sum = 0

for one_case in permutations_lis:
    for i in range(n-1):
        sum += math.sqrt((town[one_case[i+1]][0] - town[one_case[i]][0]) ** 2 + (town[one_case[i+1]][1] - town[one_case[i]][1]) ** 2)

print(sum / math.factorial(n))

Solution (Python):
import itertools
import math
N = int(input())
NL = [i for i in range(N)]
xy = [list(map(int,input().split())) for _ in range(N)]
length = [[0 for _ in range(N)] for i in range(N)]
C = list(itertools.combinations(NL,2))
for c in C:
    length[c[0]][c[1]] = math.sqrt((xy[c[0]][0]-xy[c[1]][0])**2+(xy[c[0]][1]-xy[c[1]][1])**2)
    length[c[1]][c[0]] = length[c[0]][c[1]]
P = list(itertools.permutations(NL,N))
lenglist = []
for p in P:
    leng = 0
    for i in range(len(p)-1):
        leng += length[p[i]][p[i+1]]
    lenglist.append(leng)
print(sum(lenglist)/len(P))

Solution (Python):
n = int(input())

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

from itertools import permutations

paths = [i for i in range(n)]
path_perms = list(permutations(paths))

d_list = []
for path in path_perms:
    d = 0
    for i in range(len(path)-1):
        p1 = path[i]
        p2 = path[i+1]
        tmp = ((xy[p1][0]-xy[p2][0])**2+(xy[p1][1]-xy[p2][1])**2)**(1/2)
        d += tmp
    d_list.append(d)

print(sum(d_list)/len(d_list))

Solution (Python):
import itertools
import math
import sys


def input():
    return sys.stdin.readline()[:-1]


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

sum_distance = 0
for x, y in list(itertools.combinations(range(N), 2)):
    sum_distance += math.sqrt(abs(XY[x][0]-XY[y][0])**2+abs(XY[x][1]-XY[y][1])**2)
print(sum_distance/(N/2))

Solution (Python):
n=int(input())
xy=[[0]*2 for ii in range(n)]
d=[[0]*n for ii in range(n)]
for ii in range(n):
  xy[ii][:]=map(int,input().split())
r=0
c=0
for ii in range(n):
  for jj in range(n):
    if ii!=jj:
      r+=((xy[ii][0]-xy[jj][0])**2+(xy[ii][1]-xy[jj][1])**2)**0.5
      c+=1
print((r/c)*(n-1))

Solution (Python):
import numpy as np
from math import factorial

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

def dist(i, j):
    return np.sqrt((x[i]-x[j])**2 + (y[i] - y[j])**2)

matrix = np.zeros((N, N))
for i in range(N*N):
    iy = i%N
    ix = i//N
    if iy > ix:
        matrix[ix, iy] = dist(ix, iy)

avepath = np.sum(matrix)*2/N
print(avepath)

Solution (Python):
import itertools
n=int(input())
dic={}
l=[]
for i in range(n):
  x,y=[int(x) for x in input().rstrip().split()]
  if i!=0:
    for j in range(len(l)):
      s=l[j][0]
      t=l[j][1]

      dist=((s-x)**2+(y-t)**2)**(0.5)
      dic[(i,j)]=dist
      dic[(j,i)]=dist
  l.append([x,y])

ans=0
cnt=0
nl=[i for i in range(n)]
for i in itertools.permutations(nl,n):
  cnt+=1
  for j in range(len(i)-1):
    ans+=dic[(i[j],i[j+1])]
print(ans/cnt)

Solution (Python):
import itertools, math
stdin= open(0).read().split('\n')
N = int(stdin[0])
x, y = [0]*N, [0]*N
for i in range(1,N+1):
    x[i-1], y[i-1] = map(int,stdin[i].split())
num = 0
sum = 0
for p in itertools.permutations(range(N), N):
    num += 1
    for j in range(N-1):
        sum += math.sqrt((x[p[j]]-x[p[j+1]])**2 + (y[p[j]]-y[p[j+1]])**2)
print(sum/num)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    def kyori(x1,x2,y1,y2):
            return ((x1-y1)**2+(x2-y2)**2)**0.5
    n=int(input())
    l=[list(map(int,input().split())) for i in range(n)]
    import itertools
    ans=0
    cnt=0
    p=itertools.permutations(l)
    for x in p:
        z = list(x)
        cnt+=1
        for y in range(n-1):
            ans+=kyori(z[y][0],z[y][1],z[y+1][0],z[y+1][1])
    print(ans/cnt)
resolve()

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=[list(map(int,input().split())) for i in range(n)]
    import itertools
    ans=0
    p=list(itertools.permutations(l))
    for x in p:
        z = list(x)
        for y in range(n-1):
            ans+=((z[y][0]-z[y+1][0])**2+(z[y][1]-z[y+1][1])**2)**0.5
    print(ans/len(list(p)))
resolve()

Solution (Python):
import itertools
import math
N = int(input())
AB = [[0] * 2  for i in range(N)]
P = list()
ans = 0
for i in range(1,N+1):
    P.append(i)
for j in range(N):
    A,B = map( int ,input().split())
    AB[j][0] = A
    AB[j][1] = B

for list in itertools.permutations(P, N):
    for l in range(len(list)-1):
            dic=math.sqrt((AB[list[l+1]-1][0]-AB[list[l]-1][0])**2+(AB[list[l+1]-1][1]-AB[list[l]-1][1])**2)
            ans += dic/math.factorial(N)
print(ans)

Solution (Python):
N = int(input())
XY = list([list(map(int, input().split())) for _ in range(N)])
sum = 0
for i in range(N-1):
    for j in range(i+1, N):
        sum += ((XY[j][0]-XY[i][0])**2 + (XY[j][1]-XY[i][1])**2) ** 0.5
ans = sum * 2 / N
print(ans)

Solution (Python):
import math as m
import itertools


def calc(x1, y1, x2, y2):
    return m.sqrt((x1 - x2)**2 + (y1 - y2)**2)


def resolve():
    N = int(input())
    xx, yy = [], []
    for i in range(N):
        x, y = [int(i) for i in input().split()]
        xx.append(x)
        yy.append(y)
    n = [i for i in range(N)]
    perm = itertools.permutations(n)
    sumD = 0.0
    count = 0
    for p in perm:
        count += 1
        for i in range(N - 1):
            sumD += calc(xx[p[i]], yy[p[i]], xx[p[i + 1]], yy[p[i + 1]])
    print(sumD / count)


resolve()

Solution (Python):
import sys
import itertools


input = sys.stdin.readline


def fact(n):
    res = 1
    for i in range(n):
        res *= n - i
    return res


def main():
    dst_sum = 0
    for p in itertools.permutations(T):
        for i in range(1, N):
            px, py = p[i-1]
            cx, cy = p[i]
            dst_sum += ((cx-px)**2 + (cy-py)**2)**0.5
    print(dst_sum/fact(N))


if __name__ == "__main__":
    N = int(input())
    T = [tuple(map(int, input().split())) for _ in range(N)]
    main()

Solution (Python):
n=int(input())
xy=[list(map(int,input().split())) for _ in range(n)]
goukei=0
for i in range(n):
  for j in range(n):
    if i>j:
      goukei+=((xy[i][0]-xy[j][0])**2+(xy[i][1]-xy[j][1])**2)**0.5
average=goukei/(n*(n-1)/2)
print(average*(n-1))

Solution (Python):
from itertools import permutations
import math
n=int(input())
xy=[list((map(int,input().split()))) for j in range(n)]

sum=0
for i in permutations(xy,2):
  sum+=math.sqrt((abs(i[0][0] - i[1][0]))**2 + (abs(i[0][1] - i[1][1]))**2 )
print(sum/n)

Solution (Python):
import itertools
import math

N =int(input())
X = []
Y = []
for i in range(N):
  x, y = map(int, input().split())
  X += [x]
  Y += [y]

ans = []
for v in itertools.permutations(range(N)):
  curr = 0
  for i in range(N-1):
    curr += ((X[v[i]] - X[v[i+1]])**2 + (Y[v[i]] - Y[v[i+1]])**2 )**.5
  ans += [curr]

print(sum(ans)/math.factorial(N))

Solution (Python):
from itertools import permutations
import math
n = int(input())
xy = [list(map(int,input().split())) for _ in range(n)]

sum = 0
for i in permutations(xy,2):
    sum = sum + math.sqrt( (abs(i[0][0] - i[1][0]))**2 + (abs(i[0][1] - i[1][1]))**2 )
print(sum/n)

Solution (Python):
import numpy as np
import itertools as it

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

dist = [[[] for i in range(n)] for i in range(n)]
for i in range(n):
    for j in range(n):
        v = city[i] - city[j]
        dist[i][j] = np.linalg.norm(v, ord=2)

ps = it.permutations([i for i in range(n)])
anss = []
for p in ps:
    ans = 0
    for i in range(n-1):
        ans += dist[p[i]][p[i+1]]
    anss.append(ans)

print(np.average(anss))

Solution (Python):
N = int(input())
l = [list(map(int,input().split())) for _ in range(N)]
import itertools
p = list(itertools.permutations(range(N),N))

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

Solution (Python):
import itertools
import math
N = int(input())
P = N
XY = []
for n in range(N):
    xy = list(map(int, input().split()))
    XY.append(xy)
l = [i for i in range(N)]
L = itertools.permutations(l)
total = 0
cnt = 0
for i in L:
    p = list(i)
    for n in range(N-1):
        res = 0
        res = math.sqrt((XY[p[n+1]][0] - XY[p[n]][0])**2 + (XY[p[n+1]][1] - XY[p[n]][1])**2)
        cnt += res
G = cnt / math.factorial(P)
print(G)

Solution (Python):
N = int(input())
L = []

for i in range(N):
    L.append([int(i) for i in input().split()])
from itertools import permutations
def get_len(L1,L2):
    return ((L1[0]-L2[0])**2 +(L1[1]-L2[1])**2 )**0.5
ans =0
for i in list(permutations(L)):
    for l in range(len(i)-1):
        ans +=get_len(i[l],i[l+1])
print(ans/len(list(permutations(L))))

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

points = [tuple(map(int, input().split())) for _ in range(N)]

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

math.factorial(N)

d = 0
for p in permutations(range(N)):
    for p0, p1 in zip(p[:-1], p[1:]):
        d += dist(points[p0], points[p1])
print(d / math.factorial(N))

Solution (Python):
import math
import itertools
N = int(input())
citys = [[0,0]]
for i in range(0,N,1):
    citys.append(list(map(int,input().split())))
dist = [[0 for i in range(0,N+1,1)]for j in range(0,N+1,1)]
for i in range(1,N+1,1):
    for j in range(1,N+1,1):
        dist[i][j]=math.sqrt((citys[i][0]-citys[j][0])**2 + (citys[i][1]-citys[j][1])**2)

total = 0
for i in list(itertools.permutations(range(1,N+1,1),N)):
    for j in range(0,N-1,1):
        total += dist[i[j]][i[j+1]]

print(total/math.factorial(N))

Solution (Python):
import itertools
import math

N = int(input())
arr = [None] * N
for i in range(N):
  x, y = map(int, input().split())
  arr[i] = (x,y)

results = []
for v in itertools.permutations([i for i in range(N)], N):
  way = 0
  for i in range(N-1):
    way += ((arr[v[i+1]][0]-arr[v[i]][0])**2 + (arr[v[i+1]][1]-arr[v[i]][1])**2)**0.5
  results.append(way)
all = math.factorial(N)
print(sum(results)/all)

Solution (Python):
N = int(input())

XY = []

for i in range(N):
    Xi, Yi = map (float, input().split())
    XY.append([Xi,Yi])

Z  = 0
for i in range(N-1):
    for j in range(i + 1, N):
        Z += ((XY[i][0] - XY[j][0])**2 + (XY[i][1] - XY[j][1])**2 ) ** 0.5

print(Z * 2 / N)

Solution (Python):
from itertools import permutations
import math

n = int(input())

def readlines():
    for _ in range(n):
        x, y = map(int, input().split())
        yield x, y

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

def lengths():
    for path in permutations(readlines()):
        length = 0
        for i in range(n-1):
            length += distance(path[i], path[i+1])
        yield length

print(sum(lengths()) / math.factorial(n))

Solution (Python):
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

n=n0()
xy=n3(n)

import itertools,math
ite=itertools.permutations(range(n))

ans=c=0
for line in ite:
    d=0
    for i in range(1,n):
        ax,ay=xy[line[i]]
        bx,by=xy[line[i-1]]
        d+=math.sqrt((ax-bx)**2+(ay-by)**2)
    ans+=d
    c+=1
print(ans/c)

Solution (Python):
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

n=n0()
xy=n3(n)

import itertools,math
ite=itertools.combinations(range(n),2)
d=0
for i,j in ite:
    ax,ay=xy[i]
    bx,by=xy[j]
    d+=math.sqrt((ax-bx)**2+(ay-by)**2)
    
print(2*d/n)

Solution (Python):


import itertools
import math

n = int(input())

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


sum_dis = 0
for i, j in itertools.combinations(range(n), 2):
    x1, y1 = ab[i]
    x2, y2 = ab[j]
    sum_dis += ((x1-x2)**2+(y1-y2)**2)**0.5

sum_dis /= n*(n-1)/2

print(sum_dis*(n-1))

Solution (Python):


import itertools
import math

n = int(input())

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


sum_dis = 0
for i, j in itertools.permutations(range(n), 2):
    x1, y1 = ab[i]
    x2, y2 = ab[j]
    sum_dis += ((x1-x2)**2+(y1-y2)**2)**0.5

sum_dis /= n*(n-1)/2

print(sum_dis*(n-1)/2)

Solution (Python):
import itertools
import math

n=int(input())
town=[0]*n
for i in range(n):
    town[i]=tuple(map(int,input().split(' ')))

sum_len=0
now=[0,0]

permu = itertools.permutations([i for i in range(n)])
for case in permu:
    now = town[case[0]]
    for i in case[1:]:
        sum_len += ((now[0]-town[i][0])**2 + (now[1]-town[i][1])**2)**(1/2)
        now = town[i]
print(sum_len/math.factorial(n))

Solution (Python):
def distance(L1, L2):
    return ((L1[0] - L2[0]) ** 2 + (L1[1] - L2[1]) ** 2) ** 0.5

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

dis_sum = 0
for i in range(N):
    for j in range(i+1,N):
        dis_sum += distance(mat[i], mat[j])
dis_sum = dis_sum / (N * (N - 1) / 2) * (N - 1)
print(dis_sum)

Solution (Python):
n = int(input())
x = [0]*n
y = [0]*n
for i in range(n):
    x[i],y[i] = map(int,input().split())
import itertools
data = list(range(n))
r = n
cc = list(itertools.permutations(data, r))
dis = [0]*len(cc)
j = 0
for L in cc:
    for i in range(len(L)-1):
        dis[j] += ((x[L[i]]-x[L[i+1]])**2 + (y[L[i]]-y[L[i+1]])**2)**0.5
    j += 1
print(sum(dis)/len(cc))

Solution (Python):
import itertools
import math 
n = int(input())


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

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

sum = 0
for i in root:
  temp = 0
  for j in range(len(i)-1):
    temp = temp + math.sqrt(((loc[i[j+1]-1][0]-loc[i[j]-1][0])**2 + 
                             (loc[i[j+1]-1][1]-loc[i[j]-1][1])**2))
    
  sum += temp

print(sum/len(root))

Solution (Python):
n = int(input())
L = [list(map(int, input().split())) for _ in range(n)]
d = 0
for i in range(n):
    l = [0] * n
    for j in range(n):
        l[j] = ((L[i][0] - L[j][0]) ** 2 + (L[i][1] - L[j][1]) ** 2) ** 0.5
    d += sum(l)
print(d / n)

Solution (Python):
from itertools import permutations

n=int(input())

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

total=0
for i in list(permutations(l)):
  for j in range(len(i)-1):
    total +=((i[j][0]-i[j+1][0])**2+(i[j][1]-i[j+1][1])**2)**0.5
    
print(total/len(list(permutations(l))))

Solution (Python):
from itertools import permutations, combinations
import math

def factorial(num):
    if num <= 1:
        return 1
    return num * factorial(num - 1)

n = int(input())
xy =[list(map(int, input().split())) for _ in range(n)]
ans = 0
for i, j in combinations(range(n), 2):
    l = math.sqrt((xy[i][0] -xy[j][0]) ** 2 + (xy[i][1] -xy[j][1]) ** 2)
    ans += 2 * factorial(n - 2) * (n - 1) * l
print(ans / factorial(n))

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




dst = []
for i in range(N-1):
    for j in range(i+1,N):
        
        dst.append( (((town[i][0]-town[j][0])**2) + ((town[i][1]-town[j][1])**2) )**0.5 )

print(((sum(dst))*(N-1))/len(dst))

Solution (Python):
import itertools
import math
import fractions
import functools
n = int(input())
x = []
y = []
l = []
for i in range(n):
    a, b = map(int, input().split())
    x.append(a)
    y.append(b)
    l.append(i)

distance = 0

p_list = list(itertools.permutations(l, n))

for i in range(len(p_list)):
    for j in range(len(p_list[i])-1):
        distance += math.sqrt((x[p_list[i][j]] - x[p_list[i][j+1]])**2+(y[p_list[i][j]] - y[p_list[i][j+1]])**2)

print(distance/len(p_list))

Solution (Python):
import math
N = int(input())
xy = [list(map(int, input().split())) for i in range(N)]
k = 0
total = 0
for j in range(N-1):
    for i in range(j + 1, N):
        total += ((xy[j][0] - xy[i][0])**2 + (xy[j][1]-xy[i][1])**2)**(1/2)
    k += 1
total = total * 2
print(total/N)

Solution (Python):
import itertools

def calc(p1, p2):
  tmp = (p1[0] - p2[0])**2 + (p1[1] - p2[1])**2
  return tmp ** 0.5

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

dists = [[0 for _ in range(n)] for _ in range(n)]
for row in range(n):
  for col in range(n):
    dists[row][col] = calc(p[row], p[col])

count = 0
dist_sum = 0
for v in itertools.permutations(range(n), n):
  for i in range(len(v)-1):
    dist_sum += dists[v[i]][v[i+1]]
  count += 1

print(dist_sum/count)