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 sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]

    N = int(input())
    xy = XLMIIS(N)
    

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

    def f(n):
        ans = n
        if n <= 0:
            return 1
        for i in range(1,n):
            ans *= i
        return ans
    

    sumdis = 0
    for i in range(N):
        for j in range(i+1,N):
            sumdis += distance(xy[i][0],xy[i][1],xy[j][0],xy[j][1])
    summ = sumdis  / N * 2
    print(summ)


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
data=[]
for i in range(n):
    data.append(list(map(int,input().split())))
ttl_len=0
for j in range(n):
    for k in range(n):
        if k<j:
            ttl_len+=2*((data[j][0]-data[k][0])**2+(data[j][1]-data[k][1])**2)**(1/2)
        else:
            continue
print(ttl_len/n)

Solution (Python):
import math
import itertools
import statistics

N = int(input())



cities_cord = []

for i in range(N):
    a = list(map(int,input().split()))
    cities_cord.append(a)
    
cities_no = list(range(N))

distance = []

for v in itertools.permutations(cities_no):
    all_dist = 0
    for i in range(len(v)-1):
        dist = math.sqrt((cities_cord[v[i]][0]-cities_cord[v[i+1]][0])**2 +
                         (cities_cord[v[i]][1]-cities_cord[v[i+1]][1])**2)
        all_dist += dist
        
    distance.append(all_dist)
    

print(statistics.mean(distance))

Solution (Python):
import math

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

for i in range(n):
    ix,iy=xy[i]
    tmp=[]
    for j in range(n):
        jx,jy=xy[j]
        tmp.append(math.sqrt((ix-jx)**2+(iy-jy)**2))
    l.append(tmp)

pat=1
for i in range(1,n+1):
    pat*=i
sa=1
for i in range(1, n-1):
    sa*=i
wei=pat/(n-1)-sa

sum=0
for i in range(n):
    for j in range(i+1, n):
        sum+=(wei*l[i][j])*2

print(sum/pat)

Solution (Python):
import itertools

if __name__ == '__main__':
    n = int(input())
    x, y = [], []
    for i in range(n):
        a, b = map(float, input().split())
        x.append(a)
        y.append(b)

    dp = []
    for i in range(n):
        xy = []
        for j in range(n):
            if i != j:
                xy.append(((x[i]-x[j])**2+(y[i]-y[j])**2)**0.5)
            else:
                xy.append(0)
        dp.append(xy)

    ave = 0.
    for v in itertools.permutations(range(n), n):
        for i in range(n-1):
            ave += dp[v[i]][v[i+1]]

    for i in range(1, n+1):
        ave /= i
    print(ave)

Solution (Python):
def frac(n):
    if n==1:
        return 1
    else:
        return n*frac(n-1)

n=int(input())
x=[0]*n
y=[0]*n
for i in range(n):
    x[i],y[i]=list(map(int,input().split()))
ans=0
for i in range(n):
    for j in range(i+1,n):
        ans+=((x[i]-x[j])**2+(y[i]-y[j])**2)**0.5
print(ans*frac(n-1)*2/frac(n))

Solution (Python):
import math

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


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


ans = 0
for i in range(N):
    start = XY_list[i]
    for j in range(i+1, N):
        node = XY_list[j]
        ans += distance(start[0], start[1], node[0], node[1])

print(ans * 2 / N)

Solution (Python):
import math
N = int(input())
xy = [tuple(map(int, input().split())) for i in range(N)]
ans = 0
for i in range(N):
    for j in range(i+1, N):
        if i == j:
            continue
        else:
            ans += ((xy[i][0] - xy[j][0])**2 + (xy[i][1] - xy[j][1])**2)**0.5

print(ans*math.factorial(N-2)*(N-1) / math.factorial(N) * 2)

Solution (Python):
import itertools
import numpy as np

N = int(input())

l = []
d = {}
for i in range(1, N+1):
    x, y = [int(n) for n in input().split(' ')]
    l.append(i)
    d[i] = (x, y)

temp = list(itertools.permutations(l))

length = []
for tmp in temp:
    length_i = 0
    for i in range(1, len(tmp)):
        length_i += np.sqrt((d[tmp[i]][0]-d[tmp[i-1]][0])**2+(d[tmp[i]][1]-d[tmp[i-1]][1])**2)
    length.append(length_i)

