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:
from itertools import permutations

def solve():
    N = int(input())
    P = [[int(i) for i in input().split()] for _ in range(N)]
    tmp = 0
    c = 0
    for p in permutations(P):
        t = 0
        c += 1
        for i in range(N - 1):
            t += ((p[i][0] - p[i + 1][0]) ** 2 + (p[i][1] - p[i + 1][1]) ** 2) ** 0.5
        tmp += t
    print(tmp / c)

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import math
n=int(input())
XY=[list(map(int,input().split())) for _ in range(n)]
ans=0
for i in range(n-1):
  for j in range(i+1,n):
    disx=XY[i][0]-XY[j][0]
    disy=XY[i][1]-XY[j][1]
    dis=math.sqrt(disx**2+disy**2)

    ans+=dis

print(2*ans/n)

Submission status: Accepted

Solution code:
import math
N = int(input())
x = []
y = []

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


route = 0
for i in range(N):
    for j in range(i, N):
        route += math.sqrt((x[i]-x[j])**2 + (y[i]-y[j])**2)

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

Submission status: Accepted

Solution code:
from itertools import permutations
import math

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

list = [i for i in range(n)]

total = 0
for i in permutations(list):
    norm = 0
    for j in range(n-1):
        X = x[i[j+1]] - x[i[j]]
        Y = y[i[j+1]] - y[i[j]]
        norm += math.sqrt(X**2 + Y**2)
    total += norm
print(total/math.factorial(n))

Submission status: Accepted

Solution code:
from math import factorial
from itertools import permutations

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

print(sum(sum(((x-v)**2+(y-w)**2)**.5 for (x, y), (v, w) in zip([t]+l, l+[t])) for l in map(list,permutations(c)))*(n-1)/factorial(n))

Submission status: Accepted

Solution code:
import itertools
import math
N = int(input())


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

a = list(itertools.permutations(range(N), N))

ans = 0
for i in a:
    b = 0
    for j in range(N-1):
        b += math.sqrt((citys[i[j]][0] - citys[i[j+1]][0])**2 + (citys[i[j]][1] - citys[i[j+1]][1])**2)
    ans += b

print(ans/len(a))

Submission status: Accepted

Solution code:
from math import sqrt
import itertools
N = int(input())
dis = [input().split() for i in range(N)]
lst = [x for x in range(N)]

p_lst = list(itertools.permutations(lst))

ans = 0
for i in p_lst:
    for j in range(N-1):
        ans += sqrt((int(dis[i[j]][0]) - int(dis[i[j+1]][0]))**2
                  + (int(dis[i[j]][1]) - int(dis[i[j+1]][1]))**2)

print(ans/len(p_lst))

Submission status: Accepted

Solution code:
import itertools
import math
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N=ii()
    X = []
    for i in range(N):
        x,y = mi()
        X.append((x,y))

    sm = 0
    count = 0
    for v in itertools.permutations(X,N):
        V = list(v)
        pre = V[0]

        for u in V[1:]:
            d = math.sqrt((pre[0]-u[0])**2 + (pre[1]-u[1]) ** 2)
            sm += d
        
        count += 1

    ans = sm/count
    print('{:.10f}'.format(ans))

    


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
x_y = []
for _ in range(n):
  x_y.append([int(i) for i in input().split()])
import itertools
import math
ans = 0.0
for x_yi in itertools.permutations(x_y, n):
  sum_ = 0.0
  for i in range(n-1):
    sum_ += math.sqrt((x_yi[i][0] - x_yi[i+1][0]) ** 2 +
                      (x_yi[i][1] - x_yi[i+1][1]) ** 2)
  ans += sum_ / math.factorial(n)
print(ans)

Submission status: Accepted

Solution code:
import itertools
import math

N=int(input())
pos=[list(map(int,input().split())) for _ in range(N)]
Cases=itertools.permutations([x for x in range(N)])

