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 itertools
import math

def resolve():
    n = int(input())
    fac = math.factorial(n)
    S = [list(map(int, input().split())) for _ in range(n)]

    ans = 0
    for p in itertools.permutations(range(n)):
        sum = 0
        for i in range(n-1):
            sum += math.sqrt((S[p[i]][0] - S[p[i+1]][0])**2 + (S[p[i]][1] - S[p[i+1]][1])**2)
        ans += sum

    print(ans/fac)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
import itertools
import math

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

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

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

ans=0
for v in itertools.permutations(town):
    # print(v)
    sum=0
    for i in range(len(v)-1):
        sum+=distance[v[i]][v[i+1]]
    ans+=(sum)
    # print(sum)
print(ans/(math.factorial(N)))

Submission status: Accepted

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

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

print(ans*2/n)

Submission status: Accepted

Solution code:
import math
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
ans=0.0
for i in range(0, n-1):
    for j in range(i, n):
        ans+=math.sqrt(pow(a[i][0]-a[j][0], 2)+pow(a[i][1]-a[j][1], 2))
print(ans*2/n)

Submission status: Accepted

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

ans=l/n
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import itertools
import math
n = int(input())
x = [0] * n
y = [0] * n
l = list(range(n))
ans = 0
for i in range(n):
    x[i], y[i] = map(int, input().split())
for v in itertools.permutations(l, 2):
    ans += math.sqrt((x[v[0]] - x[v[1]])**2 + (y[v[0]] - y[v[1]])**2)
print(ans/n)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import itertools
import math
n = int(input())
x = [0] * n
y = [0] * n
a = list(range(n))
ans = 0
for i in range(n):
    x[i], y[i] = map(int, input().split())
for v in itertools.permutations(a, 2):
    ans += math.sqrt((x[v[0]] - x[v[1]])**2 + (y[v[0]] - y[v[1]])**2)
print(ans/n)

Submission status: Accepted

Solution code:
import itertools
import math

def dist(xy1,xy2):
  x1,y1=xy1
  x2,y2=xy2
  return math.sqrt((x2-x1)**2+(y2-y1)**2)

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

for i in range(N):
  x,y=map(int,input().split())
  xylist.append((x,y))
  
#print(xylist)
allperm=list(itertools.permutations(range(N)))

allsum=0
for perm in allperm:
  for i in range(N-1):
    allsum+=dist(xylist[perm[i]],xylist[perm[i+1]])
    
print(allsum/len(allperm))

Submission status: Accepted

Solution code:
import math

def sort(n, pl):
    if n > 1:
        pl_list = []
        for i in range(n):
            tmp = []
            tmp.extend(pl)
            p = tmp.pop(i)
            for l in sort(n-1, tmp):
                l.insert(0, p)
                pl_list.append(l)
        return pl_list
    else:
        return [pl]

def average_len(n, pl):
    pl_list = sort(n, pl)
    ave = 0.0
    for pl in pl_list:
        for i in range(n-1):
            ave += math.sqrt((pl[i][0]-pl[i+1][0])**2+(pl[i][1]-pl[i+1][1])**2)
    ave /= len(pl_list)
    print(ave)

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

average_len(n, pl)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
grid = []
for i in range(n):
    array = list(map(int,input().split()))
    grid.append(array)
sum = 0
for i in range(n):
    for j in range(i+1,n):
        sum += np.sqrt((grid[i][0]-grid[j][0])**2+(grid[i][1]-grid[j][1])**2)
print(sum*(n-1)/n/(n-1)*2)

Submission status: Accepted

Solution code:
import math
n = int(input())
ten = [list(map(int,input().split())) for i in range(n)]
a = 0

for i, j in ten:
  for k, u in ten:
    a += math.sqrt((i - k)**2 + (j - u)**2)

print(a/n)

Submission status: Accepted

Solution code:
import itertools
import math
import numpy as np

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

ll=[]
for v in itertools.permutations(range(n)):
    ll.append(list(v))
a=0
for j in ll:
    for k in range(len(j)-1):
        x=j[k]
        y=j[k+1]
        dis=math.sqrt((l[x][0]-l[y][0])**2+(l[x][1]-l[y][1])**2)
        a+=dis

print(a/len(ll))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from itertools import permutations

def dist(P, Q):
    return ((P[0] - Q[0]) ** 2 + (P[1] - Q[1]) ** 2) ** 0.5