print(np.average(length))

Solution (Python):
import math

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

dis=[0]*(n*(n-1)//2)
k=0
for i in range(n):
    for j in range(i+1, n):
        dis[k] = math.sqrt(((city[i][0]-city[j][0])**2+(city[i][1]-city[j][1])**2))
        k+=1

ans=sum(dis)*2/n

print(ans)

Solution (Python):
import itertools
import math

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

seq = list()
for i in range(n):
    seq.append(i)

seq_all = list(itertools.permutations(seq))

ans = 0
for i in seq_all:
    for j in range(n - 1):
        end = i[j + 1]
        start = i[j]
        ans += math.sqrt((a[end][0] - a[start][0]) ** 2 + (a[end][1] - a[start][1]) ** 2)

print(ans / len(seq_all))

Solution (Python):
import itertools

N = int(input())


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

seq = range(0,N)
kumi = list(itertools.permutations(seq))

sum = 0
for k in kumi:
    for i, k_i in enumerate(k[:N-1]):
        n_i = k[i + 1]
        sum += ((X[k_i] - X[n_i]) ** 2 + (Y[k_i] - Y[n_i]) ** 2) ** 0.5

print(sum / len(kumi))

Solution (Python):
import numpy as np
import sys
import math
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
XY = np.array(read().split(), np.int64)
X = XY[0::2]
Y = XY[1::2]

t = 0
for i in range(N):
    for j in range(i, N):
        t += math.sqrt((X[i] - X[j])**2 + (Y[i] - Y[j])**2)
print(2*t/N)

Solution (Python):
import itertools
import numpy as np
n = int(input())
xy = []
ans_sum = 0
count = 0
for i in range(n):
    xy.append(list(map(int,input().split())))
for i in itertools.permutations(xy):
    ans = 0
    for j in range(n-1):
        ans += np.sqrt((i[j][0]-i[j+1][0])**2 + (i[j][1]-i[j+1][1])**2)
    count += 1
    ans_sum += ans
print("{:9f}".format(ans_sum/count))

Solution (Python):
import itertools

n = int(input())
x = []
y = []

for i in range(n):
    x_, y_ = map(int,input().split())
    x.append(x_)
    y.append(y_)

li = list(range(n))
p = list(itertools.permutations(li))
di = 0
for i in p:
    i = list(i)

    for j in range(len(i)-1):
        xd = x[i[j]] - x[i[j + 1]]
        yd = y[i[j]] - y[i[j + 1]]
        di += (xd**2 + yd**2)**0.5

print(di/len(p))

Solution (Python):
import math
import itertools
N = int(input())
towns = [list(map(int, input().split())) for _ in range(N)]
seqs = list(itertools.permutations(range(N)))
length = 0
for seq in seqs:
    for i in range(1, N):
        x_s = towns[seq[i-1]][0]
        x_e = towns[seq[i]][0]
        y_s = towns[seq[i-1]][1]
        y_e = towns[seq[i]][1]
        length += math.sqrt((x_e-x_s)**2+(y_e-y_s)**2)
print(length/len(seqs))

Solution (Python):
import itertools

n = int(input())

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

dist_list = list()
dist_way = list(itertools.permutations(range(n)))

for dist_i in dist_way:
  dist = 0
  for i in range(len(dist_i)-1):
    dist += ((city_pos[dist_i[i]][0] - city_pos[dist_i[i+1]][0])**2 + (city_pos[dist_i[i]][1] - city_pos[dist_i[i+1]][1])**2)**(0.5)
  
  dist_list.append(dist)
  
print(sum(dist_list)/len(dist_list))

Solution (Python):
import itertools
import math

def main():
    n = int(input())
    ls = [list(map(int,input().split())) for i in range(n)]
    ans = 0
    comb = 0

    for i in itertools.permutations(range(n)):
        comb += 1
        for j in range(n-1):
            ans += math.sqrt((ls[i[j]][0]-ls[i[j+1]][0])**2 + (ls[i[j]][1]-ls[i[j+1]][1])**2)
    print(ans/comb)


if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np
import itertools
import math
N = int(input())
x_list = [0] * N
y_list = [0] * N
for i in range(N):
    x_list[i], y_list[i] = map(int, input().split())

x_p_list = list(itertools.permutations(x_list))
y_p_list = list(itertools.permutations(y_list))

n = math.factorial(N)

ans_list = []
for i in range(n): 
    ans=0
    for j in range(N-1): 
        x_l2 = (x_p_list[i][j] - x_p_list[i][j+1]) ** 2
        y_l2 = (y_p_list[i][j] - y_p_list[i][j+1]) ** 2
        ans += np.sqrt(x_l2 + y_l2)
    ans_list.append(ans)

print(sum(ans_list) / n)

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

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

pattern_list = list(itertools.permutations(seq_list))

distance = 0

for j in range(len(pattern_list)):
    distance_sum_tmp = 0
    for k in range(len(pattern_list[j])-1):
            index_from = pattern_list[j][k]
            index_to = pattern_list[j][k+1]
            distance_sum_tmp += (((xy_list[index_from][0] - xy_list[index_to][0]) ** 2 ) + ((xy_list[index_from][1] - xy_list[index_to][1]) ** 2 )) ** 0.5
    distance += distance_sum_tmp

print(distance / len(pattern_list))

Solution (Python):
import math

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

frac = math.factorial(N)
route_count = math.factorial(N-1)
sum = 0

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

print(sum/N)

Solution (Python):
import itertools

N = int(input())
X = []
Y = []

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


T = []
for i in range(N):
    T.append(i)

D = 0

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

for v in itertools.permutations(T, N):
    Dtmp = 0
    for j in range(N-1):
        Dtmp += dist(X[v[j]],X[v[j+1]],Y[v[j]],Y[v[j+1]])
    D += Dtmp

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

output = D/P

print(output)

Solution (Python):
import sys
import itertools


def get_distance(from_town, to_town):
    distance = ((from_town[0] - to_town[0]) ** 2 + (from_town[1] - to_town[1]) ** 2) ** 0.5
    return distance


input = sys.stdin.readline

N = int(input())

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

patterns = itertools.permutations(townmap, N)

sum_v = 0
cnt_p = 0
for p in patterns:
    cnt_p += 1
    for i in range(len(p) - 1):
        sum_v += get_distance(p[i], p[i + 1])

result = sum_v / cnt_p


print(result)

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

Solution (Python):

import numpy as np
import itertools
N = int(input())
town_dic = {}
for i in range(N):
    x, y = map(int, input().split())
    town_dic[i] = [x, y]

l = [i for i in range(N)]
case_list_list = itertools.permutations(l)
mean_distance = 0
temp_distance = 0
counter = 0
for case_list in case_list_list:
    counter += 1
    temp_distance = 0
    for i in range(N - 1):
        temp_distance += np.sqrt((town_dic[case_list[i]][0] - town_dic[case_list[i + 1]][0]) ** 2 + (town_dic[case_list[i]][1] - town_dic[case_list[i + 1]][1]) ** 2)
    mean_distance += temp_distance

print(mean_distance / counter)

Solution (Python):
import itertools, math

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

sum = 0
cnt = 0

for v in itertools.permutations(d, N):
  cnt += 1
  for i in range(N - 1):
    x1 = v[i][0]
    y1 = v[i][1]
    x2 = v[i + 1][0]
    y2 = v[i + 1][1]
    sum += math.sqrt((abs(x1 - x2)**2) + (abs(y1 - y2)**2))
print(sum / cnt)

Solution (Python):
import math
N=int(input())
S=list()
ans=0

const=math.factorial(N-1)*2/math.factorial(N)

for i in range(N):
    S.append(list(map(int,input().split())))


for i in range(N-1):
    for j in range(i+1,N):
        x1,y1=S[i]
        x2,y2=S[j]
        ans+=const*(((x1-x2)**2+(y1-y2)**2)**(1/2))

print(ans)

Solution (Python):
'''
def factorial(n):
    if n == 1:
        return n
    return n*factorial(n-1)
'''
n = int(input())
xy = [list(map(int,list(input().split()))) for i in range(n)]

sum = 0
for ida in range(n):
    for idb in range(n):
        if ida == idb: continue
        sum += ((xy[ida][0]-xy[idb][0])**2+(xy[ida][1]-xy[idb][1])**2)**0.5

ans = sum / n

print(ans)

Solution (Python):
n=int(input())
x=[0]*n
y=[0]*n
sum=0

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

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

print(sum/n)

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

N=int(input())
A=np.zeros((8,2)).astype(int)
for i in range(N):
  temp=list(map(int, input().split()))
  A[i,0]=temp[0]
  A[i,1]=temp[1]


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



order=list(itertools.permutations(range(N)))

tab=np.zeros((N,N))
for i in range(N):
  for j in range(i+1,N):
    tab[i,j]=distance(A[i,0],A[i,1],A[j,0],A[j,1])
    tab[j,i]=distance(A[i,0],A[i,1],A[j,0],A[j,1])
    
ret=0
C=math.factorial(N)      
for i in range(C):
  o=order[i]
  for j in range(1,N):
    ret+=tab[o[j],o[j-1]]
print(ret/C)

Solution (Python):
import math
import itertools
n = int(input())
x = []
y = []
for _ in range(n):
    x1,y1 = map(int,input().split())
    x.append(x1)
    y.append(y1)
ans = 0
l = [int(i) for i in range(n)]
p = list(itertools.permutations(l,n))
for i in range(len(p)):
    for j in range(n-1):
        ans += ((x[p[i][j]]-x[p[i][j+1]])**2 + (y[p[i][j]]-y[p[i][j+1]])**2)**(0.5)
print(ans/math.factorial(n))

Solution (Python):
import itertools
import math
N=int(input())
B=[]
C=[1,2,3]
for i in range(N):
    A,C=map(int,input().split())
    B.append([A,C])


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



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

print(total/len(D))

Solution (Python):
import math
def permute(arr, s, n): 
    global result
    if (s == 1): 
        i=0
        distance=0
        while(i<n-1):
            distance+=math.sqrt(pow(A[arr[i]][0]-A[arr[i+1]][0],2)+pow(A[arr[i]][1]-A[arr[i+1]][1],2))
            i+=1
        result+=distance
        return
    for i in range(s): 
        permute(arr,s-1,n); 
        if s&1==1: 
            arr[0], arr[s-1] = arr[s-1],arr[0] 
        else: 
            arr[i], arr[s-1] = arr[s-1],arr[i] 

n=int(input())
A=[]
result=0
for i in range(n):
    x,y=list(map(int,input().split()))
    A.append([x,y])
permute([i for i in range(0,n)],n,n)
print(result/math.factorial(n))

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

Solution (Python):
import itertools
import math

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

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

sum=0

for i in itertools.permutations(range(N),N):
    ret=0
    for j in range(N-1):
        ret+=distance(i[j],i[j+1])
    sum+=ret

div = math.factorial(N)
print(sum /div)

Solution (Python):
from itertools import combinations
import math
from statistics import mean

N = int(input())
l = []
ans = []
for i in range(N):
  l.append(list(map(int, input().split())))
  
for v in combinations(l, 2):
  ans.append(math.sqrt((v[0][0]-v[1][0])**2 + (v[0][1]-v[1][1])**2))
  
print(sum(ans)/N*2)

Solution (Python):

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


ANS = 0

def dfs(d, old, x):
	global ANS
	if sum(old)==N:
		ANS += d
		return

	for i in range(N):
		if not(old[i]):
			old[i]=True
			t = ((T[x][0]-T[i][0])**2 + (T[x][1]-T[i][1])**2)**.5
			dfs(d+t, old, i)
			old[i] = False


old = [False]*N

for i in range(N):
	old[i] = True
	dfs(0,old, i)
	old[i] = False

for i in range(1,N+1):
	ANS/=i
print(ANS)

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

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

T = list(itertools.permutations(temp))

ans = 0
cnt = 0
for i in T:
    t = 0
    for j in range(1, len(i)):
        t += distance(i[j-1][0], i[j-1][1], i[j][0], i[j][1])
    cnt += 1
    ans += t
print(ans/cnt)

Solution (Python):
import itertools
import numpy as np

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

lst = []
for I in list(itertools.permutations(range(N))):
    kyori = 0
    for i in range(N-1):
        kyori = kyori + ((X[I[i]]-X[I[i+1]])**2 +
                         (Y[I[i]]-Y[I[i+1]])**2) ** 0.5
    lst.append(kyori)

print(np.average(lst))

Solution (Python):
N = int(input())
X = []
Y = []
D = 0
F = [1]
for i in range(N):
    F.append(F[i]*(i+1))
E = F[N-2]*(N-1)
E = E/F[N]
for i in range(N):
    x,y = map(int,input().split())
    X.append(x)
    Y.append(y)
for i in range(N):
    xi,yi = X[i],Y[i]
    for j in range(i,N):
        xj,yj = X[j],Y[j]
        d=((xj-xi)**2+(yj-yi)**2)**0.5
        D+= d
print(D*E*2)

Solution (Python):
import math
import functools
import itertools
import operator

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


@functools.lru_cache(maxsize=None)
def dis(i, j):
    start = points[i]
    end = points[j]
    x1, x2 = start[0], end[0]
    y1, y2 = start[1], end[1]
    return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)