ans=0
for case in Cases:
    for i in range(N-1):
        x,y=map(int,pos[case[i]])
        nx,ny=map(int,pos[case[i+1]])
        tmp=math.sqrt((nx-x)**2+(ny-y)**2)
        ans+=tmp
ans/=math.factorial(N)
print(ans)

Submission status: Accepted

Solution code:
import itertools

n = int(input())
p = [[0 for i in range(2)] for j in range(n)]

for i in range(n):
  p[i][0],p[i][1] = map(int,input().split())
#print(p)

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


perm = [i for i in range(n)]
total = 0 
num = 0
for v in itertools.permutations(perm,n):
  path = 0
  for i in range(n-1):
    path += dis(p[v[i]],p[v[i+1]])
  num += 1
  total += path
print(total/num)

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    lst = [None, ]
    for _ in range(n):
        x, y = LI()
        lst.append((x,y))

    per = itertools.permutations(range(1,n+1))
    sm = 0
    oc = math.factorial(n)
    for p in per:
        dist = 0
        before = p[0]
        for place in p[1:]:
            dist += math.sqrt((lst[place][0]-lst[before][0])**2 + (lst[place][1]-lst[before][1])**2)
            before = place
        sm += dist
    ans = sm/oc
    print(ans)
main()

Submission status: Accepted

Solution code:
from itertools import permutations
N=int(input())
arr=[list(map(int,input().split())) for i in range(N)]

def dis(x):
    c=0
    for i in range(N-1):
        aa=arr[x[i]][0]-arr[x[i+1]][0]
        bb=arr[x[i]][1]-arr[x[i+1]][1]
        c+=(aa**2+bb**2)**(0.5)
    return c
 
count=0
ans=0
for i in permutations(range(0,N),N):
    count+=1
    ans+=dis(i)

print(ans/count)

Submission status: Accepted

Solution code:
import math
n=int(input())
a = []
b = []
ans = 0
def z(p,q,r,s):
    return ((p-r)**2+(q-s)**2)**0.5
for i in range(n):
    x,y=map(int,input().split())
    a.append(x)
    b.append(y)
for i in range(n):
    for j in range(i+1,n):
        ans += 2*z(a[i],b[i],a[j],b[j])
ans /= n
print(ans)

Submission status: Accepted

Solution code:
import itertools
import math

n=int(input())
xy=[[int(i) for i in input().split()] for _ in range(n)]
p = itertools.permutations(range(n),n)

lenlist=[]
for v in p:
    len=0
    for i in range(n-1):
        len +=  ( (xy[v[i+1]][0]-xy[v[i]][0])**2 + (xy[v[i+1]][1]-xy[v[i]][1])**2 )**0.5
    lenlist.append(len)

print(sum(lenlist)/math.factorial(n))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
s = ([str(_) for _ in range(n)])
l = list(itertools.permutations(s, n))
x = []
y = []
for i in range(n):
    xi,yi = map(int,input().split())
    x.append(xi)
    y.append(yi)
c = 0.0
for j in l:
    for k in range(n-1):
        c += ((x[int(j[k])]-x[int(j[k+1])])**2 + (y[int(j[k])]-y[int(j[k+1])])**2)**0.5
print(c/len(l))

Submission status: Accepted

Solution code:
import itertools

n = int(input())

town = []

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

cnt = 0
length = 0

for t in itertools.permutations(town):
    for i in range(n-1):
        x = (t[i][0]-t[i+1][0])**2
        y = (t[i][1]-t[i+1][1])**2
        length += (x+y)**0.5
    cnt += 1

print(length/cnt)

Submission status: Accepted

Solution code:
import itertools
n = int(input())
s = "".join([str(_) for _ in range(n)])
l = list(itertools.permutations(s, n))
x = []
y = []
for i in range(n):
  xi,yi = map(int,input().split())
  x.append(xi)
  y.append(yi)
c = 0.0
for j in l:
  for k in range(n-1):
    c += ((x[int(j[k])]-x[int(j[k+1])])**2 + (y[int(j[k])]-y[int(j[k+1])])**2)**0.5
