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):
from itertools import permutations
from math import sqrt

N = int(input())

town = []

for i in range(N):
    town.append(tuple([int(x) for x in input().split()]))

route_list = list(permutations(range(N)))

result = 0
for route in route_list:
    for i in range(1, N):
        x = (town[route[i-1]][0] - town[route[i]][0])**2
        y = (town[route[i-1]][1] - town[route[i]][1])**2

        result += sqrt(x + y)

print(result / len(route_list))

Solution (Python):
N=int(input())
L=[]
for i in range(N):
    x,y=list(map(int,input().split()))
    L.append([x,y])
D=[]
for i in range(N-1):
    for j in range(i+1,N):
        d=((L[i][0]-L[j][0])**2+(L[i][1]-L[j][1])**2)**0.5
        D.append(d)
Ave=sum(D)/len(D)
print(Ave*(N-1))

Solution (Python):
from itertools import permutations

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

nc=n
sm=1
while nc>=1:
    sm*=nc
    nc-=1

dis=0

for x in permutations(range(n)):
    for i in range(n-1):
        dis+=((lst[x[i+1]][0]-lst[x[i]][0])**2+(lst[x[i+1]][1]-lst[x[i]][1])**2)**0.5

print(dis/sm)

Solution (Python):
from itertools import permutations
N = int(input())
pos = [[int(i) for i in input().split()] for j in range(N)]
total = 0
l = 0
for p in permutations([i for i in range(N)], N):
    l += 1
    for j in range(len(p)-1):
        x1,y1 = pos[p[j]]
        x2,y2 = pos[p[j+1]]
        total += ((x1 - x2)**2 + (y1 - y2)**2)**0.5
print(total / l)

Solution (Python):
import math
import itertools

def l2(z1, z2):
  return math.sqrt(pow(z1[0] - z2[0], 2) + pow(z1[1] - z2[1], 2))

n = int(input())
l = [list(map(int, input().split(' '))) for _ in range(n)]
seq_list = itertools.permutations(l, n)

dist = 0
for seq in seq_list:
  z_fr = []
  z_to = []
  
  for z in seq:
    z_fr = z_to
    z_to = z
    if len(z_fr)==0:
      continue
    dist += l2(z_fr, z_to)

print(dist/math.factorial(n))

Solution (Python):
from itertools import permutations

N = int(input())
xlist = []
ylist = []

n = 1
for i in range(1, N+1):
    x, y = map(int, input().split())
    xlist.append(x)
    ylist.append(y)
    n *= i

ans = 0
for perm in permutations(range(N)):
    for i in range(N-1):
        x1, y1 = xlist[perm[i]], ylist[perm[i]]
        x2, y2 = xlist[perm[i+1]], ylist[perm[i+1]]
        ans += ((x1 - x2)**2 + (y1 - y2)**2)**0.5 / n

print(ans)

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

ls2 = itertools.permutations(range(N))

dist = 0
for p in ls2:
    for i in range(N-1):
        j = i + 1
        dist += (((ls[p[i]][0])-(ls[p[j]][0]))**2+((ls[p[i]][1])-(ls[p[j]][1]))**2)**0.5
print(dist/math.factorial(N))

Solution (Python):
import math

N = int(input())

position_list = [0]*N
for i in range(N):
    position_list[i] = list(map(int , input().split()))

ans = 0

for i in range(N):
    for j in range(N):
        if i != j:
            x_distance = position_list[i][0] - position_list[j][0]
            y_distance = position_list[i][1] - position_list[j][1]
            ans += math.sqrt(x_distance**2 + y_distance**2)

print(ans/N)

Solution (Python):

from itertools import permutations

def solve(n,a):
    def dist(a1,a2):
        d2 = (a1[0]-a2[0])**2 + (a1[1]-a2[1])**2
        return d2 ** (1/2)
    
    l = []
    p_list = permutations(range(n))
    cnt = 0
    for p in p_list:
        s = 0
        for i in range(n-1):
            s += dist(a[p[i+1]],a[p[i]])
        l.append(s)
        cnt += 1
    return sum(l)/cnt