def pairwise(iterable):
    a, b = itertools.tee(iterable)
    next(b, None)
    return zip(a, b)


def factorial(n):
    return functools.reduce(operator.mul, range(1, n + 1))


s = 0
for path in itertools.permutations(range(N)):
    d = sum(dis(p, q) for p, q in pairwise(path))
    s += d
print(s / factorial(N))

Solution (Python):
a=int(input())
P = [input().split() for i in range(a)]

import itertools
L=list(itertools.permutations(range(a)))

N=[0]
for l in L:
  A=0
  
  for i in range(a-1):
    n0 = int(l[i])
    n1 = int(l[i+1])
    A += (((int(P[n0][0]) - int(P[n1][0])) ** 2) + ((int(P[n0][1]) - int(P[n1][1])) ** 2)) ** 0.5
  N.append(A)

print(sum(N)/len(L))

Solution (Python):
import math

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

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

total = 0
for i in range(N):
    for j in range(i + 1, N):
        xi, yi = cities[i]
        xj, yj = cities[j]
        total += math.sqrt(pow(xi - xj, 2) + pow(yi - yj, 2))   
print(total * 2 / N)

Solution (Python):
import itertools
n=int(input())
size=1
for i in range(1,n+1):
  size*=i

x=[]
y=[]
dist=0
aved=0
seq=[i+1 for i in range(n)]