print(c/len(l))

Submission status: Accepted

Solution code:
N = int(input())

cities = [tuple(map(int,input().split())) for i in range(N)]

from collections import defaultdict
dist = defaultdict(int)

import itertools
for perm in itertools.permutations(range(N), 2):
  x1,y1 = cities[perm[0]]
  x2,y2 = cities[perm[1]]
  dist[perm] = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5

ans = 0
cnt = 0
for perm in itertools.permutations(range(N), N):
  cnt += 1
  for i in range(1 ,len(perm)):
    ans += dist[(perm[i - 1], perm[i])]

print(ans / cnt)

Submission status: Accepted

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

L = 0
for i in range( N ):
 for j in range( N ):
  a = xy[i][0]-xy[j][0]
  b = xy[i][1]-xy[j][1]
  l = np.sqrt( a**2 +b**2)
  L += l

print(L/N)

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
v = [list(map(int,input().split())) for i in range(n)]
ans = 0
c = 0
for p in permutations(range(n)):
    c += 1
    arr = [i for i in p]
    for i in range(n-1):
        ans += ((v[arr[i]][0]-v[arr[i+1]][0])**2 + (v[arr[i]][1]-v[arr[i+1]][1])**2)**0.5
ans /= c
print(ans)

Submission status: Accepted

Solution code:
import math
import itertools
n = int(input())
al = [0] * n
bl = [0] * n
for i in range(n):
    al[i], bl[i] = map(int, input().split())
bunbo = math.factorial(n)
ans = 0
for p in itertools.permutations(list(range(n))):
    for i in range(n-1):
        ans += ((al[p[i+1]]-al[p[i]])**2+(bl[p[i+1]]-bl[p[i]])**2)**0.5
print(ans/bunbo)

Submission status: Accepted

Solution code:
import itertools
N = int(input())
N_List = [list(map(int,input().split())) for _ in range(N)]
ans = 0
cnt = 0
for Sort_List in itertools.permutations(N_List, N):
    cnt += 1
    cans = 0
    for v in range(1,N):
        cans += ((Sort_List[v][0] - Sort_List[v-1][0])**2 + (Sort_List[v][1] - Sort_List[v-1][1])**2)**0.5
    ans += cans

print(ans/cnt)

Submission status: Accepted

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

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

def D(a,b):
    a=a-1
    b=b-1
    return ((x[a]-x[b])**2+(y[a]-y[b])**2)**(0.5)

def f(x):
    ans=0
    while x>0:
        ans+=x%2
        x=x//2
    return ans

def r(x):
    ans=1
    for i in range(x):
        ans*=(i+1)
    return ans

dp=[[0]*(n+1) for _ in range(2**n)]
for i in range(1,2**n):
    for j in range(1,n+1):
        for k in range(1,n+1):
            if (i>>(j-1))&1 and not (i>>(k-1)&1):
                dp[i+2**(k-1)][k]+=dp[i][j]+r((f(i)-1))*D(j,k)

print(sum(dp[2**n-1])/p)

Submission status: Accepted

Solution code:
def f(n):
    if n == 1:
        return 1
    else:
        return n * f(n - 1)

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

total = 0
for i in range(n):
    for j in range(n):
        total += ((X[i] - X[j])**2 + (Y[i] - Y[j])**2)**0.5

print(total/n)

Submission status: Accepted

Solution code:
import itertools
import math

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

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

ans = 0
for p in itertools.permutations(xy):
  for i in range(n-1):
    ans += dist(*p[i], *p[i+1])

print(ans/math.factorial(n))

Submission status: Accepted

Solution code:
from itertools import permutations
from math import factorial


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

    for p in permutations(range(n), n):
        dist = 0
        for i in range(len(p) - 1):
            dist += (abs(xy[p[i]][0] - xy[p[i + 1]][0]) ** 2 +
                     abs(xy[p[i]][1] - xy[p[i + 1]][1]) ** 2) ** 0.5
        ans += dist

    ans /= factorial(n)

    print(f"{ans:.12f}")


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import permutations as p
from math import factorial as f
from math import sqrt as s

