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:
def main():
  import sys
  input = sys.stdin.readline
  n=int(input())
  xy=[list(map(int,input().split())) for _ in [0]*n]
  ans=0
  for i in range(n):
    xi,yi=xy[i][0],xy[i][1]
    for j in range(n):
      xj,yj=xy[j][0],xy[j][1]
      ans+=pow(pow(xi-xj,2)+pow(yi-yj,2),0.5)
  print(ans/n)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  import math 
  import itertools 
  n=int(input())
  xy=[list(map(int,input().split())) for _ in [0]*n]
  l=''.join([str(i) for i in range(n)])
  sum=0
  for i in itertools.permutations(l):
    i=''.join(list(i))
    for j in range(n-1):
      a,b=int(i[j+1]),int(i[j])
      sum+=pow(pow(xy[a][0]-xy[b][0],2)+pow(xy[a][1]-xy[b][1],2),0.5)
  m=math.factorial(n)
  print(sum/m)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools

N = int(input())
xy = [list(map(int, input().split())) for _ in range(N)]
l = [i for i in range(N)]
pl = itertools.permutations(l)
d = 0
cnt = 0

for one_case in pl:
  cnt += 1
  for i in range(N - 1):
    a = one_case[i]
    b = one_case[i + 1]
    dx = (xy[a][0] - xy[b][0])**2
    dy = (xy[a][1] - xy[b][1])**2
    d += (dx + dy)**0.5
    
print(d / cnt)

Submission status: Accepted

Solution code:
import sys