p=list(itertools.permutations(seq))

for i in range(n):
  a,b=map(int,input().split())
  x.append(a)
  y.append(b)
for i in range(size):
  for j in range(n-1):
    dist+=((x[p[i][j]-1]-x[p[i][j+1]-1])**2+(y[p[i][j]-1]-y[p[i][j+1]-1])**2)**0.5
aved=dist/size
print(aved)

Solution (Python):
import math

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

import itertools
seq=list(range(n))
wa=0

for p in itertools.permutations(seq,n):
  for i in range(n-1):
    x=(town[p[i]][0]-town[p[i+1]][0])**2
    y=(town[p[i]][1]-town[p[i+1]][1])**2
    temp=(x+y)**(1/2)
    wa+=temp
print(wa/math.factorial(n))

Solution (Python):
import itertools 
n = int(input())
perms = list(itertools.permutations(list(range(n))))
xy = []
for i in range(n):
    a, b = map(int, input().split())
    xy.append([a, b])
ans = 0
for perm in perms:
    length = len(perm)
    dist = 0;
    for i in range(length-1):
        ind = perm[i]
        ind1 = perm[i+1]
        dist += ((xy[ind][0]-xy[ind1][0])**2 + (xy[ind][1]-xy[ind1][1])**2)**(1/2)
    ans += dist
