Problem:


Score : 300 points

Problem Statement

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

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

Constraints

2 \leq N \leq 8

-1000 \leq x_i \leq 1000

-1000 \leq y_i \leq 1000

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

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

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

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

Sample Input 1

3
0 0
1 0
0 1

Sample Output 1

2.2761423749

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

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

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

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

Sample Input 2

2
-879 981
-866 890

Sample Output 2

91.9238815543

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

Sample Input 3

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

Sample Output 3

7641.9817824387



Sample input:
3
0 0
1 0
0 1


Sample output:
2.2761423749

Submission status: Accepted

Solution code:
import math
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# 2020/05/17
# AtCoder Beginner Contest 145 - C

# Input
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)

# Output
print(ans)

Submission status: Accepted

Solution code:
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)))))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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
#print(AB)
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
N = int(input())
l = [list(map(int,input().split())) for _ in range(N)]
import itertools
p = list(itertools.permutations(range(N),N))
#print(p)
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
    #print(num)
    ans += num
print(ans/len(p))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
import itertools
import math 
n = int(input())
# 順列
# permutations(list, n) で list から n 個選んで並べる
loc = [list(map(int, input().split())) for i in range(n)]
#print(loc)
root = list(itertools.permutations(list(range(1, n+1)), n))
#print(root)
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))
    #print(temp)
  sum += temp

print(sum/len(root))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

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

# print(town)
# print(town[1][0])

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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
import itertools
n=int(input())
x=[list(map(int,input().split())) for i in range(n)]
a=[]
for i in range(n):
    a.append(i)

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

aa=1
for j in range(1,n+1):
  aa*=j

len=0
for j in range(aa):
  for i in range(n-1):
    len+=((x[p[j][i]][0]-x[p[j][i+1]][0])**2+(x[p[j][i]][1]-x[p[j][i+1]][1])**2)**(1/2)
b=len/aa
print(b)

Submission status: Accepted

Solution code:
import itertools
import math

n = int(input())

xy = [map(int, input().split()) for _ in range(n)]
x, y = [list(i) for i in zip(*xy)]
sum = 0
def distance(a, b):
  return ((x[a]-x[b])**2+(y[a]-y[b])**2)**(1/2)


permutations_lis = itertools.permutations(range(n))# 全ての場合のリストを生成
# 以下出力

for one_case in permutations_lis: 
  for i in range(n-1):
    sum += distance(one_case[i], one_case[i+1])
    
print(sum/math.factorial(n))

Submission status: Accepted

Solution code:
import itertools

def calculate_distance(x1, x2):
    l2_norm = (x1[0] - x2[0])**2 + (x1[1] - x2[1])**2
    return l2_norm**(1/2)

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

distance = 0
M = 0
for a in itertools.permutations(range(N)):
  M += 1
  
  for i in range(N-1):
        start, end = a[i], a[i+1]
        distance += calculate_distance(point[start], point[end])
print(distance/M)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import combinations
from math import sqrt

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

nC2 = list(combinations(xy, 2))
dist = []
d_append = dist.append
for a, b in nC2:
    d_append(sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2))

if len(dist) == 1:
    print(dist[0])
else:
    dist_sum = 0
    dist_case = list(combinations(dist, N - 1))
    for d in dist_case:
        dist_sum += sum(d)

    print(dist_sum / len(dist_case))

Submission status: Accepted

Solution code:
from itertools import permutations
from numpy import sqrt
def dist(x,y):
    return sqrt((x[0]-y[0])**2 + (x[1]-y[1])**2)

N = int(input())

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

ans = 0
l = 0
for way in permutations(range(0,N)):
    l += 1
    for i in range(N-1):
        ans += dist(d[way[i]],d[way[i+1]])
    
print(ans/l)

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 145 C - Average Length

import sys
import numpy as np
import itertools

N = int(input())
town = []
for _ in range(N):
    x, y = map(int, sys.stdin.readline().strip().split())
    town.append((x, y))


def distance(i, j):
    ans = np.sqrt((i[0] - j[0]) ** 2 + (i[1] - j[1]) ** 2)
    return ans


total = 0
for i in itertools.permutations(iterable=town):
    for j in range(len(i) - 1):
        total += distance(i[j], i[j + 1])

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

ans = total / n_fact

print(ans)

Submission status: Accepted

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

def dis(li_i, li_y):
    return ((li_i[0]-li_y[0])**2+(li_i[1]-li_y[1])**2)**(1/2)

ans=[]
for p in itertools.permutations([i for i in range(n)]):
    total_dis=0
    for v in range(n-1):
        total_dis+=dis(li[p[v]], li[p[v+1]])
    ans.append(total_dis)
print(sum(ans)/len(ans))