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
from math import factorial
from math import sqrt
n=int(input())
l=[]
def distance(x,y):
  a=sqrt((x[0]-y[0])**2+(x[1]-y[1])**2)
  return a
def dist(l):
  d=0
  for i in range(1,n):
    d+=distance(l[i],l[i-1])
  return d
for i in range(n):
  l.append(list(map(int, input().split())))
x=list(permutations(l))
avg=0
for i in x:
    avg+=dist(i)
print(avg/factorial(n))

Submission status: Accepted

Solution code:
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import fractions
from functools import reduce
from operator import mul
import bisect
import sys
import logging
import heapq
import itertools

logging.basicConfig(level=logging.ERROR)

input = sys.stdin.readline

setrecursionlimit(1000000)


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

def factorization(n):
	if n<2:
		return []

	arr = []
	temp = n
	for i in range(2, int(-(-n**0.5//1))+1):
		if temp%i==0:
			cnt=0
			while temp%i==0:
				cnt+=1
				temp //= i
			arr.append([i, cnt])

	if temp!=1:
		arr.append([temp, 1])

	if arr==[]:
		arr.append([n, 1])

	return arr

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(numbers):
	return reduce(fractions.gcd, numbers)

def lcm(numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

class PriorityQueue():
	def __init__(self):
		self.queue = []
	def push(self,i):
		heapq.heappush(self.queue,-i)
	def pop(self):
		return -1 * heapq.heappop(self.queue)
	def sum(self):
		return -sum(self.queue)

main()

Submission status: Accepted

Solution code:
import math

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

n = int(input())
a = []
for i in range(n):
  t = list(map(int,input().split()))
  a.append(t)
tot=0
mul = factorical(n-1)
for i in range(n):
  for j in range(n):
    tot += (math.sqrt((a[i][0]-a[j][0])**2 + (a[i][1]-a[j][1])**2))*mul
print(tot/(mul*n))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
route = [x for x in range(n)]
t = [list(map(int,input().split())) for _ in range(n)]
route_list =  list(itertools.permutations(route,n))
l = len(route_list)
ans = 0

for i in range(l):
    for k in range(n - 1):
        now = route_list[i][k]
        nex = route_list[i][k + 1]
        ans += ((t[now][0] - t[nex][0]) ** 2 + (t[now][1] - t[nex][1]) ** 2) ** 0.5

print(ans / l)

Submission status: Accepted

Solution code:
import math
N=int(input())
ans=0
X=[0]*N
Y=[0]*N
leng=[]
for i in range(N):
    x,y=map(int,input().split())
    X[i]=x
    Y[i]=y
for i in range(N):
    for j in range(N):
        leng.append(((X[i]-X[j])**2+(Y[i]-Y[j])**2)**0.5)
ans=sum(leng)/N
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
import math

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

d = 0
for root in itertools.permutations(range(N)):
  for i in range(N-1):
    d_x = x[root[i+1]][0] - x[root[i]][0]
    d_y = x[root[i+1]][1] - x[root[i]][1]
    d   += math.sqrt(d_x**2 + d_y**2)

print(d / math.factorial(N))

Submission status: Accepted

Solution code:
import itertools
import numpy as np

num = int(input())
x = []
y = []
for i in range(num):
  x_N, y_N = map(int, input().split())
  x.append(x_N)
  y.append(y_N)

index = [str(i) for i in range(num)]
index_permut = list(itertools.permutations(index))

distance_all = 0
for l in index_permut:
  distance = 0
  index = list(map(int, l))

  for pos in range(num-1):
    x_diff = (x[index[pos+1]] - x[index[pos]]) ** 2
    y_diff = (y[index[pos+1]] - y[index[pos]]) ** 2
    distance += np.sqrt(x_diff + y_diff)

  distance_all += distance
res = distance_all / len(index_permut)
print(res)

Submission status: Accepted

Solution code:
import itertools
import numpy as np
import math

L = []
A = 0
t = 0
N = int(input())
for i in range(N):
    x, y = list(map(int, input().split()))
    L.append([x, y])
# print(L[1][0])
K = np.array(L)
for i in itertools.permutations(L):
    np.array(i)
    for k in range(N-1):
        u = math.sqrt((i[k][0]-i[k+1][0])**2+(i[k][1]-i[k+1][1])**2)
        A = A + u
    t += 1
B = A/t
print(B)

Submission status: Accepted

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

#全経路
all_way=[]
for i in itertools.permutations(x):
    all_way.append(list(i))

result=[]
for each_way in all_way:
    total=0
    for town in range(len(each_way)-1):
        total+=math.sqrt((each_way[town][0]-each_way[town+1][0])**2 + (each_way[town][1]-each_way[town+1][1])**2)
    result.append(total)

print(sum(result)/len(result))

Submission status: Accepted

Solution code:
import itertools
import math

def main():
    N = int(input())
    x = [list(map(int,input().split())) for _ in range(N)]
    d_all = 0
    for root in itertools.permutations(range(N)):
        d_x,d_y = 0,0
        for i in range(N-1):
            d_x += x[root[i+1]][0]-x[root[i]][0]
            d_y += x[root[i+1]][1]-x[root[i]][1]
            d_all += (d_x**2+d_y**2)**(1/2)
    
    all_ = math.factorial(N)     
    print(d_all/all_)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools


def distance(route_index: tuple, points: list):
	sum_distance = 0
	for i in range(len(route_index) - 1):
		p1_x = points[route_index[i]][0]
		p1_y = points[route_index[i]][1]

		p2_x = points[route_index[i + 1]][0]
		p2_y = points[route_index[i + 1]][1]

		X = p2_x - p1_x
		Y = p2_y - p1_y

		sum_distance += (X ** 2 + Y ** 2) ** (1 / 2)
	return sum_distance


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

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

sum_distance = 0
for route_index in route_index_list:
	sum_distance += distance(route_index, points)

print(sum_distance / len(route_index_list))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
array = []
for i in range(N):
  x,y = list(map(int,input().split()))
  array.append([x,y])
sum = 0
for j in range(N):
  for k in range(N):
    if j >= k:
      continue
    else:
      length = ((array[j][0]-array[k][0])**2 + (array[j][1]-array[k][1])**2)**(1/2)
      sum += length
print((2*sum)/N)

Submission status: Accepted

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

total = 0
for i in range(1,n):
    x0,y0 = lst[i][0],lst[i][1]
    for j in range(i):
        x1,y1 = lst[j][0],lst[j][1]
        total += ((x1 - x0)**2 + (y1 - y0)**2)**0.5
print(total*2/n)

Submission status: Accepted

Solution code:
import math
N = int(input())
l_x = []
l_y = []
s = 0
a = (math.factorial(N) * (N-1)) // (((N-1)*N)//2)
for i in range(N):
    x,y = map(int,input().split())
    l_x.append(x)
    l_y.append(y)
for i in range(N):
    for j in range (i+1,N):
        m = (l_x[i]-l_x[j])**2 + (l_y[i] - l_y[j])**2
        s += math.sqrt(m)*a
print(s / math.factorial(N))

Submission status: Accepted

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

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

total_distance = 0

for i in range(0, len(towns)):
    town1 = towns[i]
    for j in range(0, len(towns)):
        if i != j:
            town2 = towns[j]
            total_distance += distance(town1,town2)

avg_distance = (1/N)*total_distance
print(avg_distance)

Submission status: Accepted

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

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


Nbiku = math.factorial(N)
Nbikuhiku = math.factorial(N - 1) 

ans = 0
for index in range(N):
    for index2 in range(N):
        if index == index2:
            continue

        dx = x[index] - x[index2]
        dy = y[index] - y[index2]

        ans += (dx ** 2 + dy ** 2)** 0.5 * Nbikuhiku
        

print(ans / Nbiku)

Submission status: Accepted

Solution code:
N = input()

hoge = []
for i in range(1, int(N) + 1):
    x, y = input().split()
    hoge.append([x, y])

d = 0
for i in range(int(N)):
    for j in range(i, int(N)):
        d += (((int(hoge[i][0]) - int(hoge[j][0])) ** 2 + (int(hoge[i][1]) - int(hoge[j][1])) ** 2) ** (1 / 2))

print(d * (2/int(N)))

Submission status: Accepted

Solution code:

def dis(a):
  return ((a[0]-a[2])*(a[0]-a[2])+(a[1]-a[3])*(a[1]-a[3]))**0.5

n = int(input())
p=[]
for i in range(n):
  a,b = map(int,input().split())
  p.append([a,b])
d=[[0]*n for _ in range(n)]
s=0
for i in range(n):
  for j in range(i+1,n):
    g = dis(p[i]+p[j])
    d[i][j]=d[j][i]=g
    s+=2*g
print("%.7f"%(s/n))

Submission status: Accepted

Solution code:
import math
import itertools
 
N=int(input())
P=[]
all_dist=[]
 
def Len(X,Y):
    return math.sqrt((X[0]-Y[0])**2+(X[1]-Y[1])**2)

for _ in range(N):
    a,b=map(int,input().split())
    P.append([a,b])
#print('townmap_is',P)
 
route=list(itertools.permutations(P))
#print(route)
 
for k in range(math.factorial(N)):
    B=0
    A=list(route[k])
    for i in range(len(A)-1):
        B+=Len(A[i],A[i+1])
    all_dist.append(B)
ans=sum(all_dist)/len(all_dist)
 
print(str(ans))

Submission status: Accepted

Solution code:
from math import sqrt

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

def main():
    n = int(input())
    p_array = []
    s = 0
    for i in range(n):
        p = list(map(int, input().split(' ')))
        for q in p_array:
            s += distance(p, q)
        p_array.append(p)
    return 2*s/n

if __name__ == "__main__":
    print(main())

Submission status: Accepted

Solution code:


import numpy as np

import math


n = int(input())


a = []


sum = 0


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

for i in range(n):
    for s in range(i,n):
        sum += np.linalg.norm(a[s] - a[i])


print(sum*2/n)

Submission status: Accepted

Solution code:
#むちゃくちゃ大事
#街の数を受け取り、その経路の平均値を求めるプログラム


import numpy as np

import math

#街の数を受けとる
n = int(input())

#街の位置ベクトルを保存するためのリスト
a = []

#合計値
sum = 0

#街の一ベクトルを取得
for i in range(n):
    a.append(np.array(list(map(int,input().split()))))

for i in range(n):
    for s in range(i,n):
        sum += np.linalg.norm(a[s] - a[i])

#平均値を表示 sum : 経路の合計  math.factorial(n) : nの階乗
print(sum*2/n)

Submission status: Accepted

Solution code:
import numpy as np
N = int(raw_input())
P = np.empty([N,2], dtype=float)
for i in xrange(N):
    P[i] = map(float, raw_input().split())

Distance_matrix = np.empty([N,N], dtype=float)
for i in xrange(N):
    for j in xrange(i+1,N):
        temp = np.sqrt((P[i,0] - P[j,0])**2 + (P[i,1] - P[j,1]) ** 2)
        Distance_matrix[i,j] = Distance_matrix[j,i] = temp

from itertools import permutations
path = permutations(range(N), N)

length = []
for p in path:
    sum = 0
    for i in xrange(N-1):
        sum += Distance_matrix[p[i], p[i+1]]
    length.append(sum)

print "{:12.10f}".format(np.mean(length))

Submission status: Accepted

Solution code:
import itertools
import math

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

ans = 0
cnt = 0

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

Submission status: Accepted

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

l = 0
c = 0

for v in itertools.permutations(xy):
  for i in range(len(v)-1):
    l += ((v[i][0] - v[i+1][0])**2 + (v[i][1] - v[i+1][1])**2)**0.5
  c += 1

print(l/c)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
x = np.zeros((N, 1))
y = np.zeros((N, 1))
for n in range(N):
    x[n], y[n] = map(int, input().split())
dist_mat = np.sqrt((x - x.T) ** 2 + (y - y.T) ** 2)
print(np.mean(dist_mat * N))

Submission status: Accepted

Solution code:
def get_dist(p1, p2):
    x1, y1 = p1
    x2, y2 = p2
    return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5

def solve():
    from itertools import combinations
    from math import factorial
    from sys import stdin
    f_i = stdin
    
    N = int(f_i.readline())
    points = tuple(tuple(map(int, f_i.readline().split())) for i in range(N))
    s = sum(get_dist(p1, p2) for p1, p2 in combinations(points, 2))
    print(s * 2 / N)

solve()

Submission status: Accepted

Solution code:
import itertools

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

l=[i for i in range(N)]
dis=0
cnt=0
for v in itertools.permutations(l):
  #print(v)
  cnt+=1
  for i in range(1,N):
    dis+=((x[v[i]]-x[v[i-1]])**2+(y[v[i]]-y[v[i-1]])**2)**0.5

print(dis/cnt)

Submission status: Accepted

Solution code:
import math
from collections import deque

n = int(input())
ans = 0.0
town = []
length = [[0 for i in range(n)]for j in range(n)]
temp = []

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

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

queue = deque()

for i in range(n):
    queue.append([[i], 0.0])

while len(queue) != 0:
    left = queue.popleft()
    visited = left[0]
    now_length = left[1]
    for i in range(n):
        if i in visited:
            continue
        else:
            now_length = now_length + length[visited[len(visited) - 1]][i]
            for j in visited:
                temp.append(j)
            temp.append(i)
            if len(temp) == n:
                ans += now_length
            else:
                queue.append([temp, now_length])
            temp = []
            now_length = left[1]

print(ans / math.factorial(n))

Submission status: Accepted

Solution code:
import math

n = int(input().strip())
x_y = [[0] * 2] * n

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

den = math.factorial(n)
mol = 0

for i in range(n):
    for j in range(n):
        if (i == j):
            next
        else:
            mol += math.factorial(n - 1) * ((x_y[i][0] - x_y[j][0]) ** 2 + (x_y[i][1] - x_y[j][1]) ** 2) ** (1 / 2)

print(mol / den)

Submission status: Accepted

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

l = 0
c = 0
for v in itertools.permutations(xy): #順列をすべて列挙
    for i in range(len(v)-1):
        l += ((v[i][0] - v[i+1][0]) **2 + (v[i][1] - v[i+1][1])**2)**0.5 
    c += 1
print(l/c)

Submission status: Accepted

Solution code:
N = int(input())
dic = {}
for n in range(N):
    X, Y = input().split()
    x = int(X)
    y = int(Y)
    dic[n] = (x, y)
distancesum = 0    
for a in range(N):
    xa, ya = dic[a]
    #print(xa, ya)
    for b in range(N):
        xb, yb = dic[b]
        #print(xb, yb)
        distance2 = (xa-xb)**2+(ya-yb)**2
        distance = distance2**0.5
        distancesum += distance
#print(distancesum)  
ans = distancesum/N   
print(ans)

Submission status: Accepted

Solution code:
import itertools
import numpy as np


N = int(input())
n=[]
s=range(N)
for i in range(N):
    n.append([int(x) for x in input().split()])
Q = list(itertools.permutations(s))
L=0
for i in range(int(len(Q))):
    for j in range(N-1):
        L+= np.sqrt((n[Q[i][j+1]][0] - n[Q[i][j]][0])**2 + (n[Q[i][j+1]][1] - n[Q[i][j]][1])**2)
print(L/(len(Q)))

Submission status: Accepted

Solution code:
import math
from scipy.misc import comb

n = int(input())
t = [list(map(int, input().split())) for _ in range(n)]
d = 0
for i in range(n - 1):
    for j in range(i, n):
        d += (((t[i][0] - t[j][0]) ** 2 + (t[i][1] - t[j][1]) ** 2)
              ** 0.5) * ((n - 1) * math.factorial(n)/comb(n, 2))

print(d/math.factorial(n))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

loc = []
for i in range(n):
  loc.append(list(map(int, input().split())))
tot = 0
for i in range(n):
  for j in range(i + 1, n):
    tot += ((loc[i][0] - loc[j][0]) ** 2 + (loc[i][1] - loc[j][1]) ** 2) ** 0.5
tot *= all // (n * (n - 1) // 2)

print(tot / all * (n - 1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
import math

if __name__ == '__main__':

	n = int(input())

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

	p = list(itertools.permutations(A))

	kei = 0
	for x in p:
		for i in range(n-1):
			kei += math.sqrt((x[i+1][0] - x[i][0]) ** 2 + (x[i+1][1] - x[i][1]) ** 2)
	print(kei / len(p))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math


def len(lhs, rhs):
    return math.sqrt((lhs[0] - rhs[0]) ** 2 + (lhs[1] - rhs[1]) ** 2)


def solve(n: int, cities):
    sum = 0
    for i1, city1 in enumerate(cities):
        for i2, city2 in enumerate(cities):
            if i1 != i2:
                sum += len(city1, city2)
    return sum / n


def main():
    inputs = list(sys.stdin)
    n = int(inputs.pop(0))
    cities = [[int(n) for n in s.split(" ")] for s in inputs]
    print(solve(n, cities))


main()

Submission status: Accepted

Solution code:
import math

N = int(input())
town_location = [input().split() for _ in range(N)]
town_location = [[int(town_location[i][0]), int(town_location[i][1])] for i in range(N)]
sum_length = 0
for i in range(N-1):
    for j in range(i+1, N):
        sum_length += math.sqrt((town_location[i][0]-town_location[j][0])**2 + (town_location[i][1]-town_location[j][1])**2)
ans = (sum_length * 2) / N
print(ans)

Submission status: Accepted

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

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

def sd(p1, p2):
  return sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)

def path(p):
  distance = 0
  for i in range(len(p)-1):
    distance += sd(p[i],p[i+1])
  return distance

ans = sum(map(path,permutations(towns)))/factorial(n)
print(ans)

Submission status: Accepted

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

print(l/n)

Submission status: Accepted

Solution code:
import numpy
import itertools
import math

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

pattern_list = list(itertools.permutations(town_list, n))

a = int(math.factorial(n))

sum = 0
for j in range(a):
	for k in range(n-1):
		# sum += numpy.linalg.norm(pattern_list[j][k+1] - pattern_list[j][k])
		x1 = pattern_list[j][k][0]
		x2 = pattern_list[j][k+1][0]
		y1 = pattern_list[j][k][1]
		y2 = pattern_list[j][k+1][1]
		sum += math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)

print(sum/a)

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())

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

permus = list(permutations(list(range(n)),n))

def calc(i,j):
    return ((xy[i][0]-xy[j][0])**2+(xy[i][1]-xy[j][1])**2)**0.5

sum = 0
for permu in permus:
    for i in range(n-1):
        sum += calc(permu[i],permu[i+1])

ans = sum/len(permus)
print(ans)

Submission status: Accepted

Solution code:
import itertools
import math

def main():
    N = int(input())
    XY = []
    for i in range(N):
        x, y = list(map(int, input().split()))
        XY.append([x, y])
    
    CITY = [i for i in range(N)]
    # print(CITY)
    p = list(itertools.permutations(CITY, N))
    # print(p)

    ds = []
    for pi in p:
        dist = 0
        for i in range(N-1):
            cs = pi[i]
            ns = pi[i+1]

            x1 = XY[cs][0]
            y1 = XY[cs][1]
            x2 = XY[ns][0]
            y2 = XY[ns][1]
            d = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
            dist += d
        ds.append(dist)
    ans = sum(ds) / len(ds)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
N = int(input())
N_prime = 1
for i in range(1, N + 1):
    N_prime *= i
# print(N_prime)
coordinate = [0] * N
for k in range(N):
    coordinate[k] = [0] * 2
    coordinate[k][0], coordinate[k][1] = map(int, input().split())

N_C_2 = (N * (N - 1)) // 2
# print(N_C_2)
sum_dis = 0
for j in range(N-1):
    for h in range(j + 1, N):
        sum_dis += math.sqrt((coordinate[j][0]-coordinate[h][0])
                             ** 2 + (coordinate[j][1] - coordinate[h][1]) ** 2)
        # print(math.sqrt((coordinate[j][0]-coordinate[h][0])**2
        # + (coordinate[j][1] - coordinate[h][1]) ** 2))
ans = sum_dis * 2 / N
print(ans)

Submission status: Accepted

Solution code:
import itertools
import math

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

Submission status: Accepted

Solution code:
import itertools

n = int(input())
x = [0 for i in range(n)]
y = [0 for i in range(n)]
ans = 0

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

for l in itertools.permutations(range(n)):
  for i in range(len(l)-1):
    ans +=  ((x[l[i]]-x[l[i+1]])**2 + (y[l[i]]-y[l[i+1]])**2)**0.5

print(ans/len(list(itertools.permutations(range(n)))))

Submission status: Accepted

Solution code:
N = int(input())
lis = [[int(i)for i in input().split()]for _ in range(N)]
def leng(i,j):
    global lis
    return ((lis[i][0]-lis[j][0])**2+(lis[i][1]-lis[j][1])**2)**0.5

ans = 0
for i in range(N):
    for j in range(i+1,N):
        ans += leng(i,j)
print(ans*2/N)

Submission status: Accepted

Solution code:
from itertools import permutations

if __name__ == "__main__":
    n = int(input())
    x = [0 for i in range(n)]
    y = [0 for i in range(n)]

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

    ans = 0
    for perm in permutations(range(n)):

        for i in range(len(perm)-1):
            # print(((x[perm[i]]-x[perm[i]+1])**2 + (y[perm[i]]-y[perm[i]+1])**2)**0.5)
            ans +=  ((x[perm[i]]-x[perm[i+1]])**2 + (y[perm[i]]-y[perm[i+1]])**2)**0.5
    # print(ans)
    print(ans/len(list(permutations(range(n)))))

Submission status: Accepted

Solution code:
from itertools import permutations


def dist(x1, y1, x2, y2):
    x = x1 - x2
    y = y1 - y2
    return (x**2+y**2)**0.5


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

ans = 0
cnt = 0
for order in permutations(range(n)):
    cnt += 1
    d = 0
    for s, t in zip(order[:-1], order[1:]):
        x1, y1 = town[s]
        x2, y2 = town[t]
        d += dist(x1, y1, x2, y2)
    ans += d
print(ans/cnt)

Submission status: Accepted

Solution code:
import statistics
import itertools
import math

def get_distance(from_x, from_y, to_x, to_y) :
    return math.sqrt((to_x-from_x) **2 + (to_y-from_y)**2)

def main() :
    N = int(input())
    dic = {}
    for i in range(N) :
        p = input().split()
        dic[i] = {"x":int(p[0]), "y":int(p[1])}
    
    town_list = [int(i) for i in range(N)]

    route_list = list(itertools.permutations(town_list,N))

    total_distance = 0
    for routes in route_list :
        for i in range(len(routes)-1) :
            current_town = dic[routes[i]]
            next_town = dic[routes[i+1]]
            total_distance += get_distance(current_town["x"], current_town["y"], next_town["x"], next_town["y"])
    
    # print(total_distance / N)
    print(total_distance / math.factorial(N))


main()

Submission status: Accepted

Solution code:
import itertools
import math
N = int(input())
position = [list(map(int, input().split())) for i in range(N)]
l = [0, 1, 2, 3, 4, 5, 6, 7]
length = 0
for v in itertools.permutations(l[:N], N):
    for k in range(N-1):
        length += ((position[v[k]][0] - position[v[k+1]][0]) ** 2 +
                   (position[v[k]][1] - position[v[k+1]][1]) ** 2)**0.5
print(length/math.factorial(N))

Submission status: Accepted

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

def dist(p1, p2):
  return math.sqrt(math.pow(p1[0]-p2[0],2) + math.pow(p1[1]-p2[1], 2))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def cal(p,q):
    add=(l[p][0]-l[q][0])**2+(l[p][1]-l[q][1])**2
    return math.sqrt(add)

import itertools
import math
n=int(input())
l=[list(map(float,input().split())) for i in range(n)]


a=[]
ans=0
for i in range(n):
    a.append(i)
for x in itertools.permutations(a): 
    for i in range(n-1):
        ans+=cal(x[i],x[i+1])
for i in range(1,n+1):
    ans/=i
print(ans)

Submission status: Accepted

Solution code:




import itertools
import math
n=int(input())
l=[list(map(float,input().split())) for i in range(n)]

"""
for i in range(n):
    print(l[i])
"""

def cal(p,q):
    add=(l[p][0]-l[q][0])**2+(l[p][1]-l[q][1])**2
    return math.sqrt(add)


a=[]
ans=0
for i in range(n):
    a.append(i)
for x in itertools.permutations(a): 
    for i in range(n-1):
        ans+=cal(x[i],x[i+1])
for i in range(1,n+1):
    ans/=i
print(ans)

Submission status: Accepted

Solution code:




import itertools
import math
n=int(input())
l=[list(map(float,input().split())) for i in range(n)]

"""
for i in range(n):
    print(l[i])
"""

a=[]
ans=0
for i in range(n):
    a.append(i)
for x in itertools.permutations(a): 
    for i in range(n-1):
        #ans+=cal(a[i],a[i+1])
        add=(l[x[i]][0]-l[x[i+1]][0])**2+(l[x[i]][1]-l[x[i+1]][1])**2
        add=math.sqrt(add)
        ans+=add
        #print(ans)
    #print(x)
for i in range(1,n+1):
    ans/=i
print(ans)