import numpy as np
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
"""dd = [(-1, 0), (0, 1), (1, 0), (0, -1)];
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)];
ddn9 = ddn + [(0, 0)]
for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""

def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]  # WideIntPoint
def ws(): return sys.stdin.readline().split()
def si(): return int(sys.stdin.readline())  # SingleInt
def ss(): return input()
def hi(n): return [si() for _ in range(n)]
def hs(n): return [ss() for _ in range(n)]  # HeightString
def s_list(): return list(input())
def mi(n): return [wi() for _ in range(n)]  # MatrixInt
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]  # NumberGrid
def grid(n): return [s_list() for _ in range(n)]


def main():

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

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

    p = list(itertools.permutations(number))
    d = 0.0
    for p_1 in p:
        for i in range(n-1):
            p_1 = list(p_1)

            d += np.sqrt((xy[p_1[i]][0] - xy[p_1[i+1]][0])**2 + (xy[p_1[i]][1] - xy[p_1[i+1]][1])**2)
    ans = d/len(p)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
XY=[list(map(int,input().split())) for _ in range(n)]
ans=0
N=0
tmp=0
from itertools import permutations
from math import hypot 
for i in permutations(XY):
  N+=1
  for (x,y),(x1,y1) in zip(i,i[1:]):
    tmp+=hypot(x-x1,y-y1)

print(tmp/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))
dist=[]
for ll in list(permutations(l)):
    d=0.0
    for i in range(len(ll)-1):
        d+=((ll[i][0]-ll[i+1][0])**2+(ll[i][1]-ll[i+1][1])**2)**0.5
    dist.append(d)
print(sum(dist)/len(dist))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
x = []
y = []
for i in range(N) :
    p, q = map(int,input().split())
    x.append(p)
    y.append(q)

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

ans = ans / N
print(ans)

Submission status: Accepted

Solution code:
from math import factorial

n = int(input())

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

def dist(a, b):
    return ((pos[a][0] - pos[b][0]) ** 2 + (pos[a][1] - pos[b][1]) ** 2) ** (1/2)

ans = 0
def calc(status, total, prev):
    global ans
    if status == (1<<n) - 1:
        ans += total
        return
    for i in range(n):
        if status>>i&1:
            continue
        else:
            calc(status|(1<<i), total + dist(prev, i), i)

for i in range(n):
    calc(1<<i, 0, i)

print('{:.6f}'.format(ans/factorial(n)))

Submission status: Accepted

Solution code:
n = int(input())

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

dist = [[] for i in range(n)]
s = 0
for i, (x, y) in enumerate(c):
    for x0, y0 in c:
        s += ((x0-x)**2 + (y0-y)**2)**0.5



print(s / n)

Submission status: Accepted

Solution code:
n = int(input())
length = 0
x = []
y = []
for i in range(n):
    xi,yi = map(int,input().split())
    x.append(xi)
    y.append(yi)
for i in range(n):
    for k in range(n):
        length += ((x[i]-x[k])**2+(y[i]-y[k])**2)**0.5
print(length/n)

Submission status: Accepted

Solution code:
from itertools import combinations

n = int(input())

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


def length(list1, list2):
    return ((list1[0] - list2[0])**2 + (list1[1] - list2[1])**2) ** 0.5

ans = 0

for a, b in combinations(places, 2):
    # print(length(a, b))
    ans += length(a, b)

print(ans * 2 / n)

Submission status: Accepted

Solution code:
import itertools
import math 

N = int(input())
coordList = []
permList = []
distList = []
distance = 0

for i in range(N):
  x = input().split()
  coordList.append(x)

permList = list(itertools.permutations(coordList))

for perm in range(len(permList)):
  xList = []
  yList = []
  #print('Reset values!')
  for coord in range(len(permList[perm])):
    for num in range(len(permList[perm][coord])):
      if len(xList) != N and num + 1 < 2:
        xList.append(int(permList[perm][coord][num]))
        #print(xList)
      if len(yList) != N and num + 1 < 2:
        yList.append(int(permList[perm][coord][num + 1]))
        #print(yList)
      elif len(xList) and len(yList) == N: 
        for i in range(N):
          if i + 1 < N: 
            dist = math.sqrt(((xList[i] - xList[i+1]) ** 2) + ((yList[i] - yList[i+1]) ** 2))
            distList.append(dist)

for i in range(len(distList)):
  distance += distList[i]

print(distance / math.factorial(N))

Submission status: Accepted

Solution code:
n=int(input())
xy=[list(map(int,input().split())) for _ in range(n)]
import itertools as it
ans=0
for i in it.permutations(range(n),n):
    x=xy[i[0]][0]
    y=xy[i[0]][1]
    for k in range(1,n):
        j=i[k]
        ans+=((x-xy[j][0])**2+(y-xy[j][1])**2)**0.5
        x=xy[j][0]
        y=xy[j][1]
N=1
for i in range(1,n+1):
    N*=i
print(ans/N)

Submission status: Accepted

Solution code:
from itertools import permutations
from math import sqrt

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

fact = [1]
for i in range(1, n + 1):
    fact.append(fact[-1] * i)

sm = 0
for pat in permutations(range(n), n):
    for e1, e2 in zip(pat, pat[1:]):
        x1, y1 = xy[e1]
        x2, y2 = xy[e2]
        dist = sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
        sm += dist

ans = sm / fact[n]
print(ans)

Submission status: Accepted

Solution code:
import itertools
import math
n=int(input())
data=[]
a=[]
for i in range(n):
    x,y=map(int,input().split())
    data.append([x,y])
    a.append(i)
d=0.0
for v in itertools.permutations(a):
    for i in range(n-1):
        d0=((data[v[i]][0]-data[v[i+1]][0])**2+(data[v[i]][1]-data[v[i+1]][1])**2)**0.5
        d+=d0
print(d/math.factorial(n))

Submission status: Accepted

Solution code:
import math
import itertools

N = int(input())

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

perm_C = itertools.permutations(Cities)

total = 0
count = 0
for route in perm_C:
  count += 1
  current = route[0]
  distance_sum = 0
  for city in route[1:]:
    distance = math.sqrt(pow(current[0]-city[0], 2) + pow(current[1]-city[1], 2))
    distance_sum += distance
  total += distance_sum

avg = total / count
print("{:.7f}".format(avg))

Submission status: Accepted

Solution code:
# coding: utf-8
import math

n = int(input().rstrip())


class Pos:
    def __init__(self, init: list):
        self.x = init[0]
        self.y = init[1]

    def get_dist(self, target):
        return math.sqrt(pow(target.x - self.x, 2) + pow(target.y - self.y, 2))


mati_list = [Pos([int(i) for i in input().rstrip().split(" ")]) for j in range(n)]


def get_kumiawase(prev: int, flag: list):
    if len([i for i in flag if i == False]) == 0:
        return [0]
    ans = []
    for i in range(n):
        if flag[i]:
            continue
        diff = mati_list[prev].get_dist(mati_list[i])
        flag[i] = True
        ret = get_kumiawase(i, flag)
        flag[i] = False
        for j in ret:
            ans.append(j + diff)
    return ans


kumiawase = []
for i in range(n):
    ret = get_kumiawase(i, [j == i for j in range(n)])
    kumiawase += ret

print(sum(kumiawase) / len(kumiawase))

Submission status: Accepted

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

def permutations(nums, path, p):
    if len(nums) == 0:
        p.append(path)
        return
    for i in range(len(nums)):
        permutations(nums[:i] + nums[i + 1:], path + [nums[i]], p)

p = []
permutations([i for i in range(n)], [], p)

average = 0
for way in p:
    length = 0
    for i in range(len(way) - 1):
        start = way[i]
        end = way[i + 1]
        length += ((xy[start][0] - xy[end][0]) ** 2 + (xy[start][1] - xy[end][1]) ** 2) ** 0.5
    average += length / len(p)
print(average)

Submission status: Accepted

Solution code:
def dis(a, b):
    return np.sqrt((a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]))
import itertools
import numpy as np
n = int(input())
coor = []
for _ in range(n):
    x, y = map(int, input().split())
    coor.append([x,y])

permutations_lis = itertools.permutations(range(n))# 全ての場合のリストを生成
dis_list = []
for case in permutations_lis:
    dis_sum = 0
    for i in range(n-1):
        dis_sum += dis(coor[case[i]], coor[case[i+1]])
    dis_list.append(dis_sum)

print(np.mean(dis_list))

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
xy=np.array([list(map(int,input().split())) for i in range(N)])
print(sum(np.linalg.norm(i-j) for i in xy for j in xy)/N)

Submission status: Accepted

Solution code:
N=int(input())
xy=[list(map(int,input().split())) for i in range(N)]
distance=0
for xi,yi in xy:
    for xj,yj in xy:
        distance+=((xi-xj)**2+(yi-yj)**2)**.5
print(distance/N)

Submission status: Accepted

Solution code:
import itertools
import math

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

for route in itertools.permutations(range(n)):
    prev = coordinate[route[0]]
    for i in range(1, n):
        ans += math.sqrt((prev[0] - coordinate[route[i]][0])**2 + (prev[1] - coordinate[route[i]][1])**2)
        prev = coordinate[route[i]]

print(ans / math.factorial(n))

Submission status: Accepted

Solution code:
import math
from itertools import permutations
n = int(input())
xy = [[int(i) for i in input().split()] for _ in range(n)]
arr = list(permutations(range(n), n))
ans = 0
for i in range(len(arr)):
    for j in range(1, n):
        ans += math.sqrt(pow(xy[arr[i][j]][0] - xy[arr[i][j-1]][0], 2) + pow(xy[arr[i][j]][1] - xy[arr[i][j-1]][1], 2))
ans /= len(arr)
print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    #input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations
    #from itertools import accumulate, product
    from math import floor, ceil
    #from operator import itemgetter

    #mod = 1000000007

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
'''
https://atcoder.jp/contests/abc145/tasks/abc145_c
'''
def main():
    import sys
    #input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations
    #from itertools import accumulate, product
    from math import floor, ceil
    #from operator import itemgetter

    #mod = 1000000007

    N = int(input())
    grid = [list(map(int, input().split())) for _ in range(N)]
    res = 0
    for path in permutations(grid, N):
        for i in range(N-1):
            res += ((path[i][0]-path[i+1][0])**2 + (path[i][1]-path[i+1][1])**2)**0.5
    a = 1
    for i in range(2, N+1):
        a *= i
    print(res/a)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
import math

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

nums = [_ for _ in range(n)]
total = 0
for v in itertools.permutations(nums, n):
    dist = 0
    for i in range(n-1):
        dist += math.sqrt((dots[v[i]][0] - dots[v[i+1]][0]) **
                          2 + (dots[v[i]][1] - dots[v[i+1]][1])**2)
    total += dist

print(total/math.factorial(n))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys,math,itertools
import numpy as np

n=int(sys.stdin.readline())
a=np.array([sys.stdin.readline().split() for _ in range(n)],dtype='int64')

to=0
num=0
b=np.zeros((n,n))

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


for i in itertools.permutations(range(n)):
    f=0
    num+=1
    for j in i:
        if f==0:
            c=j
            f=1
        else:
            if c>j:
                to+=b[j,c]
            else:
                to+=b[c,j]
            c=j


print(to/num)

Submission status: Accepted

Solution code:
from itertools import combinations
import math

n = int(input())

towns = []

for _ in range(n):
    array = list(map(int, input().split()))
    towns.append(array)


distances = [[0 for _ in range(n)] for _ in range(n)]

town_num = list(range(n))

for i, j in combinations(town_num, 2):
    distance = ((towns[j][0] - towns[i][0])**2 + (towns[j][1] - towns[i][1])**2)**(0.5)
    distances[i][j] = distance
    distances[j][i] = distance

total_way = math.factorial(n)

total = 0

for i in range(n):
    for j in range(n):
        total += distances[i][j] * math.factorial(n-1)

print(total/total_way)

Submission status: Accepted

Solution code:
from itertools import combinations as comb
from math import sqrt
n = int(input())
xy = [list(map(int, input().split())) for _ in range(n)]


s = sum(sqrt((xy[i][0] - xy[j][0])**2 + (xy[i][1] - xy[j][1])**2) for i,j in comb(range(n), 2))
print(s * 2 / n)

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())
lx, ly = [], []
for _ in range(N):
    xi, yi = list(map(int, input().split()))
    lx.append(xi)
    ly.append(yi)

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

mcost = 0
for x in permutations(list(range(N))):
    cost = 0.0
    for i in range(len(x) - 1):
        dij = ((lx[x[i]] - lx[x[i + 1]]) ** 2 + (ly[x[i]] - ly[x[i + 1]]) ** 2) ** 0.5
        cost += dij
    mcost += cost / fN
print(mcost)

Submission status: Accepted

Solution code:
import itertools
import math

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

ans = 0
for p in itertools.permutations(list(range(n))):
    for i in range(1, n):
        s, t = p[i-1], p[i]
        ans += ((x[s]-x[t])**2 + (y[s]-y[t])**2)**0.5

print(ans/math.factorial(n))

Submission status: Accepted

Solution code:
# #!/usr/bin/env python3
# # -*- coding: utf-8 -*-
import itertools


def main():
    N = int(input())
    XY = [list(map(int, input().split())) for _ in range(N)]
    roots = list(itertools.permutations(XY))

    cnt, result = len(roots), 0
    
    for root in roots:
        for i in range(len(root) - 1):
            result += ((root[i][0] - root[i + 1][0])**2 +
                       (root[i][1] - root[i + 1][1])**2)**0.5
    print(result / cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
l=[]
for i in range(n):
        tmp = list(map(int,input().split()))
        l.append(tmp)
lis = range(n)
import itertools
import math
cities = list(itertools.permutations(lis))
ans=0
for j in range(len(cities)):
        vils = cities[j]
        for i in range(1,n):
                last= l[vils[i-1]]
                now = l[vils[i]]
                dist = (last[0] - now[0])**2 + (last[1] - now[1])**2
                dist = math.sqrt(dist)
                ans+=dist
print(ans/len(cities))

Submission status: Accepted

Solution code:
import math
N=int(input())
point=[list(map(int,input().split())) for _ in range(N)]
l=[i+1 for i in range(N)]
dist1=0
dist=[]
for i in range(0,N,1):
    for j in range(i+1,N,1):
        dist1=dist1+math.sqrt(pow(point[i][0]-point[j][0],2)+pow(point[i][1]-point[j][1],2))
print(dist1*2*(N-1)/(N*(N-1)))

Submission status: Accepted

Solution code:
import itertools
import math

N = int(input())
coodinate_list = [tuple(map(int, input().split(' '))) for _ in range(N)]
idx_list = [x for x in range(N)]
idx_permutations = itertools.permutations(idx_list)

total_dist = 0
permutaions_len = 0
for one_case in idx_permutations:
    permutaions_len += 1
    dist = 0
    for idx in range(len(one_case)):
        if idx > 0:
            dist += math.sqrt(math.pow(coodinate_list[one_case[idx]][0]-coodinate_list[one_case[idx-1]][0], 2)+math.pow(coodinate_list[one_case[idx]][1]-coodinate_list[one_case[idx-1]][1], 2))
    total_dist += dist
print(total_dist / permutaions_len)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n=ii()
arr=llint(n)
ans=0
cnt=0

for i in permutations(range(n)):
    cnt+=1
    for j in range(n-1):
        ans+=((arr[i[j]][0]-arr[i[j+1]][0])**2 + (arr[i[j]][1]-arr[i[j+1]][1])**2)**0.5

print(ans/cnt)

Submission status: Accepted

Solution code:
import itertools
import math

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

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

a = [list(e) for e in zip(x, y)]

c = list(itertools.permutations([i for i in range(n)]))

ans = [0]*len(c)

for i in range(len(c)):
    x_f = x[c[i][0]]
    y_f = y[c[i][0]]
    for j in range(1, n):
        x_s = x[c[i][j]]
        y_s = y[c[i][j]]

        ans[i] += math.sqrt((x_f-x_s)**2 + (y_f-y_s)**2)

        x_f = x[c[i][j]]
        y_f = y[c[i][j]]

print(sum(ans)/len(c))

Submission status: Accepted

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

from itertools import permutations
import numpy as np

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

    odrs = permutations(list(range(n)), n)
    dists = [getdist(list(o), x, y) for o in odrs]
    print(np.mean(dists))

def getdist(odr, x, y):
    xo = np.array(x)[odr]
    yo = np.array(y)[odr]
    dx = xo[1:] - xo[:-1]
    dy = yo[1:] - yo[:-1]
    return np.sum((dx ** 2 + dy ** 2) ** 0.5)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import permutations
from math import sqrt
n=int(input())
p_l=list(permutations(range(n)))
#print(p_l)
x=[]
for _ in range(n):
  x.append(list(map(int,input().split())))
n_fact=1
for i in range(n):
  n_fact*=(i+1)
ans=0
def dist(a,b):
  return sqrt((a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]))
for i in p_l:
  su=0
 # print(i)
  for j in range(1,len(i)):
    su+=dist(x[i[j-1]],x[i[j]])
#  print(su)
  ans+=su
print(ans/n_fact)

Submission status: Accepted

Solution code:
import math
from itertools import permutations
n = int(input())
x, y = [0] * n, [0]*n
for i in range(n):
    x[i], y[i] = map(int, input().split())
    
dist = [[0]*n for _ in range(n)]
for i in range(n):
    for t in range(n):
        dist[i][t] = math.sqrt((x[i]-x[t])**2 + (y[i]-y[t])**2)
        
ans = 0
for p in permutations(range(n)):
    for i in range(len(p)-1):
        ans += dist[p[i]][p[i+1]]

print(ans / math.factorial(n))

Submission status: Accepted

Solution code:
import itertools

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

res = 0
L = list(itertools.permutations(v))
for l in L:
    for i in range(1,len(l)):
        res += (abs(l[i][0]-l[i-1][0])**2 + abs(l[i][1]-l[i-1][1])**2)**.5
print(res/len(L))

Submission status: Accepted

Solution code:
from math import sqrt
from itertools import permutations

def dict(p, q):
    return sqrt((p[0]-q[0])**2 + (p[1]-q[1])**2)

n = int(input())
coord = []
for _ in range(n):
    coord.append([int(i) for i in input().split()])

sum = cnt = 0
for c in permutations(coord):
    for i in range(n-1):
        sum += dict(c[i], c[i+1])
    cnt += 1

print(sum/cnt)

Submission status: Accepted

Solution code:
from itertools import permutations
N=int(input())
xy=[tuple(map(int,input().split(' '))) for i in range(N)]
ans = 0
cnt = 0
for i in permutations([i for i in range(N)]):
    cnt += 1
    for j in range(N-1):
        ans += ((xy[i[j+1]][0]-xy[i[j]][0])**2+(xy[i[j+1]][1]-xy[i[j]][1])**2)**0.5
print(ans/cnt)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
a = []
for i in range(N):
    a.append(list(map(int, input().split())))
leng = 0
for i in range(N):
    for j in range(i+1, N):
        leng += np.sqrt((a[i][0]-a[j][0])**2 + (a[i][1]-a[j][1])**2)
print(2*leng/N)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import itertools
import math

def dist(sx, sy, dx, dy):
    return math.sqrt((sx - dx)**2 + (sy - dy)**2)


def solve(N, xs, ys):
    ps = [p for p in itertools.permutations(range(N))]
    total = 0
    for p in ps:
        for i in range(1, N):
            total += dist(xs[p[i]], ys[p[i]], xs[p[i-1]], ys[p[i-1]])
    print(total * 1.0 / len(ps))


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))
    x = [int()] * (N)
    y = [int()] * (N)
    for i in range(N):
        x[i] = int(next(tokens))
        y[i] = int(next(tokens))
    solve(N, x, y)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

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

D = [[0 for i in range(n)] for j in range(n)]

for i in range(n):
    for j in range(n):
        if i == j:
            D[i][j] = 0
        else:
            D[i][j] = math.sqrt((town[i][0]-town[j][0])**2 + (town[i][1]-town[j][1])**2)

print(sum([sum(k) for k in D]) / n)

Submission status: Accepted

Solution code:
import itertools
import math
def d(a,b):
  return math.sqrt((a[0]-b[0])**2+(a[1]-b[1])**2)

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

s = 0
fac = 1
for route in itertools.permutations([i for i in range(n)]):
  for i in range(len(route)-1):
    s += d(cities[route[i]],cities[route[i+1]])
print(s/math.factorial(n))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import bisect
import re
import queue
from collections import Counter
from collections import defaultdict
from functools import lru_cache


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def float():
        return float(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def map_float():
        return [float(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def PI():
        return math.acos(-1)

    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True

    @staticmethod
    def divisor(n):
        i = 1
        table = []
        while i * i <= n:
            if n % i == 0:
                table.append(i)
                table.append(n // i)
            i += 1
        table = list(set(table))
        return table


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)

    def list(self):
        return self.__q

    def length(self):
        return len(self.__q)


sys.setrecursionlimit(1000000)
MOD = int(1e09) + 7
INF = int(1e30)


def main():
    # sys.stdin = open("Sample.txt")
    n = Scanner.int()
    X = Scanner.int_list_list(n)
    ans = 0
    cnt = 0
    for v in itertools.permutations([i for i in range(n)]):
        len = 0
        for i in range(n - 1):
            a, b = v[i], v[i + 1]
            len += math.sqrt((X[a][0] - X[b][0]) ** 2 +
                             (X[a][1] - X[b][1]) ** 2)
        ans += len
        cnt += 1
    ans /= cnt
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math

n = int(input())
l = 0

x_list = []
y_list = []

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

for j in range(n):
    for k in range(n):
        if j>=k:
            continue
        l += math.sqrt((x_list[j] - x_list[k]) ** 2 + (y_list[j] - y_list[k]) ** 2)

print(l * 2/n)

Submission status: Accepted

Solution code:
import math
n = int(input())
l = 0

x_list = []
y_list = []

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

for j in range(n):
    for k in range(n):
        if j >= k:
            continue
        l += math.sqrt((x_list[j] - x_list[k]) ** 2 + (y_list[j] - y_list[k]) ** 2)

print(l * 2/n)

Submission status: Accepted

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

import itertools
import math

path = 0
for ptn in itertools.permutations(range(n)):
    for i, j in zip(ptn, ptn[1:]):
        path += ((xy[i][0] - xy[j][0]) ** 2 + (xy[i][1] - xy[j][1]) ** 2) ** 0.5
ans = path / (math.factorial(n))

print(ans)