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:
# Contest: AtCoder Beginner Contest 145 (https://atcoder.jp/contests/abc145)
# Problem: C: Average Length (https://atcoder.jp/contests/abc145/tasks/abc145_c)

def rint():
    return int(input())


def rints():
    return list(map(int, input().split()))


n = rint()
p = [rints() for _ in range(n)]
from itertools import permutations
from math import sqrt
per = list(permutations(range(n)))
s = 0
for pe in per:
    for i in range(1, n):
        x1, y1 = p[pe[i - 1]]
        x2, y2 = p[pe[i]]
        s += sqrt((x1 - x2)**2 + (y1 - y2)**2)
print(s / len(per))

Submission status: Accepted

Solution code:
import math
n = int(input())
b = 0
c = math.ceil(n/2)
e = math.factorial(n)
s = []
for i in range(n):
    a = list(map(int,input().split()))
    s.append(a)
ans = 0
for l in range(n):
    for m in range(l+1,n):
        ans += ((s[l][0]-s[m][0])**2 + (s[l][1] - s[m][1])**2)**0.5
    b += 1
print(ans*2 / n)

Submission status: Accepted

Solution code:
import numpy as np
from itertools import permutations

N = int(input())
x = [0] * N
y = [0] * N
for i in range(N):
    x[i], y[i] = map(int, input().split())
x = np.array(x).reshape(N, 1)
y = np.array(y).reshape(N, 1)
dist = ((x - x.T)**2 + (y - y.T)**2)**0.5
_sum = 0
combination = list(permutations([i for i in range(N)]))
for comb in combination:
    for i in range(N-1):
        _sum += dist[comb[i], comb[i+1]]
ret = _sum / len(combination)
print(ret)

Submission status: Accepted

Solution code:
from itertools import permutations
from math import sqrt

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

d = []
for route in permutations(range(N), N):
    dist = 0
    for i, j in zip(range(N - 1), range(1, N)):
        dist += sqrt((X[route[j]] - X[route[i]]) ** 2 + (Y[route[j]] - Y[route[i]]) ** 2)
    d.append(dist)
print(sum(d) / len(d))

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import math
n = int(input())
x_list = []
y_list = []
d_sum = 0
for i in range(0, n):
    x, y = map(int, input().split())
    x_list.append(x)
    y_list.append(y)

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

Submission status: Accepted

Solution code:
import math

n = int(input())
m = []
ds = []

for i in range(n):
    m.append(list(map(int, input().split())))

for i in range(n):
    for j in range(i + 1, n):
        ds.append(math.sqrt((m[i][0] - m[j][0]) ** 2 + (m[i][1] - m[j][1]) ** 2))

ps = math.factorial(n)

x = (n-1) * math.factorial(n) / len(ds)

print(sum(ds) * (n-1) / len(ds))

Submission status: Accepted

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

n_fact = math.factorial(n)
n1_fact = math.factorial(n-1)
ans = 0
for i in range(n):
    for j in range(i+1, n):
        ans += ((cities[i][0]-cities[j][0])**2+(cities[i][1]-cities[j][1])**2)**0.5*n1_fact
print(ans/n_fact*2)

Submission status: Accepted

Solution code:
import itertools
import math
N = int(input())
x = [[int(x) for x in input().split()] for _ in range(N)]
p = itertools.permutations([x for x in range(N)])
d = 0
cnt = 0
for seq in p:
    cnt+=1
    for i in range(1,N):
        d += math.sqrt((x[seq[i-1]][0] - x[seq[i]][0])**2 + (x[seq[i-1]][1] - x[seq[i]][1])**2) 
print(d/cnt)

Submission status: Accepted

Solution code:
import math
x = []
y = []
length = 0
N = int(input())
for i in range(N):
    a, b = input().strip().split()
    x.append(int(a))
    y.append(int(b))
for i in range(N):
    for j in range(N):
        length += math.sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]))
length /= N
print(length)

Submission status: Accepted

Solution code:
import math
n = int(input())
C = list()
for _ in range(n):
  a, b = map(int, input().split(" "))
  C.append((a, b))

D = list()
for i, (x1, y1) in enumerate(C):
  for j, (x2, y2) in enumerate(C):
    if i>=j:continue
    d = math.sqrt((x1-x2)**2+(y1-y2)**2)
    D.append(d)