def func(x1, x2, y1, y2): return s((x2-x1) ** 2 + (y2-y1) ** 2)

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

total = 0

for l in p(range(1, N+1)):
  for i in range(N-1):
    total += func(xy[l[i]-1][0], xy[l[i+1]-1][0], xy[l[i]-1][1], xy[l[i+1]-1][1])

print(total / f(N))

Submission status: Accepted

Solution code:
import math
import itertools
n=int(input())
town=[]
for i in range(n):
  xy=list(map(int,input().split()))
  town.append(xy)
p=list(itertools.permutations(town,n))
d=0
for i in range(len(p)):
  for j in range(len(p[i])-1):
    d+=math.sqrt((p[i][j][0]-p[i][j+1][0])**2+(p[i][j][1]-p[i][j+1][1])**2)
print(d/len(p))

Submission status: Accepted

Solution code:
N = int(input())
P = []
for i in range(N):
    s = [int(i) for i in input().split()]
    P.append(s)
L = []
for i in range(N-1):
    for j in range(i+1, N):
        l = (P[i][0] - P[j][0])**2 + (P[i][1] - P[j][1])**2
        L.append(l**0.5)
S = sum(L)
print(2*S/N)

Submission status: Accepted

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

perm= math.factorial(n)
s = 0

M=[]
for v in itertools.permutations(L, n):
    M.append(list(v))


for i in range(perm):
    for j in range(n-1):
        s += math.sqrt((M[i][j][0] - M[i][j+1][0])**2 +(M[i][j][1] - M[i][j+1][1])**2)
print(s/perm)

Submission status: Accepted

Solution code:
import itertools

n = int(input())

p = [[int(i) for i in input().split()] for i in range(n)]

def how_far(a,b):
    return ((a[0]-b[0])**2+(a[1]-b[1])**2)**0.5

p_list = list(itertools.permutations(p))

s = 0

for x in p_list:
    for y in range(n-1):
        s += how_far(x[y],x[y+1])

print(s/len(p_list))

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
x = []
y = []
ans,count,tempx,tempy = 0,0,0,0
for _ in range(n):
    a,b = map(int,input().split())
    x.append(a)
    y.append(b)
order = list(permutations(range(n)))
for i in order:
    for j in i:
        if j == i[0]:
            tempx = x[j]
            tempy = y[j]
            continue
        count += (((x[j]-tempx)**2)+((y[j]-tempy)**2)) ** (0.5) 
        tempx = x[j]
        tempy = y[j]
    ans += count / len(order)
    count = 0
print(ans)

Submission status: Accepted

Solution code:
import itertools
n = int(input())
points = [ list(map(int,input().split())) for _ in range(n)]
routes = list(itertools.permutations(points))
ans = 0
for route in routes:
    for i in range(len(route)-1):
        x,y = route[i][0],route[i][1]
        a,b = route[i+1][0],route[i+1][1]
        ans += ((x-a)**2+(y-b)**2 )**(0.5)
ans /= len(routes)        
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# row = [int(x) for x in input().rstrip().split(" ")]
# n = int(input().rstrip())
# s = input().rstrip()
# row = [int(input().rstrip()) for _ in range(n)]

def resolve():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip())

    cities = [[int(x) for x in input().rstrip().split(" ")] for _ in range(n)]
    import math

    import itertools

    length_list = []
    p = itertools.permutations(list(range(n)), n)
    for v in p:
        length = 0
        for city_i in range(n - 1):
            city1 = cities[v[city_i]]
            city2 = cities[v[city_i + 1]]
            length += math.sqrt((city1[0] - city2[0]) ** 2 + (city1[1] - city2[1]) ** 2)

        length_list.append(length)

    print(sum(length_list) / len(length_list))


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
import math

n = int(input())
x = []
num = []
for i in range(n):
  x.append([int(t) for t in input().split()])
  num.append(i)