def main():
    N = int(input())
    a = [list(map(int,input().split())) for _ in range(N)]
    print(solve(N,a))
    return

if __name__ == '__main__':
    main()

Solution (Python):
import math

N = int(input())
Nlist = [list(map(int, input().split())) for i in range(N)]
total = 0
for i in range(N-1):
    for j in range(i + 1, N):
        total += math.sqrt((Nlist[i][0]-Nlist[j][0])**2 + (Nlist[i][1]-Nlist[j][1])**2)
        
ans = total*2/N
print(ans)

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

Solution (Python):
import itertools
import math
n = int(input())
XY = [tuple(map(int,input().split())) for i in range(n)]
ans = 0
for XYs in itertools.permutations(XY):
  for i in range(n-1):
    ans += math.sqrt((XYs[i][0] - XYs[i+1][0])**2 + 
              (XYs[i][1] - XYs[i+1][1])**2) 
ans /= math.factorial(n)
print(ans)

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

Solution (Python):
from math import sqrt
from itertools import combinations

(n,), *xx = [list(map(int, s.split())) for s in open(0)]

tmp = 0

for i,j in combinations(range(n),2):
    xi, yi = xx[i]
    xj, yj = xx[j]
    tmp += sqrt((xi-xj)**2 + (yi-yj)**2)

print(tmp / n * 2)

Solution (Python):
import sys
sys.setrecursionlimit(4100000)
import math
INF = 10**9
import itertools


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

    total = 0
    P = itertools.permutations(range(n))
    P = list(P)
    for i in range(len(list(P))):
        for j in range(n-1):
            total += math.sqrt((X[P[i][j]]-X[P[i][j+1]])**2 + (Y[P[i][j]]-Y[P[i][j+1]])**2)
    print(total/len(P))


if __name__ == '__main__':
    main()

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

Solution (Python):
import itertools
import math

N = int(input())





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

tot = 0

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

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

for l_1d in l:
    for i in range(N - 1):
        tot += distance(x1 = X[l_1d[i + 1]], y1 = Y[l_1d[i + 1]], x2 = X[l_1d[i]], y2 = Y[l_1d[i]])

print(tot / len(l))

Solution (Python):

import math

(n, ), *p = [[*map(int, i.split())] for i in open(0)]
m = 0.0
f = math.factorial(n - 1)*2
for i in range(n):
    for j in range(i + 1, n):
        m += ((p[i][0] - p[j][0])**2 + (p[i][1] - p[j][1])**2)**0.5*f
print(m / math.factorial(n))

Solution (Python):
import itertools

n = int(input())
p = [tuple(map(int, input().split())) for n_ in range(n)]
routes = [i for i in itertools.permutations(range(0,n), n)]

distances = 0
for route in routes:
    for i in range(len(route)-1):
        distances += ((p[route[i]][0] - p[route[i+1]][0])**2 + (p[route[i]][1] - p[route[i+1]][1])**2) ** 0.5

print(distances / len(routes))

Solution (Python):
from math import hypot, factorial


def dfs(d, p):
    if all(used):
        return d
    res = 0
    for i in range(n):
        if used[i]:
            continue
        used[i] = True
        res += dfs(d + hypot(x[i] - x[p], y[i] - y[p]), i)
        used[i] = False
    return res


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

ans = 0
used = [False] * n
for i in range(n):
    used[i] = True
    ans += dfs(0, i)
    used[i] = False
ans /= factorial(n)

print(ans)

Solution (Python):
import itertools
import math

N = int(input())

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

dis ={}

for i in range(len(town)):
    for j in range(i+1, len(town)):
        distance = math.sqrt((town[i][0] - town[j][0])**2 + (town[i][1] - town[j][1])**2)
        dis[str(i)+str(j)] = distance
        dis[str(j)+str(i)] = distance
permutations = list(itertools.permutations(list(range(N))))