def main():
    N = int(input())
    points = tuple(tuple(map(int, input().split())) for _ in range(N))
    paths = list(permutations(list(range(N))))

    ans = 0
    for path in paths:
        for i in range(N-1):
            ans += dist(points[path[i]], points[path[i+1]])

    ans /= len(paths)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from itertools import permutations

def dist(P, Q):
    return ((P[0] - Q[0]) ** 2 + (P[1] - Q[1]) ** 2) ** 0.5

def main():
    N = int(input())
    points = tuple(tuple(map(int, input().split())) for _ in range(N))
    paths = list(permutations(list(range(N))))

    ans = 0
    for path in paths:
        for i in range(N-1):
            ans += dist(points[path[i]], points[path[i+1]])

    ans /= len(paths)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 14　Square869120Contest #4 B - Buildings are Colorful!
import itertools, math
n = int(input())
xy = [list(map(int, input().split())) for _ in range(n)]
ans = 0
for i in itertools.permutations(range(n)):
    for j in range(len(i)-1):
        ans += pow(pow(xy[i[j]][0]-xy[i[j+1]][0], 2) + pow(xy[i[j]][1]-xy[i[j+1]][1], 2), 0.5)
print(ans/math.factorial(n))

Submission status: Accepted

Solution code:
import math
n = int(input())
ten = [list(map(int,input().split())) for i in range(n)]
a = 0

for i, j in ten:
  for k, u in ten:
    a += math.sqrt((i - k)**2 + (j - u)**2)

print(a/n)

Submission status: Accepted

Solution code:
def dfs(coord, visited, route, sum):
    n = len(visited)

    allVisited = True
    for city in range(n):
        if visited[city] is False:
            allVisited = False
            break

    if allVisited is True:
        dist = 0

        for i in range(1, len(route)):
            prev = route[i - 1]
            cur = route[i]
            dist += (
                (coord[prev][0] - coord[cur][0]) ** 2
                + (coord[prev][1] - coord[cur][1]) ** 2
            ) ** 0.5
        sum += dist
        return sum

    for next_city in range(n):
        if visited[next_city] is False:
            visited[next_city] = True
            route.append(next_city)
            sum = dfs(coord, visited, route, sum)
            visited[next_city] = False
            route.pop(-1)

    return sum


n = int(input())
coord = []
route = []
visited = [False] * n

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


sum = 0
sum = dfs(coord, visited, route, sum)

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

print(sum / perm)

Submission status: Accepted

Solution code:
import itertools

n = int(input())
pos = tuple(tuple(int(i) for i in input().split()) for _ in range(n))

def distance(x, y):
    return ((x[0] - y[0])**2 + (x[1] - y[1])**2)**0.5

totals = []
for path in itertools.permutations(pos, n):
    d = 0
    for i in range(len(path)-1):
        d += distance(path[i], path[i+1])
    totals.append(d)

print(sum(totals) / len(totals))

Submission status: Accepted

Solution code:
import math
n = int(input())
ten = [list(map(int,input().split())) for i in range(n)]
av = 0

for i, j in ten:
  for k, u in ten:
    av += math.sqrt((i - k)**2 + (j - u)**2)

print(av/n)

Submission status: Accepted

Solution code:
import itertools

n = int(input())

l = []

for _ in range(n):

    l.append(list(map(int, input().split())))

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


distance = 0
for aa in aaaa:

    for i in range(len(aa)):
        if i != n-1:
            xi = (aa[i][0])
            yi = (aa[i][1])
            xj = (aa[i + 1][0])
            yj = (aa[i + 1][1])

            distance += ( (xi - xj) ** 2 + (yi - yj) ** 2 ) ** 0.5


pattern = len(aaaa)

ans = distance / pattern

print(ans)

Submission status: Accepted

Solution code:
import math
import copy

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

sum = 0
for i, xy in enumerate(a):
    xi, yi = xy
    aa = copy.deepcopy(a)
    aa.pop(i)
    for xj, yj in aa:
        sum += math.sqrt((xi - xj) ** 2 + (yi - yj) ** 2)

print(sum * math.factorial(n - 1) / math.factorial(n))

Submission status: Accepted

Solution code:
from itertools import permutations
from math import factorial


# メインプログラム
def func(N, lst):

    # N! を求める
    N_fact = factorial(N)

    # lst から座標のすべての組合せを求める。
    perm_grid_xy = permutations(lst)

    # すべての組合せを全探索して、総合距離を計算する
    total_dist = total_distance(N, perm_grid_xy)

    # 平均距離 = 総合距離 / N!
    average_dist = total_dist / N_fact

    return average_dist