# print(D)
D = [x*(2*math.factorial(n-1)) for x in D]
# print(D)
# print(sum(D))
# print(math.factorial(n))
print(sum(D) / math.factorial(n))

Submission status: Accepted

Solution code:
import math

n = int(input())
xy_list = []
#n = 3
#xy_list = [[0,0 ],[0 ,1],[1,0]]
for i in range(n):
  xy_list.append(list(map(int,input().split())))
counter = 0
for m in range(n-1):
  for l in range(m,n):
    x_l = xy_list[l][0]
    y_l = xy_list[l][1]
    x_m = xy_list[m][0]
    y_m = xy_list[m][1]
    counter += ((x_m - x_l)**2 + (y_m - y_l)**2)**0.5

print(counter*2/n)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
import math

N = int(input())
XY = [list(map(int,input().split(' '))) for _ in range(N)]
All_combination = list(itertools.permutations(XY))
All_distances = []
for each_list in All_combination:
    distance = 0
    for i in range(1,N):
        distance += math.sqrt((each_list[i-1][0] - each_list[i][0])**2 + (each_list[i-1][1] - each_list[i][1])**2) 
    All_distances.append(distance)
print(sum(All_distances)/len(All_distances))

Submission status: Accepted

Solution code:
import math

def main():
        index=0
        a=[]
        b=[]
        N = int(input())
        for i in range(N):
            A,B=input().split()
            a.append(int(A))
            b.append(int(B))
        for i in range(N):
            for j in range(N-1-i):
                index+=calc_length(a[i],a[j+1+i],b[i],b[j+1+i])

        print(index*2/N)

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import itertools
import numpy as np

def solve(n, x, y):
    total, num = 0, 0
    x, y = np.asarray(x), np.asarray(y)
    for p in itertools.permutations(range(n)):
        num += 1
        p = np.array(p, dtype=int)
        dx = x[p[1:]] - x[p[:-1]]
        dy = y[p[1:]] - y[p[:-1]]
        total += np.sum(np.sqrt(dx**2 + dy**2))
    return total / num

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

Submission status: Accepted

Solution code:
import math
N = int(input())
x_list=[]
y_list=[]
dirsum=0
for i in range(N):
	x,y = input().split()
	x_list.append(int(x))
	y_list.append(int(y))
for i in range(N):
	for j in range(i+1,N):
		dirsum += math.sqrt(pow(x_list[i]-x_list[j],2)+pow(y_list[i]-y_list[j],2))
print(2*math.factorial(N-1)*dirsum/math.factorial(N))

Submission status: Accepted

Solution code:
N = int(input())
tl = [[int(x) for x in input().split()] for _ in range(N)]
ans = 0

for i in range(N):
  for j in range(N):
    ans += ((tl[i][0]-tl[j][0])**2 + (tl[i][1]-tl[j][1])**2)**0.5
    
print(ans/N)

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)

s = 0
for p in itertools.permutations(range(N), N):
    for i in range(len(p) - 1):
        s += (((X[p[i]] - X[p[i+1]]) ** 2) + ((Y[p[i]] - Y[p[i+1]]) ** 2)) ** 0.5

print(s / math.factorial(N))

Submission status: Accepted

Solution code:
from itertools import permutations

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

lengths = []
for ps in permutations(points):
    lengths.append(sum(
        ((ps[i][0] - ps[i - 1][0]) ** 2 + (ps[i][1] - ps[i - 1][1]) ** 2) ** 0.5
        for i in range(1, N)
    ))
print(sum(lengths) / len(lengths))

Submission status: Accepted

Solution code:
n=int(input())
hoge=[]
for i in range(n):
  x,y=map(int,input().split())
  hoge.append([x,y])
ans=0
for i in range(n):
  for j in range(n):
    if(i!=j):
      ans+=((hoge[i][0]-hoge[j][0])**2+(hoge[i][1]-hoge[j][1])**2)**0.5
ans=(ans/n)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
x = []
y = []
for num in range(N):
  li = input()
  ubah = li.split(" ")
  x.append(int(ubah[0]))
  y.append(int(ubah[1]))