all_dis = 0
for p in permutations:
    for i in range(len(p)-1):
        all_dis += dis[str(p[i])+str(p[i+1])]

print(all_dis / len(permutations))

Solution (Python):
import itertools

n = int(input())
town = [tuple(map(int,input().split())) for _ in range(n)]
c_town = list(itertools.combinations(town,2))
lp = len(list(itertools.permutations(town)))
sum_road = 0

for i in c_town:
  sum_road += ((i[0][0]-i[1][0])**2 + (i[0][1]-i[1][1])**2)**0.5
  
print(2*sum_road/n)

Solution (Python):
import itertools
import math

n = int(input())

xy = []

for i in range(n):
    a,b = map(int,input().split(" "))
    xy.append((a,b))
count = 0
res = 0.0
for v in itertools.permutations(list(range(0,n))):
    count += 1
    pre = v[0]
    for i in v[1:]:
        res += math.sqrt((xy[pre][0] - xy[i][0])**2 + (xy[pre][1] - xy[i][1])**2)
        pre = i
print(res/count)

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

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

Solution (Python):
from itertools import permutations


def dfs(p, bit):
    res = 0
    for k in range(n):
        if bit >> k & 1:
            continue
        d = ((x[k] - x[p]) ** 2 + (y[k] - y[p]) ** 2) ** 0.5
        res += d + dfs(k, bit | (1 << k))
    return res


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

ans = 0




for li in list(permutations(range(n), n)):
    tmp = 0
    for i in range(n - 1):
        p = li[i]
        k = li[i + 1]
        tmp += ((x[k] - x[p]) ** 2 + (y[k] - y[p]) ** 2) ** 0.5
    ans += tmp
for i in range(n):
    ans /= i + 1

print(ans)

Solution (Python):
import math
from itertools import permutations


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

total_distance = 0
num = 0
for perm_points in permutations(point, N):
    num += 1
    for i in range(N - 1):
        p1 = perm_points[i]
        p2 = perm_points[i + 1]
        total_distance += math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)

print(total_distance / num)

Solution (Python):
from itertools import permutations
from math import factorial
n = int(input())
town = [complex(*map(int,input().split())) for _ in range(n)]
d = 0
for l in permutations(town):
  for i in range(n-1):
    d += abs(l[i]-l[i+1])
print(d/factorial(n))

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

Solution (Python):
import sys


import heapq


from collections import deque


def main():
    N = int(input())
    X=[]
    Y =[]
    for i in range(N):
        x,y = map(int,input().split())
        X.append(x)
        Y.append(y)
    ans =0

    for i in range(N):
        for j in range(N):
            if i==j:
                continue
            ans += ((X[i]-X[j])**2 + (Y[i]-Y[j])**2 )**0.5

    print(ans/N)




if __name__ == "__main__":
    main()

Solution (Python):
import itertools

N = int(input())
towns = [i for i in range(N)]

coordinates = {}
for i in range(N):
    tmp = list(map(int, input().split()))
    coordinates[i] = tmp

paths = list(itertools.permutations(towns, N))
distance = 0

for i in range(len(paths)):
    for j in range(N - 1):  
        town_now = paths[i][j]
        town_next = paths[i][j+1]
        distance += ((coordinates[town_now][0] - coordinates[town_next][0]) ** 2 +
                     (coordinates[town_now][1] - coordinates[town_next][1]) ** 2) ** (1/2)

avg_distance = distance / (len(paths))
print(avg_distance)

Solution (Python):
import itertools

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

cand = list(itertools.permutations(list(range(n)), n))

ans = 0
for cand_i in cand:
  for i in range(n-1):
    ans += ((XY[cand_i[i]][0]-XY[cand_i[i+1]][0])**2+(XY[cand_i[i]][1]-XY[cand_i[i+1]][1])**2)**(1/2)

print(ans/len(cand))

Solution (Python):
N=int(input())
position_list=[0]*N

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

import math
ans=0