s = 0
for i in itertools.permutations(num):
  i = list(i)
  #print(x,i)
  for j in range(n-1):
    s += math.sqrt((x[i[j]][0] - x[i[j+1]][0])**2 + (x[i[j]][1] - x[i[j+1]][1])**2)
    #print(s)
s /= math.factorial(n)
print(s)

Submission status: Accepted

Solution code:
from itertools import permutations

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

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

d = 0
cnt = 0
for per in permutations(list(range(n)), n):
    cnt += 1
    for i in range(n-1):
        a = xy[per[i]]
        b = xy[per[i+1]]
        d += distance(a, b)

print(d/cnt)

Submission status: Accepted

Solution code:
import math
import itertools

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

    ans = 0
    for order in itertools.permutations(list(range(N)), N):
        d = 0
        for i in range(N-1):
            s, e = order[i], order[i+1]
            d += math.sqrt((towns[s][0] - towns[e][0]) ** 2 + (towns[s][1] - towns[e][1]) ** 2)
        ans += d / math.factorial(N)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
import math

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

root = {}
for i in range(n-1):
    ix, iy = xy[i][0], xy[i][1]
    for j in range(i+1, n):
        jx, jy = xy[j][0], xy[j][1]
        root[(i, j)] = ((ix-jx)**2+(iy-jy)**2)**(1/2)

long = 0
for i in itertools.permutations([i for i in range(n)]):
    for j in range(1, len(i)):
        long += root[tuple(sorted([i[j-1], i[j]]))]
print(long/math.factorial(n))

Submission status: Accepted

Solution code:
from itertools import combinations

import math
N = int(input())

x = []
y = []

for i in range(N):
    _x, _y = map(int, input().split())
    x.append(_x)
    y.append(_y)
    
s = 0
for c in combinations(zip(x, y), 2):
    s += math.sqrt((c[0][0] - c[1][0]) ** 2 + (c[0][1] - c[1][1]) ** 2)

print(s * 2 / N)

Submission status: Accepted

Solution code:
import math
import itertools

N = int(input())
L = []
cnt = 0

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

for i in itertools.permutations(L,N):
    for j in range(len(i)-1):
        cnt += math.sqrt((i[j][0] - i[j+1][0])**2 + (i[j][1] - i[j+1][1])**2)

print(cnt / math.factorial(N))

Submission status: Accepted

Solution code:
import math
n = int(input())
nn = 1
xl = []
yl = []
ll = 0
for i in range(n):
    x,y = map(int, input().split())
    xl.append(x)
    yl.append(y)
 
j = 0
while j < n-1:
    k = j+1
    while k <= n-1:
        ll += math.sqrt((xl[j]-xl[k])**2 + (yl[j] - yl[k])**2)
        k += 1
    j += 1
 
print(ll*2/n)

Submission status: Accepted

Solution code:
from itertools import permutations

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

Submission status: Accepted

Solution code:
import math

def distance(a, b):
    return math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)

def recursive(pos):
    if len(pos) == 1:
        return 0
    dist = 0
    for p in range(len(pos) - 1):
        dist += distance(pos[0], pos[p + 1])
    dist += recursive(pos[1:])
    return dist

N = int(input())
pos = []
for i in range(int(N)):
    p = input().split(" ")
    pos.append([int(p[0]), int(p[1])])

print(recursive(pos) * 2 / N)

Submission status: Accepted

Solution code:
N = int(input())
def length(a,b):
    return ((a[0]-b[0])**2 + (a[1]-b[1])**2)**(1/2)
p = [list(map(int,input().split())) for _ in range(N)]
ans = 0
for i in range(N-1):
    for j in range(i+1,N):
        ans += length(p[i],p[j])
print(ans*2/N)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
import itertools
import math

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

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

