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 collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction





def main():
    num = int(input())
    data = [list(map(int, input().split())) for i in range(num)]

    ans = 0
    count = 0
    for now_data in itertools.permutations([i for i in range(num)]):
        for i in range(num - 1):
            now_index = now_data.index(i)
            next_index = now_data.index(i + 1)
            dist = ((data[now_index][0] - data[next_index][0]) ** 2 + (data[now_index][1] - data[next_index][1]) ** 2) ** 0.5
            ans += dist
        count += 1
    ans /= count
    print(ans)





if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
import math

n = int(input())
pos = []
for i in range(n):
  pos.append(tuple(map(int, input().split())))
l = list(itertools.permutations(pos))
sum = 0
for i in l:
  for j in range(len(i)-1):
    sum += math.sqrt((i[j][0] - i[j+1][0])**2 + (i[j][1] - i[j+1][1])**2)
print(sum / len(l))

Submission status: Accepted

Solution code:
import itertools
import math

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

s = 0
c = list(itertools.permutations(p, n))
for v in c:
    for i in range(n - 1):
        s += math.sqrt((v[i + 1][0] - v[i][0]) ** 2 + (v[i + 1][1] - v[i][1]) ** 2)
print(s * 1.0 / len(c))

Submission status: Accepted

Solution code:
import sys
import itertools
import math
input = sys.stdin.readline

def slove():
    n = int(input())
    P = [tuple(map(int,input().split())) for _ in range(n)]
    #print(P)
    
    count = 0
    total_distance = 0
    for item in itertools.permutations(range(n)):
        #print(item)
        count += 1
        for i in range(n-1):
            a = item[i]
            b = item[i+1]

            total_distance += math.sqrt((P[b][0]-P[a][0])**2+(P[b][1]-P[a][1])**2)
    print(total_distance/count)


if __name__ == "__main__":
    slove()

Submission status: Accepted

Solution code:
import itertools

n = int(input())
X = []
ans = 0
for i in range(n):
  X.append(tuple(map(int,input().split())))
ls = list(itertools.permutations(X))
for v in ls:
  for j in range(len(X)-1):
    ans += ((v[j][0] - v[j+1][0])**2 + (v[j][1] - v[j+1][1])**2)**0.5
print(ans/len(ls))

Submission status: Accepted

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

Submission status: Accepted

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