for i in range(N):
    for j in range(N):
        if i != j:
            x_distance=position_list[int(i)][0]-position_list[int(j)][0]
            y_distance=position_list[int(i)][1]-position_list[int(j)][1]
            ans+=math.sqrt(x_distance**2+y_distance**2)

print(ans/N)

Solution (Python):

import math
import sys
from itertools import permutations

readlines = sys.stdin.buffer.readlines

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

total = 0
n = 0

for pattern in permutations(patterns):

    for i in range(len(pattern) - 1):
        town_a = pattern[i]
        town_b = pattern[i + 1]

        distance = math.sqrt((town_a[0] - town_b[0])**2 +
                             (town_a[1] - town_b[1])**2)
        total += distance

    n += 1

print(total / n)

Solution (Python):
from math import sqrt
from itertools import permutations

def dist(x0,y0,x1,y1):
  return sqrt((x0-x1)**2+(y0-y1)**2)

N=int(input())
x,y=[],[]
for _ in range(N):
  x_tmp,y_tmp=list(map(int,input().split()))
  x.append(x_tmp)
  y.append(y_tmp)
ls=list(permutations(list(range(N)),N))
ans=0
for l in ls:
  idx=0
  while idx+1<len(l):
    ans+=dist(x[l[idx]],y[l[idx]],x[l[idx+1]],y[l[idx+1]])
    idx+=1
print(ans/len(ls))

Solution (Python):




import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial,sqrt
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
lis=[LI() for i in range(n)]
ans=0
for i in range(n):
    for j in range(n):
        ans+=sqrt((lis[i][0]-lis[j][0])**2+(lis[i][1]-lis[j][1])**2)
print(ans/n)

Solution (Python):
import math
from itertools import permutations

def dist(x0, y0, x1, y1):
    return math.sqrt((x0 - x1) ** 2 + (y0 - y1) ** 2)

def resolve():
    N = int(input())
    x, y = [], []
    for _ in range(N):
        x_tmp, y_tmp = list(map(int, input().split()))
        x.append(x_tmp)
        y.append(y_tmp)

    ls = list(permutations(list(range(N)), N))
    ans = 0
    for l in ls:
        idx = 0
        while idx + 1 < len(l):
            ans += dist(x[l[idx]], y[l[idx]], x[l[idx+1]], y[l[idx+1]])
            idx += 1

    print(ans / len(ls))
    return

resolve()

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate 

from collections import deque,defaultdict,Counter
import decimal
import re











import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
xy = []
for i in range(n):
    x,y = readInts()
    xy.append((x,y))
dist = 0
for x1,y1 in xy:
    for x2,y2 in xy:
        dist += ((x1-x2)**2 + (y1-y2)**2)**0.5
print(dist/n)

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

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

ans = []
for v in itertools.permutations(list(range(n)), n):
    a = 0
    for i in range(len(v)-1):
        x1,y1 = xy[v[i]][0], xy[v[i]][1]
        x2,y2 = xy[v[i+1]][0], xy[v[i+1]][1]
        a += ((x1-x2)**2+(y1-y2)**2)**0.5
    ans.append(a)
print(sum(ans)/len(ans))

Solution (Python):
from itertools import permutations
def dist(x,y):
    return ((x[0]-y[0])**2+(x[1]-y[1])**2)**0.5
N = int(input())
X = [list(map(int,input().split())) for _ in range(N)]
cnt = 0
for z in permutations(range(N),N):
    for i in range(1,N):
        cnt += dist(X[z[i]],X[z[i-1]])
k = 1
for i in range(2,N+1):
    k = k*i
print(cnt/k)

Solution (Python):
import math
n=int(input())
towns=[]
for i in range(n):
  x,y=map(int,input().split())
  towns.append([x,y])
total=0
for i in range(n-1):
  for j in range(i+1,n):
    total+=math.sqrt((towns[i][0]-towns[j][0])**2+(towns[i][1]-towns[j][1])**2)
print(2*total/n)

Solution (Python):
from itertools import permutations
from math import sqrt

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

kyori=[]
for i in range(n):
    kyori.append([])