def total_distance(N, perm_grid_xy):

    ret = 0
    
    for grid_xy in perm_grid_xy:
        for i in range(N-1):
            x0, y0 = grid_xy[i][0], grid_xy[i][1]
            x1, y1 = grid_xy[i+1][0], grid_xy[i+1][1]
            dist = ((x0 - x1)**2 + (y0 - y1)**2) ** 0.5
            ret += dist

    return ret


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

Submission status: Accepted

Solution code:
from itertools import permutations
from math import factorial


# メインプログラム
def func(N, lst):

    # N! を求める
    N_fact = factorial(N)

    # lst から座標のすべての組合せを求める。
    perm_grid_xy = permutations(lst)

    # すべての組合せを全探索して、総合距離を計算する
    total_dist = total_distance(N, perm_grid_xy)

    # 平均距離 = 総合距離 / N!
    average_dist = total_dist / N_fact

    return average_dist


def total_distance(N, perm_grid_xy):
    
    ret = 0
    
    for grid_xy in perm_grid_xy:
        tmp = sum(
            ((grid_xy[i][0] - grid_xy[i+1][0]) ** 2 
            + (grid_xy[i][1] - grid_xy[i+1][1]) ** 2) ** 0.5 
            for i in range(N-1)
            )
        ret += tmp

    return ret


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

Submission status: Accepted

Solution code:
import math
N = int(input())
city_list = [list(map(int, input().split())) for _ in range(N)]
average = 0
for x1, y1 in city_list:
    for x2, y2 in city_list:
        average += math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
print(average / N)

Submission status: Accepted

Solution code:
import itertools
import math
N = int(input())
city_list = []
for i in range(N):
    x, y = map(lambda z: int(z), input().split())
    city_list.append((x, y))
root_list = list(itertools.permutations(city_list, N))
ave = 0
for root in root_list:
    length = 0
    while len(root) > 1:
        x1, y1, x2, y2 = root[0][0], root[0][1], root[1][0], root[1][1]
        length += math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
        root = root[1:]
    ave += (length / len(root_list))
print(ave)

Submission status: Accepted

Solution code:
import itertools, math

def dist(way, n):
    dist = 0
    for i in range(n-1):
        x = (way[i][0] - way[i+1][0]) ** 2
        y = (way[i][1] - way[i+1][1]) ** 2
        dist += math.sqrt(x+y)
    return dist

n = int(input())
point = []

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

mean_dist = 0
cnt = 0
for v in itertools.permutations(point, n):
    mean_dist += dist(v, n)
    cnt += 1

print(mean_dist/cnt)

Submission status: Accepted

Solution code:
from math import sqrt
import itertools

#for i in itertools.permutations(['a', 'b', 'c']):
def distance(dx,dy):
  return sqrt(dx**2 + dy**2)

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

ans=0
count=0
for sxy in itertools.permutations(xy):
  #print(sxy)
  for i in range(1,N):
    x2,y2=sxy[i]
    x1,y1=sxy[i-1]
    #print(x1,y1,x2,y2)
    dx=x2-x1
    dy=y2-y1
    ans+=distance(dx,dy)
  count+=1
    
#print(count,ans)
print(ans/count)

Submission status: Accepted

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

ans=0
for x1, y1 in xy:
	for x2, y2 in xy:
		ans+=((x1-x2)**2+(y1-y2)**2)**0.5
print(ans/n)

'''
文字式にする、町の数をｎとするとき。

１経路に道 ( n-1 )　×　経路の数 n!　÷　道の種類 nC２　÷　経路の数 n!

= ( n-1 )　×　 n!　÷　 nC２　÷　n!
= ( n-1 )　÷　 nC２　　　　　　　　　
= ( n-1 )　÷　 ( n( n-1 ) ÷ 2・1 )
= 2 　÷　 n　　　　　　
'''

Submission status: Accepted

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

ans=0
for x1, y1 in xy:
	for x2, y2 in xy:
		ans+=((x1-x2)**2+(y1-y2)**2)**0.5
print(ans/n)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
import itertools
import math

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