print(ans/len(perms))

Solution (Python):


import math
N = int(input())
li = []
for i in range(N):
    li.append(input().split())
    
def distance(x1,y1,x2,y2):
    return math.sqrt((float(x1)-float(x2))**2+(float(y1)-float(y2))**2)

import itertools
li1 = list(itertools.permutations(li))
Sum_Sum = 0
for i in range(len(li1)):
    Sum = 0
    for j in range(len(li1[i])-1):
        Sum += distance(li1[i][j][0],li1[i][j][1],li1[i][j+1][0],li1[i][j+1][1])
    Sum_Sum += Sum
print("{:.10f}".format(Sum_Sum/len(li1)))

Solution (Python):
import math
ret=0
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
for i in range(n):
  for j in range(n):
    x,y=l[i]
    z,q=l[j]
    ret+=math.sqrt((z-x)**2+(y-q)**2)
print(ret/n)

Solution (Python):
import math
n=int(input())
loc=[]
for i in range(n):
    loc.append(list(map(int,input().split())))

d_sum=0
for i in range(n-1):
    for j in range(i+1,n):
        x=(loc[j][0]-loc[i][0])**2
        y=(loc[j][1]-loc[i][1])**2
        dij=math.sqrt(x + y)
        d_sum+=dij
print(2*d_sum/n)

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

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

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