for i in range(n):
    for j in range(n):
        kyori[i].append(sqrt((lst[i][0]-lst[j][0])**2 + (lst[i][1]-lst[j][1])**2))

pmt=list(permutations(range(0,n)))
ans=0
for i in range(len(pmt)):
    for j in range(n-1):
        ans+=kyori[pmt[i][j]][pmt[i][j+1]]

print(ans/len(pmt))

Solution (Python):

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


l = [i for i in range(N)]
p = list(itertools.permutations(l,N))
S = 0

for v in p:
    for i in range(N-1):
        S += math.sqrt((R[v[i]][0]-R[v[i+1]][0])**2+(R[v[i]][1]-R[v[i+1]][1])**2)

ans = S / len(p)
print(ans)

Solution (Python):
import itertools

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

dis_count=[]
for i in itertools.permutations([i for i in range(1,N+1)],N):
    dis = 0
    for j in range(N-1):
        dis += ((xy[i[j]-1][0]-xy[i[j+1]-1][0])**2+(xy[i[j]-1][1]-xy[i[j+1]-1][1])**2)**0.5
    dis_count.append(dis)

print(sum(dis_count)/len(dis_count))

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


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

from itertools import permutations
def func(a):
    if a==1:
        return 1
    return a*func(a-1)

counter = 0
for i in permutations(range(N)):
    x_now,y_now = li[i[0]]
    path = i[1:]
    for next in path:
        x_next,y_next = li[next]
        
        ok = (x_next-x_now)**2+(y_next-y_now)**2
        distans = math.sqrt(ok)
    
        counter += distans
 
        x_now,y_now  = x_next,y_next
        
    
oppai = func(N)
print(counter/oppai)

Solution (Python):
import itertools
import math

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

numbers = 1
for i in range(1,n+1):
    numbers *= i


route = list(itertools.permutations(point))

d = 0

for pattern in route:
    for i in range(n-1):
        d += math.sqrt((pattern[i][0]-pattern[i+1][0])**2 + (pattern[i][1]-pattern[i+1][1])**2)
        

ans = d / numbers

print(ans)

Solution (Python):
from itertools import permutations
from math import sqrt

N = int(input())
pos = {i: tuple(map(int, input().split())) for i in range(1, N + 1)}


total = 0
for i, permutation in enumerate(permutations([i for i in range(1, N + 1)]), 1):
    for j in range(N - 1):
        x_i, y_i = pos[permutation[j]]
        x_j, y_j = pos[permutation[j + 1]]
        dis = sqrt((x_i - x_j) ** 2 + (y_i - y_j) ** 2)
        total += dis

print(total / i)

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

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

from itertools import permutations
a=range(1,N+1)
ps = list(permutations(a))


dammy=0
for p in ps:
    
    for j in range(1,N):
        dammy += ((xy[p[j-1]-1][0] - xy[p[j]-1][0])**2 + (xy[p[j-1]-1][1] - xy[p[j]-1][1])**2) ** (1/2)
        
print(dammy/len(ps))

Solution (Python):
from itertools import permutations
from math import sqrt,factorial
n=int(input())
ans=0
distance=[list(map(int,input().split())) for i in range(n)]
town=[]
for i in range(n):
    town.append(i)
for a in list(permutations(town)):
    for j in range(n-1):
        ans+=sqrt((distance[a[j+1]][1]-distance[a[j]][1])**2+(distance[a[j+1]][0]-distance[a[j]][0])**2)
print(ans/(factorial(n)))

Solution (Python):
from itertools import combinations, permutations

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


dists = {i: {j: 0 for j in range(N)} for i in range(N)}
for i,j in combinations(range(N), 2):
    x1, y1 = xy[i]
    x2, y2 = xy[j]
    dists[i][j] = dists[j][i] = ((x1 - x2)**2 + (y1 - y2)**2)**(0.5)

factorial_of_N = 0
total_dist = 0.0
for route in permutations(range(N)):
    prev_i = route[0]
    factorial_of_N += 1
    for i in route[1:]:
        total_dist += dists[prev_i][i]
        prev_i = i