sum = 0
for i in range(N):
    for j in range(1+i,N):
        dist = ((x[i]-x[j])**2+(y[i]-y[j])**2)**0.5
        sum += dist*2/N
print(sum)

Submission status: Accepted

Solution code:
import math
import itertools
n = int(input())
x = [0 for _ in range(n)]
y = [0 for _ in range(n)]
for i in range(n):
    x[i], y[i] = map(int, input().split())
a = [k for k in range(n)]
cnt = math.factorial(n)
l_list = [0 for _ in range(cnt)]
m = 0
for b in itertools.permutations(a):
    l = 0
    for j in range(1, n):
        l += ((x[b[j]] - x[b[j - 1]]) ** 2 + (y[b[j]] - y[b[j - 1]]) ** 2) ** 0.5
    l_list[m] = l
    m += 1
print(sum(l_list) / cnt)

Submission status: Accepted

Solution code:
import copy
import heapq
import math

def j(n):
    if n:print("Yes")
    else:print("No")
    exit(0)
rem = 10 ** 9 + 7

"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def ipmultiplerow(n):
    a = []
    for i in range(n):
        a.append(ip())
    return a
def printrow(a):
    for i in a:
        print(i)

n = ip()
a = []
s=0
for i in range(n):
    a.append(iprow())
adder = math.factorial(n)*2//n
#print(adder)
for i in range(n-1):
    for k in range(i,n):
        x = a[i]
        y = a[k]
        dist = math.sqrt( (x[0] - y[0])**2 + (x[1] - y[1])**2 )
        s+=dist*adder
print(s/math.factorial(n))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
import math

#math.sqrt(x)

N = int(raw_input())
xy = []
p = []
ans = float(0)
for i in range(0,N):
  tmp = list(map(int,raw_input().split()))
  xy.append(tmp)
  p.append(i)


x = 0

for v in itertools.permutations(p, N):
  #print tmp
  for j in range(0,N-1):
    #print math.sqrt((float((xy[v[j]][0]-xy[v[j+1]][0])**2) + float((xy[v[j]][1]-xy[v[j+1]][1]))**2))
    x = (xy[v[j]][0]-xy[v[j+1]][0])**2 + (xy[v[j]][1]-xy[v[j+1]][1])**2
    x = math.sqrt(int(x))
    #tmp = math.sqrt((xy[v[j]][0]-xy[v[j+1]][0])**2 - (xy[v[j+1]][j+1]-xy[v[j+1]][j])**2)
    ans += x

print ans/math.factorial(N)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
from itertools import combinations
from math import sqrt

def main():
    n=int(input())
    xy=[]
    for _ in range(n):
        x,y=map(int,input().split())
        xy.append((x,y))
    s=0
    c=0
    for a,b in combinations(xy, 2):
        s+=sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2)
        c+=1
    s*=(n-1)
    s/=c
    print(s)

main()

Submission status: Accepted

Solution code:
import itertools

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

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

cnt = 0
cnt2 = 0
	
for _ in itertools.permutations(lst,n):
	cnt2 +=1
	for i in range(n-1):
		cnt += ((_[i][0]-_[i+1][0])**2+(_[i][1]-_[i+1][1])**2)**0.5

print(cnt/cnt2)

Submission status: Accepted

Solution code:
import itertools
import math

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

P = math.factorial(N)

paterns = list(itertools.permutations(A))
sum = 0

for _patern in paterns:
    patern = list(_patern)
    last = patern.pop(0)
    for point in patern:
        d = ((last[0] - point[0])**2 + (last[1] - point[1])**2)**0.5 / P
        sum = sum + d
        last = point

print(sum)

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

result = 0
d = 0
for p in permutations(xy):
    d += 1
    for i in range(N - 1):
        result += sqrt((p[i][0] - p[i + 1][0]) * (p[i][0] - p[i + 1][0]) + (p[i][1] - p[i + 1][1]) * (p[i][1] - p[i + 1][1]))
print(result / d)

Submission status: Accepted

Solution code:
N=int(input())
L=[]
for i in range(N):
    x,y=map(int,input().split())
    L.append([x,y])
    
cnt=0
S=0
for i in range(N-1):
    for j in range(i+1,N):
        cnt+=1
        S+=((L[i][0]-L[j][0])**2+(L[i][1]-L[j][1])**2)**0.5