ans = sum / n
print(ans)

Solution (Python):
import math
def fact(x):
    if x == 0 or x == 1:
        return 1

    ans = 1
    for i in range(1,x+1):
        ans *= i
    return ans
def combi(n,r):
    return fact(n)//((fact(r))*(fact(n-r)))


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

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

print(D*fact(N-1)/( fact(N-2)*(combi(N,2)) ))

Solution (Python):
import math

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

N = int(input().strip())
p = []
for i in range(N):
    p.append(list(map(int, input().strip().split(" "))))

NALL = 1
for i in range(1,N+1):
    NALL *= i

NPATH = 1
for i in range(1,N):
    NPATH *= i
NPATH *= 2

dst = 0.0
for i in range(N-1):
    for j in range(i+1,N):
        dst += dist(p[i], p[j])
print(dst * NPATH / NALL)

Solution (Python):
import itertools

N = int(input())
p = list(list(map(int,input().split()))for _ in range(N))

orders = list(itertools.permutations(range(N),N))
dist = 0
for i in range(N-1):
    for j in range(i+1,N):
        dist += ((p[i][0]-p[j][0])**2+(p[i][1]-p[j][1])**2)**0.5
ans = dist*2/N
print(ans)

Solution (Python):


import math
import itertools
def main():
    N = int(input())
    town_zahyou = [ [int(s) for s in input().split()] for i in range(N)]

    sum_route = 0

    for junban_list in list(itertools.permutations(town_zahyou, N)):
        route = 0
        for i, town in enumerate(junban_list):
            if i == N-1:
                continue
            from_town = town
            to_town = junban_list[i+1]
            route += math.sqrt((from_town[0] - to_town[0])**2 + (from_town[1] - to_town[1])**2)
        sum_route += route
    
    print(sum_route / math.factorial(N))



if __name__ == "__main__":
    main()

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

m=0

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

print(m*2/n)

Solution (Python):
n = int(input())
x = [0]*n
y = [0]*n
for i in range(0,n):
    x[i], y[i] = input().split()
    x[i] = int(x[i])
    y[i] = int(y[i])
dist = 0
for i in range(0,n):
    for j in range(0,i):
        dist += ((x[i]-x[j])**2 + (y[i]-y[j])**2)**0.5

print(dist * 2 /n)

Solution (Python):
import itertools
import math

n = int(input())
xy = [list(map(int, input().split())) for _ in range(n)]
seq = []
for i in range(n):
    seq.append(i)
b = itertools.permutations(seq)
total = 0

for a in b:
    for i in range(1, n):
        total += math.sqrt((xy[a[i]][0] - xy[a[i-1]][0]) ** 2 +
                           (xy[a[i]][1] - xy[a[i-1]][1]) ** 2)
print(total/math.factorial(n))

Solution (Python):


import sys
import itertools

sys.setrecursionlimit(10**7)
n=int(input())
xy=[]
for i in range(n):
    a,b=map(int,input().split())
    xy.append((a,b))

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

avg=0
for e in p:
    for i in range(n-1):
        avg+=pow((xy[e[i]][0]-xy[e[i+1]][0])**2+(xy[e[i]][1]-xy[e[i+1]][1])**2,0.5)
print(avg/len(p))

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

Solution (Python):
import math
import numpy as np


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

diff = np.expand_dims(t, axis=1) - np.expand_dims(t, axis=0)
dis = np.sqrt(np.sum(diff ** 2, axis=-1))


print(dis.sum()/N)

Solution (Python):
from itertools import permutations

import numpy as np

N = int(input())

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

combs = np.array(list(permutations(xys, N)))
ret = np.array(
    [np.linalg.norm(combs[:, i] - combs[:, i + 1], axis=1) for i in range(N - 1)]
).sum() / len(combs)

print("{:.10f}".format(ret))

Solution (Python):
import numpy as np
import itertools

N=int(input())