for v in itertools.permutations(l, n):
    dist=0
    for j in range(len(v)-1):
        dist+=math.sqrt(math.pow((point[v[j]][0]-point[v[j+1]][0]),2)+math.pow((point[v[j]][1]-point[v[j+1]][1]),2))
    ans.append(dist)
print(sum(ans)/len(ans))

Submission status: Accepted

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

import math
import itertools

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

distance = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
    for j in range(i+1, n):
        dist_i_j = math.sqrt((x[i]-x[j])**2 + (y[i]-y[j])**2)
        distance[i][j] = dist_i_j
        distance[j][i] = dist_i_j

p = itertools.permutations(range(n))
tmp_n = n
num_p = 1
while tmp_n > 1:
    num_p *= tmp_n
    tmp_n -= 1
res = 0
for perm in p:
    for i in range(n-1):
        dist = distance[perm[i]][perm[i+1]]
        res += dist / num_p

print(res)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed Sep  9 02:04:12 2020

@author: liang
"""
import math

N = int(input())
d = list()
for i in range(N):
    x, y = map(int,input().split())
    d.append((x,y))
#print(d)
lis = list(range(N))
num = list()
perm_list = list()

def make_perm():
    if len(num) == N:
        perm_list.append(num.copy())
    for n in lis:
        a = lis.pop(0)
        num.append(a)
        make_perm()
        num.pop()
        lis.append(a)
        
make_perm()
#print(len(perm_list))
res = 0
for nums in perm_list:
    tmp = 0
    for i in range(N-1):
        tmp += math.sqrt((d[nums[i]][0]-d[nums[i+1]][0])**2 + (d[nums[i]][1] - d[nums[i+1]][1])**2)
    res += tmp
print(res/len(perm_list))

Submission status: Accepted

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

# ABC-150-Cの類題
# N!通りの経路を全探索し、各経路の長さを計算し、その平均値を出力する
# 順列組み合わせの生成には、Pythonならitertoolsが使える！
# O(N!N)だが、Nが十分小さいので間に合う
avg = 0
permutations = list(itertools.permutations(range(N)))
for l in permutations:
  for i in range(len(l)-1):
    avg += math.sqrt((xy[l[i+1]][0] - xy[l[i]][0])**2 + (xy[l[i+1]][1] - xy[l[i]][1])**2)

avg /= len(permutations)
print(avg)

Submission status: Accepted

Solution code:
import itertools
import math
 
N = int(input())
xy = [list(map(int, input().split())) for _ in range(N)]
 
avg = 0
permutations = list(itertools.permutations(range(N)))
for l in permutations:
  tmp = 0
  for i in range(len(l)-1):
    tmp += math.sqrt((xy[l[i+1]][0] - xy[l[i]][0])**2 + (xy[l[i+1]][1] - xy[l[i]][1])**2)
  avg += tmp

avg /= len(permutations)
print(avg)

Submission status: Accepted

Solution code:
import math
n = int(input())
nn = 1
xl = []
yl = []
ll = 0
for i in range(n):
    x,y = map(int, input().split())
    xl.append(x)
    yl.append(y)

j = 0
while j < n-1:
    k = j+1
    while k <= n-1:
        ll += math.sqrt((xl[j]-xl[k])**2 + (yl[j] - yl[k])**2)
        k += 1
    j += 1

print(ll*2/n)

Submission status: Accepted

Solution code:
import math
import itertools

# 与えられた数値の桁数と桁値の総和を計算する.
def calc_digit_sum(num):
    digits = sums = 0
    while num > 0:
        digits += 1
        sums += num % 10
        num //= 10
    return digits, sums


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

total = 0
routes = list(itertools.permutations(range(n), n))
for route in routes:
    distance = 0
    for index in range(len(route)-1):
        idx1 = route[index]
        idx2 = route[index+1]
        distance += math.sqrt((distances[idx1][0] - distances[idx2][0]) ** 2 + (distances[idx1][1] - distances[idx2][1]) ** 2)
    total += distance

print(total / len(routes))

Submission status: Accepted

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

sm=0

for i in itertools.permutations(A):
    for j in range(1,N):
        sm+=(abs(i[j-1][0]-i[j][0])**2+abs(i[j-1][1]-i[j][1])**2)**0.5

print(sm/math.factorial(N))

Submission status: Accepted

Solution code:
def main():
    import sys
    readline = sys.stdin.buffer.readline

    import math
    import itertools

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

    c = list(itertools.combinations(range(n), 2))
    ans = 0
    for a, b in c:
        ans += ((xy[a][0] - xy[b][0])**2 + (xy[a][1] - xy[b][1])**2)**(1/2)
    print(ans*2*math.factorial(n-1)/math.factorial(n))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
from decimal import Decimal
import itertools

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

Submission status: Accepted

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

def dfs(current, dist, already):
    if len(already) == N:
        return dist
    d = 0
    for i in range(N):
        if i in already:
            continue
        d += dfs(i, dist + math.sqrt((town[current][0] - town[i][0]) ** 2 + (town[current][1] - town[i][1]) ** 2), already | set([i]))
    return d
answer = 0
for i in range(N):
    answer += dfs(i, 0, set([i]))
print(answer / math.factorial(N))

Submission status: Accepted

Solution code:
import math
def dis(x1,y1,x2,y2):
    return(((x1-x2)**2+(y1-y2)**2)**(1/2))

N = int(input())
cord = []
tem = 0

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

for i in range(N):
    x1,y1 = cord[i]
    for j in range(0,i):
        x2,y2 = cord[j]
        tem += dis(x1,y1,x2,y2)    
    for j in range(i+1,N):
        x2,y2 = cord[j]
        tem += dis(x1,y1,x2,y2)

print(tem*math.factorial(N-1)/math.factorial(N))

Submission status: Accepted

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

import math

def solver(visited, coord, depth = 0):
  dists = []

  if len(visited) == depth:
    for i in range(1, len(visited)):
      x1, y1 = coord[visited[i - 1]][0], coord[visited[i - 1]][1]
      x2, y2 = coord[visited[i]][0], coord[visited[i]][1]
      dists.append(math.sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)))
    return sum(dists)

  for i in range(0, len(visited)):
    if visited[i] != -1: continue
    visited[i] = depth
    dists.append(solver(visited, coord, depth + 1))
    visited[i] = -1;

  return sum(dists)

def main():
  n = int(input())
  coord = []
  for i in range(0, n):
    coord.append([int(c) for c in input().split()])

  print(solver([-1 for i in range(0, n)], coord) / math.factorial(n))

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
N = int(input())
ct = []
 
for i in range(N):
  ct.append([int(i) for i in input().split()])
 
dist = 0
for a in ct:
  for b in ct:
    dist += (abs(a[0] - b[0])**2 +abs(a[1] - b[1])**2)**0.5
print(dist/N)

Submission status: Accepted

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

from itertools import permutations
import math
m = math.factorial(n)
per = permutations(xy,n)

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

print(d/m)

Submission status: Accepted

Solution code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

import itertools, math

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

def do():
    xs=[]
    ys=[]
    per=[]
    n=INT()
    ans=0
    for i in range(n):
        x,y=INTM()
        xs.append(x)
        ys.append(y)
        per.append(i)
    
    for i in itertools.permutations(per, n):
        for k in range(n-1):
            ans+=distance(xs[i[k]],ys[i[k]],xs[i[k+1]],ys[i[k+1]])
        #print(ans)
    
    print(ans/math.factorial(n))

    
if __name__=='__main__':
    do()

Submission status: Accepted

Solution code:
import math
N = int(input())
#https://qiita.com/jamjamjam888/items/e066b8c7bc85487c0785
xy = [map(int, input().split()) for _ in range(N)]
x, y = [list(i) for i in zip(*xy)]
l = 0

for j in range(N):
    for k in range(N):
        if j >= k:
            continue
        l += math.sqrt((x[j] - x[k])**2 + (y[j] - y[k]) ** 2)

print(l * 2/N)