AVE=S/cnt
print(AVE*(N-1))

Submission status: Accepted

Solution code:
from math import sqrt,factorial
from itertools import permutations
N = int(input())
xy = [list(map(int,input().split())) for _ in range(N)]
perm = permutations(range(N),N)
tmp = 0
for p in perm:
    for i in range(1,N):
        tmp += sqrt((xy[p[i]][0] - xy[p[i-1]][0])**2 + (xy[p[i]][1] - xy[p[i-1]][1])**2)
print(tmp/factorial(N))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
n = int(input())
x, y = [], []
f = 1
for i in range(n):
    f *= i + 1
    xi, yi = map(int, input().split())
    x.append(xi)
    y.append(yi)
p = [i for i in range(n)]
d = 0
for perm in itertools.permutations(p):
    for i in range(n - 1):
        d += ((x[perm[i]] - x[perm[i + 1]]) ** 2 + (y[perm[i]] - y[perm[i + 1]]) ** 2) ** 0.5
print(d / f)

Submission status: Accepted

Solution code:
import math
import itertools

n = int(input())
e = [[int(i) for i in input().split()] for i in range(n)] 
len_sum = 0
count = 0

for x in itertools.permutations(e):
  for i in range(n):
    for j in range(n):
      if j - i != 1:
        continue
      else:
        len_sum += math.sqrt(math.pow(x[i][0] - x[j][0], 2) + math.pow(x[i][1] - x[j][1], 2))
  count += 1
  