print(total_dist / factorial_of_N)

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

a=[]
for j in range(0,N):
    a.append(j)
ans=[]
for v in itertools.permutations(a, N):
    v=list(v)
    K=0
    for j in range(N-1):
        K+=math.sqrt((L[v[j+1]][0]-L[v[j]][0])**2+(L[v[j+1]][1]-L[v[j]][1])**2)
    ans.append(K)
    
print(sum(ans)/len(ans))

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

ans=0

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

print(ans/n)

Solution (Python):
from math import sqrt
N = int(input())
pts = []
for i in range(N):
    x,y = map(int,input().split())
    pts.append((x,y))
def fact(n):
    if n == 1:
        return 1
    return fact(n-1)*n
ans = 0
for i in range(N):
    for j in range(N):
        x1,y1 = pts[i]
        x2,y2 = pts[j]
        dx = x1-x2
        dy = y1-y2
        ans += fact(N-1)*(sqrt(dx**2+dy**2))
print(ans/fact(N))

Solution (Python):
import itertools
N =int(input())
X = [list(map(int,input().split())) for i in range(N)]
dist = 0
counter = 0
perm = list(itertools.permutations(list(range(N)),N))
for s in perm:
    idist = 0
    for i in range(N-1):
        idist +=( (X[s[i]][0]-X[s[i+1]][0])**2 + (X[s[i]][1]-X[s[i+1]][1])**2)**0.5
    dist += idist
    counter += 1
print(dist/counter)

Solution (Python):
import itertools as itr

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

L = list(range(N))
P = list(itr.permutations(L,N))

D = 0
for i in P:
    for j in range(N-1):
        D += ((x[i[j]] - x[i[j+1]])**2 + (y[i[j]] - y[i[j+1]])**2)**0.5

print(D / len(P))

Solution (Python):
from itertools import permutations

def main():
    def distance(x1, y1, x2, y2):
        return((abs(x1-x2)**2 + abs(y1-y2)**2) ** (1/2))
        
    n = int(input())
    l = list(range(n))
    perm = list(permutations(l, n))
    perm_l = len(perm)
    a = [0] * n
    b = [0] * n
    for i in range(n):
        a[i], b[i] = map(int, input().split())
    ans = 0
    for p in perm:
        for i in range(n-1):
            ans += distance(a[p[i]], b[p[i]], a[p[i+1]], b[p[i+1]])
    print(ans/perm_l)

if __name__ == '__main__':
    main()

Solution (Python):
import itertools
import math

n = int(input())
distances = []
for index in range(n):
    distance = list(map(int, input().split()))
    distances.append(distance)

output = []
permutation = list(itertools.permutations(range(n)))
for p in permutation:
    total = 0
    for index in range(len(p) - 1):
        idx1, idx2 = p[index], p[index+1]
        d0 = distances[idx1]
        d1 = distances[idx2]
        
        total += math.sqrt((d0[0] - d1[0]) * (d0[0] - d1[0]) + (d0[1] - d1[1]) * (d0[1] - d1[1]))
    output.append(total)
print(sum(output) / len(permutation))

Solution (Python):
from itertools import permutations


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

    combs = list(permutations(cities))
    sum_dist = 0
    for comb in combs:
        for i in range(len(comb)-1):
            idx1 = comb[i]
            idx2 = comb[i+1]
            sum_dist += (
                (xys[idx1][0] - xys[idx2][0]) ** 2 +
                (xys[idx1][1] - xys[idx2][1]) ** 2) ** 0.5
    print(sum_dist / len(combs))


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
x=[]
y=[]
for i in range(n):
  a,b=map(int,input().split())
  x.append(a)
  y.append(b)
ans=0
for i in range(n):
  for j in range(n):
    ans+=((x[i]-x[j])**2+(y[i]-y[j])**2)**.5
print(ans/n)

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


pattern = list(permutations(range(n)))