def main():
    N = int(input())
    matrix = []
    for i in range(N):
        x, y = input_nums()
        matrix.append((x, y))
    total = 0
    count = 0
    for cmb in itertools.permutations(range(N), N):
        count += 1
        sumv = 0
        for i in range(len(cmb)-1):
            x1 = matrix[cmb[i]][0]
            y1 = matrix[cmb[i]][1]
            x2 = matrix[cmb[i+1]][0]
            y2 = matrix[cmb[i+1]][1]
            sumv += calc_distance(x1, y1, x2, y2)
        total += sumv
    print(total/count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
#input = sys.stdin.readline
input = sys.stdin.buffer.readline

# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
#import numpy as np

from itertools import permutations
import math


def main():
	n=int(input())
	li=[list(map(int,input().split())) for _ in range(n)]

	nsum=math.factorial(n)

	ans=0
	tmp=0

	for i in permutations(range(n),n):
		tmp=0
		for j in range(n-1):
			x0=li[i[j]][0]
			x1=li[i[j+1]][0]
			y0=li[i[j]][1]
			y1=li[i[j+1]][1]
			tmp+=((x0-x1)**2+(y0-y1)**2)**0.5
		ans+=tmp/nsum
	print(ans)


if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
from math import sqrt
n = int(input())
xy = [[int(i) for i in input().split()] for _ in range(n)]
def d(a, b):
    return sqrt((a[0] - b[0])** 2 + (a[1] - b[1])** 2)

s = 0
for i in range(n):
    for j in range(i + 1, n):
        s += d(xy[i], xy[j])
s /= n / 2
print(s)

Submission status: Accepted

Solution code:
import math
import itertools
def main():
    n = int(input())
    x = []
    y = []
    for i in range(n):
        xx,yy = map(int, input().split())
        x.append(xx)
        y.append(yy)
    ans = 0.0
    dist = [[0.0] * n for i in range(n)]
    for i in range(n):
        for j in range(i+1,n):
            dist[i][j] = cal_dist(x[i],y[i],x[j],y[j])
            dist[j][i] = dist[i][j]
    r = []
    for i in range(n):
        r.append(i)
    cnt = 0
    for perm in itertools.permutations(r,n):
        p = list(perm)
        cnt += 1
        for i in range(1,n):
            ans += dist[p[i-1]][p[i]]

    ans /= cnt
    print(ans)



def cal_dist(x1,y1,x2,y2):
    a = x1 -x2
    b = y1 - y2
    retval = a ** 2 + b ** 2
    return math.sqrt(retval)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import permutations
n=int(input())
xy=[]
dist=[]
for i in range(n):
    x,y=map(int,input().split())
    xy.append((x,y))

for l in list(permutations(xy)):
    d=0.0
    for i in range(len(l)-1):
        d+=((l[i+1][0]-l[i][0])**2+(l[i+1][1]-l[i][1])**2)**0.5
    dist.append(d)
print(sum(dist)/len(dist))

Submission status: Accepted

Solution code:
import itertools
import math

N = int(input())

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

pattern = itertools.permutations(range(1,N+1),  N)

ans = 0

for p in pattern:
    before = ()
    count = 0
    for place in p:
        if before:
            count += math.sqrt((d[before][0] - d[place][0])**2 + (d[before][1] - d[place][1])**2)
        before = place
    ans += count

ans /= math.factorial(N)

print(ans)

Submission status: Accepted

Solution code:
import math
import itertools

N = int(input())

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

ans = 0
for i in itertools.permutations(range(0, N), N):
	for j in range(1, N):
		ans += ((x[i[j]] - x[i[j - 1]]) ** 2 + (y[i[j]] - y[i[j - 1]]) ** 2) ** 0.5

print(ans / math.factorial(N))

Submission status: Accepted

Solution code:
import math

N=int(input())
X=[]
Y=[]
for i in range(N):
	x,y=map(int,input().split())
	X.append(x)
	Y.append(y)
L=[]
for i in range(N-1):
	for j in range(i,N):
		l=math.sqrt(((X[i]-X[j])**2)+((Y[i]-Y[j])**2))
		L.append(l)
ans=sum(L)*(2/N)
print(ans)

Submission status: Accepted

Solution code:
import math

N=int(input())
X=[]
Y=[]
for i in range(N):
	x,y=map(int,input().split())
	X.append(x)
	Y.append(y)
L=[]
for i in range(N-1):
	for j in range(i,N):
		l=math.sqrt(((X[i]-X[j])**2)+((Y[i]-Y[j])**2))
		L.append(l)
kai=(math.factorial(N))
ans=sum(L)*((kai*2)/N)/kai
print(ans)

Submission status: Accepted

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

import itertools
import math
pe=itertools.permutations(range(n))
pe=list(pe)
ans=0

for i in pe:
    tmp=0
    for j in range(n-1):
        tmp+=((a[i[j]][0]-a[i[j+1]][0])**2+(a[i[j]][1]-a[i[j+1]][1])**2)**0.5
    ans+=tmp
print(ans/math.factorial(n))

Submission status: Accepted

Solution code:
import math
from itertools import permutations

def f(i, j):
  return ((XY[i][0] - XY[j][0]) ** 2 + (XY[i][1] - XY[j][1]) ** 2) ** .5

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

ptn = permutations(range(n))

diff = 0
for p in ptn:
  diff += sum([f(i, j) for i, j in zip(p, p[1:])])
ans = diff / math.factorial(n)

print(ans)

Submission status: Accepted

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

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

print(ans/len(pattern))

Submission status: Accepted

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

import itertools
import math

f = [1]*n

for i in range(1, n):
	f[i]= f[i-1]*(i+1)
    
permutations_lis = itertools.permutations(xy)# 全ての場合のリストを生成
# 以下出力
total_d = 0
for one_case in permutations_lis: 
  x_cur = None
  y_cur = None
  d = 0
  for x, y in one_case:
    #print(x,y, end=' ')
    if x_cur is None:
      x_cur = x
      y_cur = y
    else:
      d += math.sqrt((x_cur-x)**2 + (y_cur-y)**2)
      #print(math.sqrt((x_cur-x)**2 + (y_cur-y)**2), end=' ')
      x_cur, y_cur = x, y
      
  total_d += d
      
print(total_d/f[n-1])

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc145/tasks/abc145_c

from itertools import permutations

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

def dst(x1,x2,y1,y2):
    return pow(pow(x1-x2,2)+pow(y1-y2,2),0.5)


for p in P:
    for i in range(n-1):
        cnt+=dst(p[i][0],p[i+1][0],p[i][1],p[i+1][1])

print(cnt/len(P))

Submission status: Accepted

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

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

ans = 0
for i in range(len(route)):
  for j in range(N-1):
    a = route[i][j]
    b = route[i][j + 1]
    X = (xy[a][0] - xy[b][0]) ** 2
    Y = (xy[a][1] - xy[b][1]) ** 2
    ans += (X + Y) ** 0.5
print(ans / len(route))

Submission status: Accepted

Solution code:
import itertools
def d(x,y):
    return ((x[0]-y[0])**2+(x[1]-y[1])**2) ** 0.5

N = int(input())
X = []
for _ in range(N):
    x, y = map(int, input().split())
    X.append((x,y))
r = 0
#print(X)
c = 0
for p in itertools.permutations(range(N)):
    c += 1
    for i in range(N-1):
#        print(p[i], p[i+1])
        r += d(X[p[i]], X[p[i+1]])
print(r/c)

Submission status: Accepted

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

d=0
for i in range(n):
    for j in range(n):
        d+=((xy[i][0]-xy[j][0])**2+(xy[i][1]-xy[j][1])**2)**0.5
print(d/n)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import itertools
n = int(input())
xy = []
for i in range(n):
    xy.append([int(i) for i in input().split()])

ans = 0
tmp = list(itertools.permutations(range(n)))
for i in range(len(tmp)):
    for j in range(len(tmp[i]) - 1):
        tmp2 = tmp[i][j + 1]
        tmp3 = tmp[i][j]
        ans += pow((pow(xy[tmp2][0] - xy[tmp3][0], 2) + pow(xy[tmp2][1] - xy[tmp3][1], 2)), 0.5)
ans /= len(tmp)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
x,y=[],[]
for i in range(n):
    X,Y=map(int,input().split())
    x.append(X)
    y.append(Y)
kaijo=1
for i in range(1,n+1):
    kaijo *= i
t = [i for i in range(1,n+1)]
ans=0
#print(t)
for i in list(permutations(t)):
    for j in range(n-1):
        #print(i[j+1])
        i = list(i)
        ans+=((x[i[j]-1]-x[i[j+1]-1])**2+(y[i[j]-1]-y[i[j+1]-1])**2)**0.5
print(ans/kaijo)

Submission status: Accepted

Solution code:
import itertools
n = int(input())
l = [list(map(int, input().split())) for _ in range(n)]
ln = [i for i in range(n)]
ans = 0
c = 0
for i in itertools.permutations(ln):
    c += 1
    for j in range(len(i)-1):
        ans += ((l[i[j]][0] -l[i[j+1]][0])**2 +(l[i[j]][1] -l[i[j+1]][1])**2)**0.5
print(ans/c)

Submission status: Accepted

Solution code:
from math import sqrt

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

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

    print(ans/n)

Submission status: Accepted

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

Submission status: Accepted

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

import itertools
import math

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

Submission status: Accepted

Solution code:
import math
from itertools import permutations

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

ret = 0
cnt = 0
for i in permutations(xy):
    for j in range(N-1):
        x = (i[j][0] - i[j+1][0]) ** 2
        y = (i[j][1] - i[j+1][1]) ** 2
        ret += math.sqrt(x+y)
    cnt += 1
print(ret/cnt)

Submission status: Accepted

Solution code:
# なぜかうまくいかない
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_right, bisect_left

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
L = [LIST() for i in range(N)]

# print(list(combinatins(L, 2)))
A = [n for n in range(N)]
ans = 0
for i in list(combinations(A, 2)):
    l, r = i
    a, b = L[r], L[l]
    c = (a[0] - b[0]) ** 2
    d = (a[1] - b[1]) ** 2
    ans += sqrt(c + d)

print(ans/ N * 2)

Submission status: Accepted

Solution code:
from itertools import permutations
n=int(input())
xy=[list(map(int,input().split()))for _ in range(n)]
ans=0
calc=lambda x1,y1,x2,y2:(abs(x1-x2)**2+abs(y1-y2)**2)**(0.5)
for i in permutations(xy):
  for j in range(1,n):
    ans+=calc(i[j-1][0],i[j-1][1],i[j][0],i[j][1])
for i in range(2,n+1):ans/=i
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
xy = [[int(i) for i in input().split()] for _ in range(N)]

from itertools import permutations

states = list(permutations(list(range(N))))

ans = 0
for state in states:
    dist = 0
    for i,n in enumerate(state):
        if i == 0:
            prev = xy[n]
            continue
        now = xy[n]
        dist += pow(pow(prev[0]-now[0], 2)+pow(prev[1]-now[1],2),.5)
        prev = now
    ans += dist

ans /= len(states)
print(ans)

Submission status: Accepted

Solution code:
from itertools import permutations
from math import factorial
N = int(input())
XY = [list(map(int, input().split())) for _ in range(N)]
x, y = zip(*XY)


def f(x, y): return (x ** 2 + y ** 2)**0.5


ans = 0
for p in permutations(range(N)):
    for s, t in zip(p[:-1], p[1:]):
        dx = x[t] - x[s]
        dy = y[t] - y[s]
        ans += f(dx, dy)

print(ans/factorial(N))

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

n = INT()
ans = 0
xy = [LIST() for _ in range(n)]
arr = list(permutations(list(range(n)), n))
ans = 0
for i in range(len(arr)):
    for j in range(len(arr[i]) - 1):
        ans += sqrt((xy[arr[i][j]][0] - xy[arr[i][j + 1]][0]) ** 2 + (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.buffer.readline
    from itertools import permutations
    from math import hypot
    N = int(input())
    XY = [[int(i) for i in input().split()] for j in range(N)]
    ans = 0
    for p in permutations(range(N)):
        for i in range(N-1):
            ans += hypot(XY[p[i+1]][0] - XY[p[i]][0],
                         XY[p[i+1]][1] - XY[p[i]][1])
    for i in range(1, N+1):
        ans /= i
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from itertools import permutations
from itertools import combinations

N = int(input().strip())
xy_list = [list(map(int, input().rstrip().split())) for i in range(N)]
#-----

dist ={}
for i,j in combinations(range(1,N+1),2):
    xi = xy_list[i-1][0]
    yi = xy_list[i-1][1]

    xj = xy_list[j-1][0]
    yj = xy_list[j-1][1]
    
    key = tuple(sorted( (i,j) ))
    dist[key] = ( (xi-xj)**2 + (yi-yj)**2 )**(1/2)


dist_all=[]
for p in permutations(range(1,N+1)):
    dist_sub=0 
    for i in range(len(p)-1):
        key = tuple(sorted( (p[i],p[i+1]) ))
        dist_sub += dist[key]
    
    dist_all.append(dist_sub)


ans = sum(dist_all) / len(dist_all)
print(Decimal(str(ans)).quantize(Decimal('1E-10'), rounding=ROUND_HALF_UP))