print(len_sum/count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
pl = list()
for i in range(n):
    pl.append(list(map(int, input().split())))

import math
lsum = 0
for i in range(n):
    for j in range(n):
        if i != j:
            a, b = pl[i]
            c, d = pl[j]
            lsum += math.sqrt((a-c) ** 2 + (b-d) ** 2)
print(lsum /(n))

Submission status: Accepted

Solution code:
import math

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

k = [1]
for i in range(n):
    k.append(k[i] * (i + 1))
l = 0


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


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


def main():
    from itertools import permutations

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

    m = len(list(permutations(range(n))))

    for p in permutations(range(n)):
        for i in range(len(p) - 1):
            a = p[i]
            b = p[i + 1]
            total += calc_dist(xy[a][0], xy[b][0], xy[a][1], xy[b][1])

    print(total / m)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
import itertools

N = int(input())
A =[]
path_length = 0
permm = list(range(N))

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

for p in itertools.permutations(permm):
  # p は、点の順列

  for i, j in zip(p, p[1:]):
      x1, y1 = A[i]
      x2, y2 = A[j]
      path_length += math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
ans = path_length / math.factorial(N)

print(ans)

Submission status: Accepted

Solution code:
from itertools import permutations
from math import hypot, factorial
n = int(input())
t = [list(map(int,input().split())) for _ in range(n)]

sum = 0
for p in permutations(t):
  for i in range(n-1):
    sum += hypot(p[i+1][0]-p[i][0], p[i+1][1]-p[i][1])
print(sum/factorial(n))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
x = [list(map(int,input().split())) for _ in range(N)]
# print(list(itertools.combinations(x,2)))
a = sum(((p-r)**2 + (q-s)**2 ) **0.5 for [(p,q),(r,s) ]in itertools.combinations(x,2))

print(2*a/N)

Submission status: Accepted

Solution code:
import math
N= int(input())
x=[0]*N
y=[0]*N
for i in range(N):
  x[i], y[i]=map(int, (input()).split() )
S = 0
nb =0
for i in range(N) :
  for j in range(0, i) :
    S += math.sqrt( (x[i]-x[j])**2 + (y[i]-y[j])**2 )
    nb += 1
print(S/nb*(N-1))

Submission status: Accepted

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

R = list(itertools.permutations(range(n), n))
out = 0
N = math.factorial(n)
for case in R:
  for i in range(1, n):
    out += math.sqrt((L[case[i]][0] - L[case[i-1]][0])**2 
                     + (L[case[i]][1] - L[case[i-1]][1])**2)
print(out/N)

Submission status: Accepted

Solution code:
import itertools
import math
n = int(input())
town = [tuple(map(int, input().split())) for _ in range(n)]
ans = 0
cnt = 0

for p in itertools.permutations(range(0, n), n):
    for i in range(n - 1):
        distance = (town[p[i]][0] - town[p[i + 1]][0]) ** 2 + \
            (town[p[i]][1] - town[p[i + 1]][1]) ** 2
        ans += math.sqrt(distance)
    cnt += 1
ans /= cnt
print(ans)

Submission status: Accepted

Solution code:
import math
from itertools import permutations, combinations,combinations_with_replacement,product

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

def distance_aaa(arg_list):
    result =[]
    for i in range(len(arg_list)-1):
        result.append(math.sqrt((list_aaa[arg_list[i]][0]-list_aaa[arg_list[i+1]][0])**2+(list_aaa[arg_list[i]][1]-list_aaa[arg_list[i+1]][1])**2))
    return sum(result)


loop_x =list(permutations(range(N)))

result_aaa =[]

for i in loop_x:
    result_aaa.append(distance_aaa(i))

print(sum(result_aaa)/math.factorial(N))

Submission status: Accepted

Solution code:
import itertools
import numpy as np
import math

# print('input >>')
N = int(input())
towns = []
for _ in range(N):
    x, y = map(int,(input().split()))
    towns.append(np.array([x,y]))

sum = 0
perms = itertools.permutations(range(N), N)

distances = [[0] * N for _ in range(N)]

for i in range(N):
    for j in range(N):
        distances[i][j] = np.linalg.norm(towns[i]-towns[j])

sum = 0
for p in perms:
    distance = 0
    for i in range(N-1):
        distance += distances[p[i]][p[i + 1]]
    sum += distance

# print('----------output----------')
print(sum / math.factorial(N))

Submission status: Accepted

Solution code:
import numpy as np
from scipy.spatial import distance
from scipy.sparse.csgraph import floyd_warshall
n = int(input())
xy = np.zeros((n, 2))
for i in range(n):
    xy[i, 0], xy[i, 1] = (int(x) for x in input().split())
print(np.sum(distance.cdist(xy, xy, metric='euclidean'))/n)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
pos = []
for i in range(N):
    pos.append(list(map(int, input().strip().split())))
pos = np.array(pos)
tmp_index = np.arange(N)
xx, yy = np.meshgrid(tmp_index, tmp_index)
distances = np.linalg.norm(pos[xx]-pos[yy], axis=2)

print(distances.mean()*N)

Submission status: Accepted

Solution code:
import itertools
import math

N = int(input())

dis = 0
a = 0


x = [0] * N
y = [0] * N
for i in range(N):
  x[i], y[i] = map(int, input().split())
for i in itertools.permutations(range(N)):
  for j in range(1,len(i)):
    dis += math.sqrt((x[i[j]] -x[i[j-1]])**2 + (y[i[j]] - y[i[j-1]])**2)
a = itertools.permutations(range(N))
print(dis/len(list(a)))

Submission status: Accepted

Solution code:
import math
import itertools
N = int(input())
x = [0] * N
y = [0] * N
way = [0] * N
seq = []
dis = 0

def distance(a, b):
    return ((x[way[a]] - x[way[b]]) ** 2 + (y[way[a]] - y[way[b]]) ** 2) ** 0.5

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

ways = list(itertools.permutations(seq))

n = len(ways)
for i in range(n):
    way = ways[i]
    for j in range(N - 1):
        dis += distance(j, j + 1)
print(dis / n)

Submission status: Accepted

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

import itertools

town_number = [i for i in range(N)]
town_group = list(itertools.combinations(town_number,2))

length = []

for i in range(len(town_group)):
    x = xy[town_group[i][0]][0] - xy[town_group[i][1]][0]
    y = xy[town_group[i][0]][1] - xy[town_group[i][1]][1]
    length.append((x**2 + y**2)**0.5)
    
print(sum(length)*2/N)

Submission status: Accepted

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

ls = []
from itertools import permutations
Ns = [i for i in range(N)]
perms = permutations(Ns)
perms = list(perms)

for perm in perms:
    cnt = 0
    for i in range(1,N):
        x1, y1 = a[perm[i-1]]
        x2, y2 = a[perm[i]]
        cnt += ((x1-x2)**2+(y1-y2)**2)**0.5
    ls.append(cnt)


#     for j in range(i+1,N):
#         x1,y1 = a[i]
#         x2,y2 = a[j]
print(sum(ls)/len(ls))


# print(perms)

Submission status: Accepted

Solution code:
n=int(input())
p=1
a=[]
for i in range(n):
  p*=i+1
  a.append(list(map(int,input().split())))
p//=n
ans=0
Dis=lambda x,y:((x[0]-y[0])**2+(x[1]-y[1])**2)**.5
for x in a:
  for y in a:
    ans+=p*Dis(x,y)

print("%.10f" % (ans/p/n))

Submission status: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in sys.stdin.readline().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v

def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False

# --------------------------------------------

dp = None


def f(a, b, c, d):
    return math.sqrt((a - b) ** 2 + (c - d) ** 2)

def main():
    N = int(input())
    L = [list(map(int, input().split())) for i in range(N)]
    Dsum = 0
    Dlen = 0

    for i, l in enumerate(itertools.permutations(range(N))):
        for j in range(len(l)-1):
            Dsum += f(L[l[j]][0], L[l[j+1]][0], L[l[j]][1], L[l[j+1]][1])
        Dlen += 1

    print(Dsum / Dlen)


main()

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
l = [[int(i) for i in input().split()] for _ in range(n)]

distance = 0
for i in l:
    for j in l:
        distance += np.sqrt((i[0] - j[0])**2 + (i[1] - j[1])**2)
        
print(distance/n)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
x = []
y = []
for i in range(n):
  _x, _y = map(int, input().split())
  x.append(_x)
  y.append(_y)
#それぞれの組み合わせに対して2*(n-1)!回ずつ合計に加算される→平均への寄与は2/n倍
def d(x1, y1, x2, y2):
  return ((x1-x2)**2 + (y1-y2)**2)**0.5

ans = 0
for i in range(n):
  for j in range(i+1, n):
    ans += d(x[i], y[i], x[j], y[j])
print(ans*2/n)

Submission status: Accepted

Solution code:
import math
N=int(input())
A = [[int(i) for i in input().split()] for N in range(N)] 
S=0
for x in range(0,N-1):
  for y in range(x+1,N):
    S=S+math.sqrt((A[x][0]-A[y][0])**2+(A[x][1]-A[y][1])**2)
ave=S*2/N
print(ave)

Submission status: Accepted

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

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

    
print(ans*2/n)

Submission status: Accepted

Solution code:
n=int(input())
a=[list(map(int,input().split())) for i in range(n)]
def l2(a,b,c,d):
    return ((a-c)**2+(b-d)**2)**0.5
c=[]
for i in range(n):
    for j in range(n):
        if i==j: continue
        else:
            c.append(l2(a[i][0],a[i][1],a[j][0],a[j][1]))
#print(c)
print(sum(c)*(n-1)/len(c))

Submission status: Accepted

Solution code:
class Pos:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def cal_distance(self, pos):
        return ((self.x - pos.x)**2 + (self.y - pos.y)**2)**0.5


N = int(input())
data = [input() for _ in range(N)]

pos_list = []
for d in data:
    x,y = map(int, d.split())
    pos = Pos(x, y)
    pos_list.append(pos)

result_list = []
for i in range(N):
    for j in range(N):
        cal = pos_list[i].cal_distance(pos_list[j])
        result_list.append(cal)

print(sum(result_list) / N)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def calc(x1,y1,x2,y2):
    return (((x1-x2)**2+(y1-y2)**2))**0.5

def kaijo(n):
    if n == 1:
        return n
    return n * kaijo(n-1)

n = int(input())
lsx = []
lsy = []

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

num = 0
cnt = 0

for i in range(n):
    for j in range(i+1,n):
        num += calc(lsx[i],lsy[i],lsx[j],lsy[j])
        cnt += 1

print(num*(n-1)/cnt)

Submission status: Accepted

Solution code:
import math

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

XY_dict = {}

for i in range(0,N):
  for j in range(i+1,N):
    key = str(i) + str(j)
    XY_dict[key] = math.sqrt( (XY[i][0] - XY[j][0])**2 + (XY[i][1] - XY[j][1])**2 )

ans = 0
for xy in XY_dict.values():
  ans += xy

ans = (ans * 2)/N

print(ans)