X=[]
Y=[]
for i in range(N):
    XY=input().split(" ")
    X.append(float(XY[0]))
    Y.append(float(XY[1]))

def get_dist(x1,y1,x2,y2):
    return np.sqrt(pow(x2-x1,2)+pow(y2-y1,2))

seq=range(1,N+1)
p=list(itertools.permutations(seq))

totals=[]
for i in range(len(p)):
    route=list(p[i])
    S=0
    for k in range(len(route)-1):
        S+=get_dist(X[route[k]-1],Y[route[k]-1],X[route[k+1]-1],Y[route[k+1]-1])
    totals.append(S)

print(np.average(totals))

Solution (Python):
import math
import itertools
import numpy as np
N=int(input())
XY=[list(map(int, input().split())) for _ in range(N)]
d=np.zeros((N,N))
for i in range(N-1):
    for j in range(i+1,N):
        dd=math.sqrt((XY[i][0]-XY[j][0])**2+(XY[i][1]-XY[j][1])**2)
        d[i,j]=dd
        d[j,i]=dd
ans=[]
path=list(itertools.permutations(range(N)))
for p in path:
    s=0
    for i in range(N-1):
        s+=d[p[i],p[i+1]]
    ans.append(s)
print(sum(ans)/len(ans))

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

sub_ls = []
for i in range(n):
    for j in range(i+1,n):
        a = (ls[i][0] - ls[j][0], ls[i][1] - ls[j][1])
        sub_ls.append(a)


sub = 0

for i in range(len(sub_ls)):
    sub+=(sub_ls[i][0]**2 + sub_ls[i][1]**2)**0.5
print(sub/len(sub_ls)*(n-1))

Solution (Python):
import itertools
import math

N = int(input())
root = [list(map(int, input().split())) for _ in range(N)]
ans = 0
for l in list(itertools.permutations(root)):
    total = 0
    for i in range(len(l) - 1):
        total += math.sqrt(((l[i][0] - l[i + 1][0]) ** 2) + ((l[i][1] - l[i + 1][1]) ** 2))
    ans += total
print(ans / math.factorial(N))

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

Solution (Python):
import math

N = int(input())
P = []
for i in range(N):
    P.append([int(a) for a in input().split()])

count = 0

for i in range(N):
    for j in range(N):
        if i < j:
            count += math.sqrt((P[i][0] - P[j][0]) ** 2 + (P[i][1] - P[j][1]) ** 2)

print(2 * count / N)

Solution (Python):
import numpy as np
n = int(input())
x = []
y = []
for i in range(n):
  xy = input().split(" ")
  x.append(int(xy[0]))
  y.append(int(xy[1]))
sum_length = 0
for i in range(n-1):
  for j in range(i+1,n):
    sum_length += np.sqrt((x[i]-x[j])**2+(y[i]-y[j])**2)
print(sum_length*2/n)

Solution (Python):

import itertools
import math
N = int(input())
l = []
for i in range(N):
    a,b = map(int,input().split())
    l.append([a,b])


t = list(itertools.permutations(range(N)))


def cal(T):
    res = 0
    
    for i,j in zip(T,T[1:]):
        res += math.sqrt(((l[i][0]-l[j][0])**2)+(l[i][1]-l[j][1])**2)
        
    return res
ans = 0
s = 0
for A in t:
    ans += cal(A)
    s+=1
print(ans/s)

Solution (Python):

import itertools
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
n = ni()
L = [[] for i in range(n)]
D = []
for i in range(n):
    x,y = nm()
    L[i].append(x)
    L[i].append(y)

L = list(itertools.permutations(L))

for l in L:
    d = 0.0
    for i in range(len(l)-1):
        d += float(((l[i][0]-l[i+1][0])**2 + (l[i][1]-l[i+1][1])**2)**0.5)
    D.append(d)

ans = float(sum(D)/len(D))
print(ans)

Solution (Python):
import math
N = int(input())
al = []
for _ in range(N):
    al.append(list(map(int,input().split())))
comb = 1
for i in range(1,N+1):
    comb *= i
combnum = comb//((N*(N-1))/2)
sumlen = 0
for i in range(N):
    for j in range(i,N):
        sumlen += math.sqrt((al[i][0]-al[j][0])**2+(al[i][1]-al[j][1])**2)*(combnum/N*2)

print(sumlen/combnum)