N=int(input())
towns=[tuple(map(int,input().split())) for _ in range(N)]
dis_town=[[0]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        dis_town[i][j]=distance(towns[i],towns[j])
combs=list(itertools.permutations(range(N)))
total=0
for comb in combs:
    for i in range(N-1):
        total+=dis_town[comb[i]][comb[i+1]]

print(total/len(combs))

Submission status: Accepted

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

N=int(input())
towns=[tuple(map(int,input().split())) for _ in range(N)]
ave=0
for i in range(N-1):
    for j in range(i,N):
        ave+=distance(towns[i],towns[j])
ave=ave/(N*(N-1)/2)
print(ave*(N-1))

Submission status: Accepted

Solution code:
n = int(input())
X = []
Y = []
ans = 0
for _ in range(n):
    x,y = map(int,input().split())
    X.append(x)
    Y.append(y)
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:
from itertools import permutations
from math import factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# 座標 a から b の距離を求める
def calc(a, b):
    x1, y1 = a
    x2, y2 = b
    return ((x1 - x2)** 2 + (y1 - y2)** 2)** 0.5


n = int(readline())
xy = [list(map(int, readline().split())) for _ in range(n)]
l = [i for i in range(n)]
ans = 0
for v in permutations(l):
    for i in range(n-1):
        ans += calc(xy[v[i]],xy[v[i+1]])

ans /= len(list(permutations(l)))
print(ans)

Submission status: Accepted

Solution code:
import math
import itertools

n = int(input())
xy = [list(map(int,input().split())) for _ in range(n)]
seq = [i for i in range(n)]
p = list(itertools.permutations(seq))
dist = 0
for i in p:
    for j in range(n-1):
        dist += math.sqrt((xy[i[j]][0] - xy[i[j+1]][0])**2 + (xy[i[j]][1] - xy[i[j+1]][1])**2)
print(dist/math.factorial(n))

Submission status: Accepted

Solution code:
import math

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 += math.sqrt((a[i][0]-a[j][0])**2+(a[i][1]-a[j][1])**2)/n
    
print(ans)

Submission status: Accepted

Solution code:
N=int(input())

import itertools
li_c=list(itertools.permutations(range(N),2))

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

import math
distance=[]
for j in range(len(li_c)):
    distance.append(math.sqrt((rocation[li_c[j][0]][0]-rocation[li_c[j][1]][0])**2+(rocation[li_c[j][0]][1]-rocation[li_c[j][1]][1])**2))

print(sum(distance)/N)

Submission status: Accepted

Solution code:
import itertools
import math
# import decimal import *

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

#idList = [i for i in range(1, n + 1)]
idList = [i for i in range(n)]

com = list(itertools.combinations(idList, 2))
# print(com)

ans = 0

for i, j in com:
    tar1 = data[i]
    tar2 = data[j]

    add = (tar1[0] - tar2[0]) ** 2 + (tar1[1] - tar2[1]) ** 2
    ans += math.sqrt(add)

ans = ans * 2 / float(n)
#ans = decimal(ans / float(n))

print(ans)

Submission status: Accepted

Solution code:
#ABC145C
def main():
    import sys, itertools as ite
    import math
    N = int(sys.stdin.readline())
    XY = [tuple(map(int, sys.stdin.readline().split())) for _ in range(N)]
    #print(XY)
    P = tuple(ite.permutations([i for i in range(N)]))
    #print(P)
    
    def d_calc(A, B):
        return math.sqrt((A[0]-B[0])**2 + (A[1]-B[1])**2)
    
    ans = 0
    for i in range(len(P)):
        for j in range(N-1):
            ans += d_calc(XY[P[i][j+1]], XY[P[i][j]])
    print(ans/len(P))
  

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import math
import itertools

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

data=[]
for case in itertools.permutations(range(n)):
  dist = 0.0
  for i in range(n-1):
    x1,y1=plot[case[i]]
    x2,y2=plot[case[i+1]]
    dist += math.sqrt((x1-x2)**2+(y1-y2)**2)
  data.append(dist)
print(sum(data)/len(data))

Submission status: Accepted

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

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

Submission status: Accepted

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

import itertools
import math
seq = range(n)
p=list(itertools.permutations(seq))

dis=0
for index,item in enumerate(p):
  for i in range(n-1):
    dis_tmp = 0
    dis_tmp += ( xy[item[i+1]][0] - xy[item[i]][0] ) ** 2
    dis_tmp += ( xy[item[i+1]][1] - xy[item[i]][1] ) ** 2
    dis += math.sqrt(dis_tmp)
dis /= (len(p))
print(dis)

Submission status: Accepted

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

total=0
for i in range(N-1):
    for j in range(i+1,N):
        total+=2*math.factorial(N-1)*math.sqrt((List[j][0]-List[i][0])**2+(List[j][1]-List[i][1])**2)

average=(total/(math.factorial(N)))
print(average)

Submission status: Accepted

Solution code:
import itertools

N = int(input())
# n週入力させる
P = [list(map(int,input().split())) for i in range(N)]

S = 0
# 3!
# pから2つ選ぶ組み合わせ
route = list(itertools.combinations(P, 2))
# 例題の出力結果
# [([0, 0], [1, 0]), ([0, 0], [0, 1]), ([1, 0], [0, 1])]
# 6通り全部計算式に当てはめる 2-1,3-2 3-2,4-3
for i in route:
  #  指定の計算式
   S += ((i[0][0] - i[1][0])**2 + (i[0][1] - i[1][1])**2)**0.5
   
print(S*2/N)

Submission status: Accepted

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

import itertools
all_route=list(itertools.permutations(xy))

length=0
for route in all_route:
    i=1
    while i<N:
        length+=((route[i][0]-route[i-1][0])**2+(route[i][1]-route[i-1][1])**2)**0.5
        i+=1

import math
print(length/math.factorial(N))

Submission status: Accepted

Solution code:
from math import sqrt, factorial

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

distance = 0
for i in range(N):
    for j in range(N):
        distance += sqrt((x[j][0]-x[i][0])**2 + (x[j][1]-x[i][1])**2)

print(distance/N)

Submission status: Accepted

Solution code:
n = int(input())

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

def kyori(a, b): # 2座標間の距離を求める関数
    return ((a[0]-b[0])**2 + (a[1]-b[1])**2)**0.5

soukyori = 0
for j in range(n):
    for k in range(j, n):
        soukyori = soukyori + kyori(l[j],l[k])
#print(soukyori)

print(soukyori * 2 / n) # ?

Submission status: Accepted

Solution code:
import sys
import numpy as np
import math
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
xy = []
for _ in range(N):
  x,y = map(int, readline().split())
  xy.append((x,y))
sum = 0
for i in range(N):
  for j in range(i+1,N):
    x1,y1 = xy[i]
    x2,y2 = xy[j]
    dist = math.sqrt((x1-x2)**2 + (y1-y2)**2)
    sum += dist
ans = 2*sum/N
print('{:.7f}'.format(ans))

Submission status: Accepted

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

#S = [[0 for j in range(3)] for i in range(2)]　2行3列の場合
S = [[0 for j in range(2)] for i in range(N)]
for i in range(N):
    X,Y=map(int,input().split())
    S[i][0]=X
    S[i][1]=Y
    

goukei=0

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


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


D=list(itertools.permutations(T))
for Z in D:

    for j in range(N-1):
        goukei+=math.sqrt((S[Z[j]][0]-S[Z[j+1]][0])**2+(S[Z[j]][1]-S[Z[j+1]][1])**2)
print(goukei/divide)

Submission status: Accepted

Solution code:
import itertools
import math


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

distance = 0
for c in itertools.combinations(range(N), 2):
    c1, c2 = c
    distance += ((cities[c1][0] - cities[c2][0]) ** 2 + (cities[c1][1] - cities[c2][1]) ** 2) **0.5

answer = 2 * distance * (N-1) * math.factorial(N-2) / math.factorial(N)

print(answer)

Submission status: Accepted

Solution code:
## C - Average Length
import math
import itertools
N = int(input())
X = [0] * N
Y = [0] * N
L = [n for n in range(N)]

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

dist = 0
cnt = 0
for i in itertools.permutations(L, N):
    cnt += 1
    for n in range(1,N):
        dist += math.sqrt( (X[i[n]]-X[i[n-1]]) ** 2 + (Y[i[n]]-Y[i[n-1]]) ** 2 )
print(dist / cnt)

Submission status: Accepted

Solution code:
import math
from itertools import permutations

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

all_routes = list(permutations(town_index))
for root in all_routes:
    length = 0
    for i in range(1, N):
        length += math.sqrt((town[root[i-1]][0] - town[root[i]][0]) ** 2 + (town[root[i-1]][1] - town[root[i]][1]) ** 2)
    sum_length += length

print(sum_length / len(all_routes))

Submission status: Accepted

Solution code:
import itertools

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

ans = 0
cnt = 0
for pat in itertools.permutations(zahyo):
    cnt += 1
    for i in range(len(pat)-1):
        distance = ((pat[i][0]-pat[i+1][0])**2 + (pat[i][1]-pat[i+1][1])**2)**(1/2)
        ans += distance
print(ans/cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
C - Average Length
https://atcoder.jp/contests/abc145/tasks/abc145_c

"""
import sys


from itertools import permutations
from statistics import mean

def solve(N, points):
    dists = []
    for p in permutations(points):
        x = 0.0
        for a, b in zip(p, p[1:]):
            x += ((a[0]-b[0])**2 + (a[1]-b[1])**2)**0.5
        dists.append(x)
    return mean(dists)


def main(args):
    N = int(input())
    points = [list(map(int, input().split())) for _ in range(N)]
    ans = solve(N, points)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
import math
N = int(input())
x, y = [], []
for i in range(N):
  tmp = list(map(int, input().split()))
  x.append(tmp[0])
  y.append(tmp[1])
  
num = 1
for i in range(N, 0, -1):
  num *= i
total1 = num*(N-1)
total2 = N*(N-1)/2
b = total1/total2

avg = 0
for i in range(0,N-1,1):
  for j in range(i+1,N,1):
    avg += math.sqrt((x[i]-x[j])**2+(y[i]-y[j])**2)*b
avg /= num
print(avg)

Submission status: Accepted

Solution code:
import itertools
import math

def calc(xy1, xy2):
    return (math.sqrt((xy1[0]-xy2[0])**2 + (xy1[1]-xy2[1])**2))

N = int(input())

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

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

ans = 0
for i in p_list:
    for j in range(1, len(i)):
        ans += calc(x_y[i[j]], x_y[i[j-1]])

print(ans/math.factorial(N))

Submission status: Accepted

Solution code:
import math
import numpy as np
N =int(input())

a = np.zeros((N, 2))
kyori = np.zeros(int(N*(N-1)/2))

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


count=0
for i in range(0,N-1):
    for j in range(i+1,N):
     kyori[count]=math.sqrt((a[i][0]-a[j][0])**2+(a[i][1]-a[j][1])**2) 
     count=count+1
  

print(2*sum(kyori)*(N-1)*math.factorial(N-2)/math.factorial(N))

Submission status: Accepted

Solution code:
import itertools
import math
#import numpy as np
N = int(input())

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

loots = list(itertools.permutations(range(1,N+1)))
#print(len(loots))

goukei = 0
for loot in loots:
#    print(loot)
    for index, value in enumerate(loot[1:N]):
#        print(xys[loot[index]-1][0], xys[loot[index-1]-1][0],"\n",xys[loot[index]-1][1], xys[loot[index-1]-1][1])
        goukei += math.sqrt((xys[loot[index]-1][0] - xys[loot[index-1]-1][0]) ** 2 + (xys[loot[index]-1][1] - xys[loot[index-1]-1][1]) ** 2)
#        print(xys[1][0] ,xys[loot[index-1]-1][0])
#    print(goukei)


print(goukei / len(loots))

Submission status: Accepted

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

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

import itertools
import math
per=list(itertools.permutations(xy))
#print(per)
N=math.factorial(n)
ans=0
for i in range(N):
    sum=0
    p=per[i]
    for j in range(n):
        if j!=n-1:
            sum+=distance(p[j][0],p[j][1],p[j+1][0],p[j+1][1])
    ans+=sum
print(ans/N)

Submission status: Accepted

Solution code:
import math
import itertools
 
N = int(input())
XY = [list(map(int, input().split())) for x in range(N)]
 
P = list(itertools.permutations(XY, N))
 
total = 0
for p in P:
    for i in range(1, N):
        a = p[i - 1]
        b = p[i]
        total += math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
        
ans = total / len(P)
print(ans)

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

all_sum = 0

for i in permutations(range(N)):
    for j in range(len(i) - 1):
        all_sum += sqrt((xy[i[j]][0] - xy[i[j + 1]][0]) ** 2 +
                        (xy[i[j]][1] - xy[i[j + 1]][1]) ** 2)


print(all_sum / factorial(N))

Submission status: Accepted

Solution code:
from math import sqrt, factorial

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

all_sum = 0

for i in range(N):
    xi, yi = xy[i]
    for j in range(i + 1, N):
        xj, yj = xy[j]
        all_sum += sqrt((xi - xj) ** 2 + (yi - yj) ** 2)

fN = factorial(N - 1)

all_sum /= fN * N
all_sum *= fN * 2

print(all_sum)

Submission status: Accepted

Solution code:
from math import sqrt, factorial

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

all_sum = 0

for i in range(N):
    xi, yi = xy[i]
    for j in range(i + 1, N):
        xj, yj = xy[j]
        all_sum += sqrt((xi - xj) ** 2 + (yi - yj) ** 2)

fN = factorial(N)

all_sum /= fN
all_sum *= factorial(N - 1) * 2

print(all_sum)

Submission status: Accepted

Solution code:
import math
import itertools

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

P = list(itertools.permutations(XY, N))

total = 0
for p in P:
  for i in range(1, N):
    a = p[i-1]
    b = p[i]
    total += math.sqrt((a[0]-b[0])**2+(a[1]-b[1])**2)

print(total / len(P))

Submission status: Accepted

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

res = 0
cnt = 0
for pair in list(permutations(range(N), N)):
  for i in range(N):
    if i+1 == N: continue
    x,y = pair[i], pair[i+1]
    x_y = math.sqrt( (C[x][0]-C[y][0])**2 + (C[x][1]-C[y][1])**2 )
    res += x_y
  cnt += 1

print(res / cnt)

Submission status: Accepted

Solution code:
from itertools import permutations
from math import factorial, hypot

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

ans = 0
for ps in permutations(p, n):
    x1, y1 = ps[0]
    for i in range(n):
        x2, y2 = ps[i]
        ans += hypot(x1-x2, y1-y2)
        x1, y1 = x2, y2
ans /= factorial(n)
print(ans)

Submission status: Accepted

Solution code:
import itertools
import math
def dist(i, j):
    return math.sqrt((coords[i][0] - coords[j][0]) ** 2 + (coords[i][1] - coords[j][1]) ** 2)

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

ans = []
for item in itertools.permutations(range(n)):
    length = 0
    for i in range(n-1):
        length += dist(item[i], item[i+1])
    ans.append(length)
print(sum(ans) / len(ans))

Submission status: Accepted

Solution code:
from itertools import permutations
from math import factorial

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

def dist(a, b):
   x1, y1 = a
   x2, y2 = b
   return pow((x1-x2) ** 2 + (y1-y2) ** 2, 1/2)

res = 0
for i in range(N):
   for j in range(i+1, N):
      res += dist(coord[i], coord[j]) * 2 / N

print(res)

Submission status: Accepted

Solution code:
from itertools import permutations
from math import factorial

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

def dist(a, b):
   x1, y1 = a
   x2, y2 = b
   return pow((x1-x2) ** 2 + (y1-y2) ** 2, 1/2)

res = 0
div = factorial(N)
idx = [i for i in range(N)]
for line in permutations(idx, N):
   for i in range(N-1):
      res += dist(coord[line[i]], coord[line[i+1]]) / div

print(res)

Submission status: Accepted

Solution code:
import itertools

n = int(input())
X = []
for _ in range(n):
    X.append(list(map(int, input().split())))
L = 0
count = 0
for xl in itertools.permutations(X):
    l = 0
    pre = []
    for x in xl:
        if len(pre) > 0:
            l += ((x[0] - pre[0]) ** 2 + (x[1] - pre[1]) ** 2) ** (1/2)
        pre = x
    L += l
    count += 1

print(L / count)

Submission status: Accepted

Solution code:
# ABC145C

import itertools
import math

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

lensum=0
for i in itertools.permutations(r):
    for j in range(N-1):
        length=((i[j][0]-i[j+1][0])**2+(i[j][1]-i[j+1][1])**2)**0.5
        lensum+=length
        
print(lensum/math.factorial(N))

Submission status: Accepted

Solution code:
import itertools
import math

N = int(input())
permutation = list(itertools.permutations([x for x in range(1, N + 1)]))
coordinate = [list(map(int, input().split())) for _ in range(N)]
distances = []

for one_case in permutation:
    sum_dis = 0
    for j in range(N - 1):
        dis = math.sqrt((coordinate[one_case[j] - 1][0] - coordinate[one_case[j + 1] - 1][
            0]) ** 2 + (coordinate[one_case[j] - 1][1] - coordinate[one_case[j + 1] - 1][
            1]) ** 2)
        sum_dis += dis
    distances.append(sum_dis)
print('{:.7f}'.format(sum(distances) / len(permutation)))

Submission status: Accepted

Solution code:
import itertools
N=int(input())
retu=[i for i in range(N)]
XY=[]
for i in range(N):
    x=list(map(int,input().split()))
    XY.append(x)
permi=list(itertools.permutations(retu))
goukei=0
count=0
for i in permi:
    count+=1
    for k in range(1,N):
        a=XY[i[k - 1]][0]- XY[i[k]][0]
        b=XY[i[k - 1]][1]- XY[i[k]][1]
        goukei+=(a**2+b**2)**0.5
print(goukei/count)

Submission status: Accepted

Solution code:
import itertools
import numpy as np
N=int(input())
retu=[i for i in range(N)]
XY=[]
for i in range(N):
    x=list(map(int,input().split()))
    XY.append(x)
permi=list(itertools.permutations(retu))
goukei=0
count=0
for i in permi:
    count+=1
    for k in range(1,N):
        a=XY[i[k - 1]][0]- XY[i[k]][0]
        b=XY[i[k - 1]][1]- XY[i[k]][1]
        goukei+=(a**2+b**2)**0.5
print(goukei/count)

Submission status: Accepted

Solution code:
import numpy as np
import itertools
import math

n = int(input())

towns = []

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

towns = np.array(towns)

l = np.zeros((n, n))

for i in range(n):
    for j in range(n):
        l[i, j] = np.sum((towns[i] - towns[j])**2) ** 0.5

ans = 0
for order in itertools.permutations(range(n), n):
    now = order[0]
    f = 0
    for i in order[1:]:
        f += l[now, i]
        now = i
    ans += f
print(ans/math.factorial(n))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
# C = [int(i) for i in input().split()]
town = []
for i in range(N):
    x, y = [int(_) for _ in input().split()]
    town.append([i, x, y])

T = itertools.combinations(town, 2)
d_sum = 0
for t in T:
    xi = t[0][1]
    yi = t[0][2]
    xj = t[1][1]
    yj = t[1][2]
    d = ((xi - xj)**2 + (yi - yj)**2)**0.5
    d_sum += d

print(d_sum*2/N)

Submission status: Accepted

Solution code:
from itertools import permutations as perm


def euclidean_distance(list1, list2):
    x_norm = (list1[0] - list2[0]) ** 2
    y_norm = (list1[1] - list2[1]) ** 2
    return (x_norm + y_norm) ** 0.5


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

    orders = list(perm([xys[i] for i in range(n)], n))
    order_num = len(orders)
    ans = 0
    for order in orders:
        for i in range(n-1):
            ans += euclidean_distance(order[i], order[i+1])

    print(ans/order_num)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# For taking integer inputs.
import math
from decimal import Decimal
from itertools import permutations


def inp():
    return(int(input()))


# For taking List inputs.
def inlist():
    return(list(map(int, input().split())))


# For taking string inputs. Actually it returns a List of Characters, instead of a string, which is easier to use in Python, because in Python, Strings are Immutable.
def instr():
    s = input()
    return(list(s[:len(s)]))


# For taking space seperated integer variable inputs.
def invr():
    return(map(int, input().split()))


def fact(n):
    if n == 0:
        return 1
    return fact(n-1) * n


N = inp()
points = []
p = []
for i in range(N):
    p.append(i)
    points.append(inlist())
D = [[0 for i in range(N)] for j in range(100)]
c = 0
for i in range(N-1):
    for j in range(i+1, N):
        x1, y1 = points[i]
        x2, y2 = points[j]
        #print("p ", points[i], points[j])
        d = math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
        D[i][j] = d
        D[j][i] = d
# print(D)
permut = list(permutations(p))

res = 0
for pt in permut:
    for i in range(N-1):
        res += D[pt[i]][pt[i+1]]
print(res / fact(N))

Submission status: Accepted

Solution code:
#ABC145C
def main():
    import sys, math
    import itertools as ite
    N = int(sys.stdin.readline())
    L = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
    #print(L)
    
    A = list(ite.permutations([i for i in range(N)]))
    #print(A)
    ans = 0
    for a in A:
        for i in range(N-1):
            ans += math.sqrt((L[a[i]][0]- L[a[i+1]][0])**2 + (L[a[i]][1]- L[a[i+1]][1])**2)
    print(ans/len(A))
            





if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import permutations

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

def dist(a, b):
  dx = x[a] - x[b]
  dy = y[a] - y[b]
  return (dx**2 + dy**2)**0.5

ans = 0
np = [x for x in range(n)]
p = list(permutations(np))
for a in p:
  memo = 0
  for i in range(n-1):
    memo += dist(a[i],a[i+1])
  ans += memo
print(ans / len(p))

Submission status: Accepted

Solution code:
import itertools
from math import sqrt,factorial
n=int(input())
x=[]
y=[]
for i in range(n):
    xi,yi=map(int,input().split())
    x.append(xi)
    y.append(yi)
lis = [x for x in range(n)] # 0からn-1までのリスト
ans=0
permutations_lis = itertools.permutations(lis)# 全ての場合のリストを生成
# 以下出力
for one_case in permutations_lis: 
  for i in range(n-1):
      xi=x[one_case[i]]
      yi=y[one_case[i]]
      xj=x[one_case[i+1]]
      yj=y[one_case[i+1]]
      ans+=sqrt((xi-xj)**2+(yi-yj)**2)
ans=ans/factorial(n)
print(ans)

Submission status: Accepted

Solution code:
from math import factorial
from itertools import permutations

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

dist_sum = 0
for route in permutations(range(n), n):
    for j in range(1, n):
        dist = (x[route[j]] - x[route[j - 1]]) ** 2
        dist += (y[route[j]] - y[route[j - 1]]) ** 2
        dist = dist ** 0.5
        dist_sum += dist
ans = dist_sum / factorial(n)
print(ans)

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
box = []
for _ in range(n):
    x,y = map(int,input().split())
    box.append([x,y])
ans = 0
ans_box = list(permutations(range(n)))
cnt = len(ans_box)
for i in range(cnt):
    for j in range(n-1):
        ans += (((box[ans_box[i][j+1]][0] - box[ans_box[i][j]][0]) ** 2 + (box[ans_box[i][j+1]][1] - box[ans_box[i][j]][1]) ** 2 ) ** 0.5)

print(ans/cnt)

Submission status: Accepted

Solution code:
#abc 145 C
n = int(input())
coor = [] # 各町の座標
dis_ij = []# 各町の距離を格納

for i in range(n):
    x,y = map(int, input().split())
    coor.append((x,y))
    
def dis_2point(s,k):
    return ((s[0] -k[0])**2 + (s[1] - k[1])**2)**0.5

for i in range(n):
    for j in range(i):
        dis_ij.append(dis_2point(coor[i], coor[j]))
print(sum(dis_ij) * 2/n)

Submission status: Accepted

Solution code:
N=int(input())

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

print(res/N)

Submission status: Accepted

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

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

#print(ans)
print(ans*(n-1)/count)

Submission status: Accepted

Solution code:
from math import factorial,sqrt
from itertools import permutations

def dfs(s):
    pass

n = int(input())
v = []
x = 0
for _ in range(n):
    v.append(tuple(map(int,input().split())))
for t in permutations(v):
    z = 0
    for i in range(n-1):
        ti = t[i]
        tn = t[i+1]
        z+=sqrt((abs(ti[0]-tn[0])**2)+(abs(ti[1]-tn[1])**2))
    x+=z
print(x/factorial(n))

Submission status: Accepted

Solution code:
# 2138~
# アイディア 2140
# permutationを使って，N!通りの経路を実際に作ってそれらの経路を足して最終的にpermutation数で割れば良い
# 実装2200

N = int(input())

X=[]
Y=[]
for _ in range(N):
    xi,yi = map(int,input().split())
    X.append(xi)
    Y.append(yi)
XY = [[x,y] for x,y in zip(X,Y)]
import itertools

All = itertools.permutations(XY)
distance = 0
import math
for j,xy in enumerate(All):
    for i,xiyi in enumerate(xy):
        if i == 0:
            start = xiyi
            continue
        now = xiyi
        distance += math.sqrt((start[0]-now[0])**2+(start[1]-now[1])**2)
        start = now
    num = j
print(distance/(num+1))

Submission status: Accepted

Solution code:
# coding:utf-8
import itertools
import math


def distance(t1, t2):
    return(math.hypot(t1[0] - t2[0], t1[1] - t2[1]))


def main():

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

    for item in itertools.permutations(town, r=2):
        # print(*item)
        ans += distance(*item)

    print(ans / n)


if __name__ == "__main__":
    main()