ans = 0
for p in pattern:
    tmp = 0
    for i in range(n - 1):
        tmp += ((xy[p[i + 1]][0] - xy[p[i]][0])**2 +
                (xy[p[i + 1]][1] - xy[p[i]][1])**2)**0.5
    ans += tmp

print(ans / len(pattern))

Solution (Python):


import math
import itertools


def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力を取得
    """
    N = int(input())
    x, y = [], []
    for n in range(N):
        x_n, y_n = list(map(int, input().split()))
        x.append(x_n)
        y.append(y_n)

    return N, x, y


def get_distance(x: list, y: list, i: int, j: int) -> float:
    """
    町iと町jの間の距離を取得する.

    Args:\n
        x (list): 町のx座標
        y (list): 町のy座標
        i (int): 町
        j (int): 町

    Returns:\n
        float: 町iと町jの間の距離
    """
    return math.sqrt((x[i] - x[j])**2 + (y[i] - y[j])**2)


def get_path_length(N: int, distance: dict, path: list) -> float:
    """
    経路の長さを求める.

    Args:\n
        N (int): 町の数
        distance (dict): 町間の距離
        path (list): 経路

    Returns:\n
        float: 経路の長さ
    """
    path_length = 0
    for n in range(N - 1):
        path_length += distance[(path[n], path[n + 1])]

    return path_length


def main(N: int, x: list, y: list) -> None:
    """
    メイン処理.

    Args:\n
        N (int): 町の数
        x (list): 町のx座標
        y (list): 町のy座標
    """
    
    distance = {}
    for i, j in itertools.product(range(N), repeat=2):
        distance[(i, j)] = get_distance(x, y, i, j)

    
    ans = 0
    for path in itertools.permutations(range(N)):
        ans += get_path_length(N, distance, path)
    ans /= math.factorial(N)

    
    print(ans)


if __name__ == "__main__":
    
    N, x, y = get_input()

    
    main(N, x, y)

Solution (Python):


import math
import itertools


N = int(input())
x, y = [], []
for n in range(N):
    x_n, y_n = list(map(int, input().split()))
    x.append(x_n)
    y.append(y_n)


def get_distance(i: int, j: int) -> float:
    return math.sqrt((x[i] - x[j])**2 + (y[i] - y[j])**2)


def get_path_length(distance: dict, path: list) -> float:
    path_length = 0
    for n in range(N - 1):
        path_length += distance[(path[n], path[n + 1])]

    return path_length



distance = {}
for i, j in itertools.product(range(N), range(N)):
    distance[(i, j)] = get_distance(i, j)

ans = 0
for path in itertools.permutations(range(N)):
    ans += get_path_length(distance, path)
ans /= math.factorial(N)


print(ans)

Solution (Python):
import itertools
import numpy as np

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

city = [i for i in range(n)]
dist_list = []
for root in itertools.permutations(city):
  dist = 0
  for i in range(1,len(root)):
    now = root[i]-1
    prev = root[i-1]-1
    
    prev_x = posi_list[prev][0]
    prev_y = posi_list[prev][1]
    now_x = posi_list[now][0]
    now_y = posi_list[now][1]
    
    dist += np.sqrt((now_x-prev_x)**2 + (now_y-prev_y)**2)
  dist_list.append(dist)
  
print(np.mean(dist_list))

Solution (Python):






import math





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





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

Solution (Python):
import sys
from itertools import permutations
from math import factorial

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def distance_nd(p, q):
    return sum((px - qx) ** 2.0 for px, qx in zip(p, q)) ** 0.5


def main():
    N = int(readline())
    P = [0] * N
    for i in range(N):
        P[i] = tuple(map(int, readline().split()))

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

    ans = 0
    for perm in permutations(range(N)):
        for i in range(N - 1):
            ans += dist[perm[i]][perm[i + 1]]

    ans /= factorial(N)

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import itertools
import math
N = int(input())
xy = [list(map(int,input().split())) for _ in range(N)]
distance = 0
pt = list(itertools.permutations(xy, N))
bunbo = len(pt)

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


print(distance/bunbo)