Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Distance II

  Your task is to calculate the distance between two $n$ dimensional vectors $x = \{x_1, x_2, ..., x_n\}$ and $y = \{y_1, y_2, ..., y_n\}$.

  The Minkowski's distance defined below is a metric which is a generalization of both the Manhattan distance and the Euclidean distance.

\[
D_{xy} = (\sum_{i=1}^n |x_i - y_i|^p)^{\frac{1}{p}}
\]

It can be the Manhattan distance 

\[
D_{xy} = |x_1 - y_1| + |x_2 - y_2| + ... + |x_n - y_n|
\]

where $p = 1 $.

It can be the Euclidean distance

\[
D_{xy} = \sqrt{(|x_1 - y_1|)^{2} + (|x_2 - y_2|)^{2} + ... + (|x_n - y_n|)^{2}}
\]

where $p = 2 $.

Also, it can be the Chebyshev distance

\[
D_{xy} = max_{i=1}^n (|x_i - y_i|)
\]

where $p = \infty$

Write a program which reads two $n$ dimensional vectors $x$ and $y$, and calculates Minkowski's distance where $p = 1, 2, 3, \infty$ respectively.

Input

  In the first line, an integer $n$ is given. In the second and third line, $x = \{x_1, x_2, ... x_n\}$ and $y = \{y_1, y_2, ... y_n\}$ are given respectively. The elements in $x$ and $y$ are given in integers.

Output

  Print the distance where $p = 1, 2, 3$ and $\infty$ in a line respectively.
The output should not contain an absolute error greater than 10-5.

Constraints

$1 \leq n \leq 100$

$0 \leq x_i, y_i \leq 1000$

Sample Input

3
1 2 3
2 0 4

Sample Output

4.000000
2.449490
2.154435
2.000000



Sample input:
3
1 2 3
2 0 4


Sample output:
4.000000
2.449490
2.154435
2.000000

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))

a=[abs(x[i]-y[i]) for i in range(n)]

for i in range(1,4):
    D=0
    for d in a:
        D+=d**i
    print(float(D**(1/i)))

print(max(a))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))

tmp = [abs(x[i] - y[i]) for i in range(n)]

for p in range(1, 4):
    ans = 0
    for d in tmp:
        ans += d ** p
    print("{:.6f}".format(ans ** (1 / p)))

print("{:.6f}".format(max(tmp)))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
dxy1 = 0
for i in range(n):
    dxy1 += abs(x[i]-y[i])
dxy2 = 0
for i in range(n):
    dxy2 += abs(x[i]-y[i])**2
dxy2 = math.sqrt(dxy2)
dxy3 = 0
for i in range(n):
    dxy3 += abs(x[i]-y[i])**3
dxy3 = dxy3**(1/3)
dxy4 = 0
for i in range(n):
    tmp = abs(x[i]-y[i])
    if dxy4 < tmp:
        dxy4 = tmp
print(dxy1)
print(dxy2)
print(dxy3)
print(dxy4)

Submission status: Accepted

Solution code:
import math
def Minkowski(r, n, x, y):
    sum_dif = 0
    for i in range(n):
        dif = abs(x[i] - y[i])
        sum_dif += dif ** r
    print(sum_dif ** (1 / r))

def Chebyshev(n, x, y):
    max_dif = 0
    for i in range(n):
        dif = abs(x[i] - y[i])
        max_dif = max(dif, max_dif)
    print(max_dif)

n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
Minkowski(1, n, x, y)
Minkowski(2, n, x, y)
Minkowski(3, n, x, y)
Chebyshev(n, x, y)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

c =[abs(a[i]-b[i]) for i in range(n)]

for p in range(1,4):
    total=0
    for d in c:
        total+=d**p
    ans=total**(1/p)
    print(ans)

print(max(c))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
p1 =0
for i in range(len(x)):
    p1 += abs(x[i]-y[i])
print(p1)
p2=0
for i in range(len(x)):
    p2 += (x[i]-y[i])**2
print(math.sqrt(p2))
p3=0
for i in range(len(x)):
    p3 += abs(x[i]-y[i])**3
print(math.pow(p3,1/3))
pinf=0
for i in range(len(x)):
    if abs(x[i]-y[i]) >= pinf:
        pinf = abs(x[i]-y[i])
print(pinf)

Submission status: Accepted

Solution code:
import math
n=int(input())
X=list(map(int,input().split()))
Y=list(map(int,input().split()))
D=[abs(x-y) for x,y in zip(X,Y)]
p1=sum(D)
p2=math.sqrt(sum(i**2 for i in D))
p3=sum(i**3 for i in D)**(1/3)
pinf=max(D)
print(p1)
print(p2)
print(p3)
print(pinf)

Submission status: Accepted

Solution code:
def calc_mincowski_distance(n, p, x, y):
    distance = 0
    for i in range(n):
        distance += abs((x[i]-y[i]) ** p )
    distance **= 1/p
    return distance

def calc_chebyshev_distance(n, x, y):
    max_distance = -1
    for i in range(n):
        distance = abs(x[i] - y[i])
        if distance > max_distance:
            max_distance = distance
    return max_distance

n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

d1 = calc_mincowski_distance(n, 1, x, y)
d2 = calc_mincowski_distance(n, 2, x, y)
d3 = calc_mincowski_distance(n, 3, x, y)
d_infinity = calc_chebyshev_distance(n, x, y)

print(d1, d2, d3, d_infinity, sep='\n')

Submission status: Accepted

Solution code:
n = float(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))

D_p1 = 0.0
for i in range(len(x)):
    D_p1 += abs(x[i] - y[i])

D_p2 = 0.0
for i in range(len(x)):
    D_p2 += (x[i]-y[i])**2
D_p2 = D_p2**(1/2)

D_p3 = 0.0
for i in range(len(x)):
    D_p3 += abs((x[i] - y[i]))**3
D_p3 = D_p3**(1/3)

D_pinf = 0.0
inf_list =[]
for i in range(len(x)):
    inf_list.append(abs(x[i]-y[i]))
D_pinf = max(inf_list)

print(D_p1)
print(D_p2)
print(D_p3)
print(D_pinf)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))

def D(p):
    sum = 0
    for i in range(n):
        sum += abs(x[i]-y[i])**p
    print('{:.6f}'.format(sum ** (1/p)))

for i in range(n):
    z = [abs(x[i]-y[i]) for i in range(n)]

D(1)
D(2)
D(3)
print('{:.6f}'.format(max(z)))

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
def distant(A,B,p):
    count=0
    for i in range(len(A)):
        count +=(abs(A[i]-B[i]))**p
    count=count**(1/p)
    return print("%.10f"%count)
distant(A,B,1)
distant(A,B,2)
distant(A,B,3)

point=0
for i in range(n):
    point=max(point,abs(A[i]-B[i]))
print("%.10f"%point)

Submission status: Accepted

Solution code:
import math

# Inputs
n = int(input())
x_list = list(map(int, input().split()))
y_list = list(map(int, input().split()))

# p = 1
d1_list = [abs(x-y) for x, y in zip(x_list, y_list)]
d1 = sum(d1_list)

# p = 2
d2_list = [abs((x-y))**2 for x, y in zip(x_list, y_list)]
d2 = math.sqrt(sum(d2_list))

# p = 3
d3_list = [(abs(x-y))**3 for x, y in zip(x_list, y_list)]
d3 = (sum(d3_list))**(1/3)

# p = infinite
d4_list = [abs(x-y) for x, y in zip(x_list, y_list)]
d4 = max(d4_list)

# Output
print('{:.8f}'.format(d1))
print('{:.8f}'.format(d2))
print('{:.8f}'.format(d3))
print('{:.8f}'.format(d4))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

for i in range(1,4):
    total = 0
    Dxy = 0   
    for j,k in zip(a,b):
        total += pow(abs(j-k),i)
        Dxy = pow(total,(1/i))
    print(Dxy)

c = []
for j,k in zip(a,b):
    c.append(abs(j-k))
print(max(c))

Submission status: Accepted

Solution code:
n = int(input())
x = [float(x) for x in input().split()]
y = [float(x) for x in input().split()]
z = [0]*4

for _ in range(n):
    z[0] += abs(x[_] - y[_])
    z[1] += abs(x[_] - y[_])**2
    z[2] += abs(x[_] - y[_])**3
    if abs(x[_] - y[_]) > z[3]:
           z[3] = abs(x[_] - y[_])    

print(z[0])
print(z[1]**0.5)
print(z[2]**(1/3))
print(z[3])

Submission status: Accepted

Solution code:
def d(p):
    return sum(abs(x[i] - y[i]) ** p for i in range(n)) ** (1 / p)


n = int(input())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
print(d(1), d(2), d(3), max([abs(x[i] - y[i]) for i in range(n)]), sep="\n")

Submission status: Accepted

Solution code:
n = int(input())
x = [float(i) for i in input().split()]
y = [float(i) for i in input().split()]
num = 1000
print(sum([abs(a - b) for a, b in zip(x, y)]))
print((sum([abs(a - b) ** 2 for a, b in zip(x, y)])) ** (1 / 2))
print((sum([abs(a - b) ** 3 for a, b in zip(x, y)])) ** (1 / 3))
print(max([abs(a - b) for a, b in zip(x, y)]))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
d=[]
for i in range(n):
    d.append(abs(a[i]-b[i]))
d1=d2=d3=0
for e in d:
    d1+=e
    d2+=e**2
    d3+=e**3
d2=d2**(1/2)
d3=d3**(1/3)
dinf=max(d)
print('{:7f}'.format(d1))
print('{:7f}'.format(d2))
print('{:7f}'.format(d3))
print('{:7f}'.format(dinf))

Submission status: Accepted

Solution code:
#coding: utf-8

n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
d = [0 for i in range(4)]
for i in range(n):
    temp1 = abs(x[i] - y[i])
    d[0] += temp1
    d[1] += temp1 ** 2
    d[2] += temp1 ** 3
    d[3] = max(d[3], temp1)
d[1] **= (1/2)
d[2] **= (1/3)
for i in d:
    print(i)

Submission status: Accepted

Solution code:
import math

def Dxy_p(X,Y,n,p):
    ret=0
    for i in range(n):
        ret += pow(abs(X[i]-Y[i]), p)
    return pow(ret, 1/p)

def Dxy_inf(X,Y,n):
    ret=0
    for i in range(n):
        ret = max(ret, abs(X[i]-Y[i]))
    return ret

n=int(input())
X=list(map(int,input().split()))
Y=list(map(int,input().split()))

print(f"{Dxy_p(X,Y,n,1):.6f}")
print(f"{Dxy_p(X,Y,n,2):.6f}")
print(f"{Dxy_p(X,Y,n,3):.6f}")
print(f"{Dxy_inf(X,Y,n):.6f}")

Submission status: Accepted

Solution code:
n = int(input())
*x, = map(float, input().split())
*y, = map(float, input().split())

d = [0]*4
d[0] = sum(abs(a - b) for a, b in zip(x, y)) # manhattan
d[1] = (sum((a - b)**2 for a, b in zip(x, y)))**0.5 # euclid
d[2] = (sum(abs(a - b)**3 for a, b in zip(x, y)))**(1/3) # 
d[3] = max(abs(a - b) for a, b in zip(x, y)) # chebyshev

for i in range(4):
    print("%.6f" % d[i])

Submission status: Accepted

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

x = [int(a) for a in input().split()]
y = [int(a) for a in input().split()]



p1 = 0
for i in range(num):
    p1 += abs(x[i]-y[i])
print(float(p1))

p2 = 0
for i in range(num):
    p2 += (x[i] - y[i])**2
p2 = math.sqrt(p2)
print(p2)

p3 = 0
for i in range(num):
    p3 += abs((x[i]-y[i])**3)
    
p3 = math.pow(p3,(1.0/3.0))
print(p3)

m = []
for i in range(num):
    m.append(abs(x[i]-y[i]))

print(float(max(m)))

Submission status: Accepted

Solution code:
n= int(input())
x =[int(s) for s in input().split()]
y =[int(s) for s in input().split()]

#p=1
S = 0
for i in range(n):
	S += abs(x[i]-y[i])
D_1 = S
print("{:.8f}".format(D_1))

#p=2
T = 0
for i in range(n):
	T += (x[i]-y[i])**2
D_2 = T**(1/2)
print("{:.8f}".format(D_2))

#p=3
V = 0
for i in range(n):
	V += abs(x[i]-y[i])**3
D_3 = V**(1/3)
print("{:.8f}".format(D_3))

#p=∞
U = 0
max = 0
for i in range(n):
	if max <= abs(x[i]-y[i]):
		max = abs(x[i]-y[i])
print("{:.8f}".format(max))

Submission status: Accepted

Solution code:
n=int(input())
X=list(map(float,input().split()))
Y=list(map(float,input().split()))
for p in range(1,4):
    Dxy=float(0)
    for i in range(n):
        Dxy+=abs(X[i]-Y[i])**p
    Dxy=Dxy**(1/p)
    print(Dxy)
Dxy=float(0)
for i in range(n):
    if Dxy<abs(X[i]-Y[i]):Dxy=abs(X[i]-Y[i])
print(Dxy)

Submission status: Accepted

Solution code:
import math

n = int(input())
x =list(map(float,input().split()))
y =list(map(float,input().split()))

D1=[]
D2=[]
D3=[]
for i in range(n):
    D1.append(abs(x[i]-y[i]))
    D2.append((x[i]-y[i])**2)
    D3.append((abs(x[i]-y[i]))**3)

print(sum(D1))
print(math.sqrt(sum(D2)))
print(math.pow(sum(D3),(1/3)))
print(max(D1))

Submission status: Accepted

Solution code:
def test():

	n = int(input())
	x_array = list(map(int,input().split()))
	y_array = list(map(int,input().split()))

	p_1 = 0
	p_2 = 0
	p_3 = 0
	p_inf = 0

	for i in range(n):
		
		tmp = abs(x_array[i] - y_array[i])
		p_1 += tmp
		p_2 += tmp ** 2
		p_3 += tmp ** 3
		if p_inf < tmp:
			p_inf = tmp

	p_2 = p_2**(1/2)
	p_3 = p_3**(1/3)

	print(f"{p_1:.6f}")
	print(f"{p_2:.6f}")
	print(f"{p_3:.6f}")
	print(f"{p_inf:.6f}")


if __name__ == "__main__":
	test()

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
a = []
p1, p2, p3, p_inf = 0, 0, 0, 0
for i in range(n):
    c = abs(x[i]-y[i])
    p1 += c
    p2 += c ** 2
    p3 += c ** 3
    a.append(c)

p_inf = max(a)

print(p1, p2**(1/2), p3**(1/3), p_inf)

Submission status: Accepted

Solution code:
n = int(input())
X = [int(_) for _ in input().split()]
Y = [int(_) for _ in input().split()]

d1, d2, d3, d = 0, 0, 0, 0 
for x, y in zip(X, Y):
    d1 += abs(x - y)
    d2 += abs(x - y) ** 2
    d3 += abs(x - y) ** 3
    d = max(d, abs(x - y))

print(d1)
print(d2 ** (1/2))
print(d3 ** (1/3))
print(d)

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
# p=1,2,3
for p in range(1, 4):
    print("{0:.6f}".format(sum([abs(a-b)**p for a, b in zip(X, Y)])**(1/p)))
# p=infinity
print("{0:.6f}".format(max([abs(a-b) for a, b in zip(X, Y)])))

Submission status: Accepted

Solution code:
n = int(input())
x = map(int, input().split())
y = map(int, input().split())
 
x_y = [abs(xi - yi) for xi, yi in zip(x, y)]
p1 = sum(x_y)
p2 = sum(i ** 2 for i in x_y) ** (1 / 2)
p3 = sum(i ** 3 for i in x_y) ** (1 / 3)
p_inf = max(x_y)
 
print(p1)
print(p2)
print(p3)
print(p_inf)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(float,input().split()))
y=list(map(float,input().split()))
for p in range(1,4):
    Dxy=float(0)
    for i in range(n):
        Dxy+=abs(x[i]-y[i])**p
    Dxy=Dxy**(1/p)
    print(f'{Dxy:.6f}')
Dxy=float(0)
for i in range(n):
    if Dxy<abs(x[i]-y[i]):Dxy=abs(x[i]-y[i])
print(f'{Dxy:.6f}')

Submission status: Accepted

Solution code:
def dist(p, X, Y):
  s = 0
  for x, y in zip(X, Y):
    s += abs(x - y) ** p
  return s ** (1/p)

N = int(input())
*X, = map(int, input().split())
*Y, = map(int, input().split())
print(dist(1, X, Y))
print(dist(2, X, Y))
print(dist(3, X, Y))
print(max(abs(x - y) for x, y in zip(X, Y)))

Submission status: Accepted

Solution code:
import math
 
class Vector(object):
    def __init__(self, arr):
        self.arr = arr
 
    def sub(self, vec):
        return Vector([self.arr[i] - vec.arr[i] for i in range(len(self.arr))])
     
    def p_norm(self, p):
        return sum([abs(e) ** p for e in self.arr]) ** (1 / p)
     
    def inf_norm(self):
        return max([abs(e) for e in self.arr])
 
 
n = int(input())
x = Vector([int(e) for e in input().split()])
y = Vector([int(e) for e in input().split()])
z = x.sub(y)
 
print(z.p_norm(1))
print(z.p_norm(2))
print(z.p_norm(3))
print(z.inf_norm())

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
 
d1 = 0
d2 = 0
d3 = 0
d = [0] * n
 
for i in range(n) :
  d1 += abs(x[i] - y[i])
print(d1)
   
for j in range(n) :
  d2 += abs(x[j] - y[j]) ** 2
d2 = math.sqrt(d2)
print(d2)
 
for k in range(n) : 
  d3 += abs(x[k] - y[k]) ** 3
d3 = d3 ** (1/3)
print(d3)
 
for l in range(n) :
  d[l] = abs(x[l] - y[l])
di = max(d)  #infinity
print(di)

Submission status: Accepted

Solution code:
lw = []
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
for p in range(1, 4):
    r = 0
    q = 0
    for i in range(n):
        z = abs(x[i] - y[i])
        if p == 1:
            if q < z:
                q = z
            if i == n - 1:
                lw.append(q)
        r += pow(z, p)
    print(pow(r, float(1 / p)))
print(lw[0])

Submission status: Accepted

Solution code:
# coding: utf-8

n = input()
xs = map(float, input().split())
ys = map(float, input().split())
abs_d = [abs(x - y) for (x, y) in zip(xs, ys)]
d = [sum([i ** p for i in abs_d]) ** (1 / p) for p in range(1, 4)]
d.append(max(abs_d))

print(*d, sep='\n')

Submission status: Accepted

Solution code:
import math
N=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
c=[abs(b[i]-a[i]) for i in range(N)]
print(sum(c))
print(math.sqrt(sum([c[i]**2 for i in range(N)])))
print((sum([c[i]**3 for i in range(N)]))**(1/3))
print(max(c))

Submission status: Accepted

Solution code:
import math

n = int(input())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))

def calculate_distance(data1,data2,p):
  if len(data1) != len(data2):
    print("error")
  tmp_sum=0
  for i in range(len(data1)):
    tmp_sum += abs(data2[i]-data1[i]) ** p
  return tmp_sum ** (1/p)

#where p = 1
print("{:.5f}".format(calculate_distance(a_list,b_list,1)))

#where p = 2
print("{:.5f}".format(calculate_distance(a_list,b_list,2)))

#where p = 3
print("{:.5f}".format(calculate_distance(a_list,b_list,3)))

#where p = ∞
tmp_list=[0]*n
for i in range(n):
  tmp_list[i]=abs(a_list[i]-b_list[i])
print("{:.5f}".format(max(tmp_list)))

Submission status: Accepted

Solution code:
def minkowski_distance(n, x, y, p):
    if p == float('inf'):
        distance = max([abs(x[i] - y[i]) for i in range(n)])
    else:
        distance = sum([abs(x[i] - y[i]) ** p for i in range(n)]) ** (1 / p)
    return distance

n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

f_inf = float('inf')
print(minkowski_distance(n, x, y, 1))
print(minkowski_distance(n, x, y, 2))
print(minkowski_distance(n, x, y, 3))
print(minkowski_distance(n, x, y, f_inf))

Submission status: Accepted

Solution code:
def distance(x, y, p):
	dist = 0
	for cx, cy in zip(x, y):
		dist += abs(cx - cy) ** p
	return dist ** (1 / p)

N = int(input())
X = list(map(float, input().split()))
Y = list(map(float, input().split()))

print(distance(X, Y, 1))
print(distance(X, Y, 2))
print(distance(X, Y, 3))

max_dist = 0
for cx, cy in zip(X, Y):
	max_dist = max(max_dist, abs(cx - cy))
print(max_dist)

Submission status: Accepted

Solution code:
import math

n =int(input())
x = [];y = []
x = list(map(int,input().split()))
y = list(map(int,input().split()))
d1 = 0
d2 = 0
d3 = 0
dx = -1e9
for i in range(n):
    d1 += abs(x[i]-y[i])
    d2 += (x[i]-y[i])**2
    d3 += (abs(x[i]-y[i]))**3
    dx = max(dx,abs(x[i]-y[i]))

d1 = d1**1
d2 = d2**0.5
d3 = d3**(1/3)
dx = dx
print("%.5f"%d1)
print("%.5f"%d2)
print("%.5f"%d3)
print("%.5f"%dx)

Submission status: Accepted

Solution code:
import math

n = int(input())

a = list(map(float, input().split()))
b = list(map(float, input().split()))

d1 = [0 for i in range(n)]
d2 = [0 for i in range(n)]
d3 = [0 for i in range(n)]

for i in range(n):
    d1[i] = abs(a[i] - b[i])
    d2[i] = abs(a[i] - b[i])**2
    d3[i] = abs(a[i] - b[i])**3

print(sum(d1))
print(math.sqrt(sum(d2)))
print(sum(d3)**(1/3))
print(max(d1))

Submission status: Accepted

Solution code:
import math
def souhei(n):
	for w in range(n):
		a=list(map(int,input().split()))
		return(a)
n=int(input())
A=souhei(n)
B=souhei(n)
P=[]
for mm in range(1,5):
	del P[:]
	P=[]
	if mm==4:
		for m in range(n):
			P.append(abs(A[m]-B[m]))
			p=max(P)
	else:
		for m in range(n):
			o=(abs(A[m]-B[m]))**mm
			P.append(o)
		p=sum(P)
		p=(p**(1/mm))
	print('{:.5f}'.format(p))

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int,input().split()))
Y = list(map(int,input().split()))

#P=1のとき
D1 = 0
for x, y in zip(X, Y):
  D1 += abs(x-y)
print(D1)

#P=2のとき
D2 = 0
for x, y in zip(X, Y):
  D2 += (abs(x-y))**2
print(D2**(1/2))

#P=3のとき
D3 = 0
for x, y in zip(X, Y):
  D3 += (abs(x-y))**3
print(D3**(1/3))  

#P=∞のとき
D4 = []
for x, y in zip(X, Y):
  D4.append(abs(x-y))
print(max(D4))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
d1 = 0  # p = 1のときのミンコフスキー距離
d2 = 0  # p = 2のときのミンコフスキー距離
d3 = 0  # p = 3のときのミンコフスキー距離
dx = 0  # p = ∞のときのミンコフスキー距離

for i in range(n):
    t = x[i] - y[i]
    if t < 0:  # 絶対値にするための処理
        t *= -1
    d1 += t
    d2 += t ** 2
    d3 += t ** 3
    if dx < t:
        dx = t
d2 = d2 ** (1.0 / 2.0)
d3 = d3 ** (1.0 / 3.0)
print(d1)
print(d2)
print(d3)
print(dx)

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(float,input().split()))
Y = list(map(float,input().split()))
D = [abs(X[i]-Y[i]) for i in range(len(X))]
print(sum(D))
print((sum([ele**2 for ele in D]))**(1/2))
print((sum([ele**3 for ele in D]))**(1/3))
print(max(D))

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int,input().split()))
Y = list(map(int,input().split()))
# p=1,2,3
for p in range(1,4):
    print("{0:.6f}".format(sum([abs(a-b)**p for a,b in zip(X,Y)])**(1/p)))
# p=infinity
print("{0:.6f}".format(max([abs(a-b) for a,b in zip(X,Y)])))

Submission status: Accepted

Solution code:
N=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
distance=[0 for _ in range(N)]
for i in range(N):
    distance[i]=abs(x[i]-y[i])
for i in range(1,5):
    P=0
    for j in range(N):
        P+=distance[j]**i
    if i<4:
        print(P**(1/i))
    else:
        print(max(distance))

Submission status: Accepted

Solution code:

import math

if __name__ == '__main__':

	n = input()
	X = list(map(int,input().split()))
	Y = list(map(int,input().split()))

    #マンハッタン距離
	d1 = 0
	for x1,y1 in zip(X,Y):
		d1 += abs(x1-y1)

	print('{:.6f}'.format(d1))

    #ユークリッド距離
	d2_tmp = 0
	for x2,y2 in zip(X,Y):
		d2_tmp += abs(x2-y2)**2
	d2 = math.sqrt(d2_tmp)
	print('{:.6f}'.format(d2))

	#n=3
	d3_tmp = 0
	for x3,y3 in zip(X,Y):
		d3_tmp += abs(x3-y3)**3
	d3 = d3_tmp ** (1/3)
	print('{:.6f}'.format(d3))

	#チェビシェフ距離
	d8 = -1
	for x8,y8 in zip(X,Y):
		d8 = max(d8,abs(x8-y8))

	print('{:.6f}'.format(d8))

Submission status: Accepted

Solution code:
import math
def dist(p, x, y):
    s = 0
    for i, j in zip(x, y):
        s += abs(i - j) ** p
    return s ** (1 / p)

n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
print(dist(1, x, y))
print(dist(2, x, y))
print(dist(3, x, y))
print(max(abs(i-j) for i, j in zip(x, y)))

Submission status: Accepted

Solution code:
def distance_p(x_values: list, y_values: list, p: int) -> float:
    return sum(map(lambda x, y: abs(x - y) ** p, x_values, y_values)) ** (1 / p)


def distance_chebyshev(x_values: list, y_values: list) -> float:
    return max(map(lambda x, y: abs(x - y), x_values, y_values))


if __name__ == "__main__":
    elem_num = int(input())
    x_values = list(map(lambda x: float(x), input().split()))
    y_values = list(map(lambda x: float(x), input().split()))
    print(f"{distance_p(x_values, y_values, 1)}")
    print(f"{distance_p(x_values, y_values, 2)}")
    print(f"{distance_p(x_values, y_values, 3)}")
    print(f"{distance_chebyshev(x_values, y_values)}")

Submission status: Accepted

Solution code:
# coding=utf-8

###
### for atcorder program
###

import sys
import math

# math class
class mymath:
    ### pi
    pi = 3.14159265358979323846264338

    ### Prime Number
    def pnum_eratosthenes(self, n):
        ptable = [0 for i in range(n+1)]
        plist = []

        for i in range(2, n+1):
            if ptable[i]==0:
                plist.append(i)
                for j in range(i+i, n+1, i):
                    ptable[j] = 1
        return plist

    ### GCD
    def gcd(self, a, b):
        if b == 0:
            return a
        return self.gcd(b, a%b)

    ### LCM
    def lcm(self, a, b):
        return (a*b)//self.gcd(a,b)

    ### Mat Multiplication
    def mul(self, A, B):
        ans = []
        for a in A:
            c = 0
            for j, row in enumerate(a):
                c += row*B[j]
            ans.append(c)
        return ans

mymath = mymath()

### output class
class output:
    ### list
    def list(self, l):
        l = list(l)
        #print(" ", end="")
        for i, num in enumerate(l):
            print(num, end="")
            if i != len(l)-1:
                print(" ", end="")
        print()

output = output()

### input sample
#i = input()
#N = int(input())
#A, B, C = [x for x in input().split()]
#N, K = [int(x) for x in input().split()]
#inlist = [int(w) for w in input().split()]

#R = float(input())
#A.append(list(map(int,input().split())))

#for line in sys.stdin.readlines():
#    x, y = [int(temp) for temp in line.split()]

#abc list
#abc = [chr(ord('a') + i) for i in range(26)]

### output sample
#print("{0} {1} {2:.5f}".format(A//B, A%B, A/B))
#print("{0:.6f} {1:.6f}".format(R*R*math.pi,R*2*math.pi))
#print(" {}".format(i), end="")

def get_input():
    N = []
    while True:
        try:
            N.append(input())
        except EOFError:
            break
    return N

N = int(input())
X = [int(i) for i in input().split()]
Y = [int(i) for i in input().split()]

for i in range(1,4):
    P = 0
    for j in range(N):
        P += (math.fabs(X[j]-Y[j]))**i
    P = P**(1/i)
    print(P)

P4 = []
for i in range(N):
    P4.append(math.fabs(X[i]-Y[i]))
print(max(P4))

Submission status: Accepted

Solution code:
n = int(input())
x, y = [list(map(int, input().split())) for _ in range(2)]
 
print(sum([abs(x[i] - y[i]) for i in range(n)]))
print(pow(sum([(x[i] - y[i]) ** 2 for i in range(n)]), 1/2))
print(pow(sum([abs(x[i] - y[i]) ** 3 for i in range(n)]), 1/3))
print(max([abs(x[i] - y[i]) for i in range(n)]))

Submission status: Accepted

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

print(sum([abs(x[i] - y[i]) for i in range(n)]))
print(pow(sum([(x[i] - y[i]) ** 2 for i in range(n)]), 1/2))
print(pow(sum([abs(x[i] - y[i]) ** 3 for i in range(n)]), 1/3))
print(max([abs(x[i] - y[i]) for i in range(n)]))

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline())
x = list(map(int, sys.stdin.readline().split()))
y = list(map(int, sys.stdin.readline().split()))
absxy = [abs(x[i]-y[i]) for i in range(n)]

for dim in range(1,4):
    total = sum( (absxy[i]**dim for i in range(n)) )
    print(round(total**(1/dim), 6))
    
print(float(max(absxy)))

Submission status: Accepted

Solution code:
from math import pow


n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

d1 = d2 = d3 = dm = 0
for i in range(n):
    diff = abs(x[i] - y[i])
    d1 += diff
    d2 += diff ** 2
    d3 += diff ** 3
    dm = diff if dm < diff else dm

print("{:.5f}\n{:.5f}\n{:.5f}\n{:.5f}".format(d1, pow(d2, 1 / 2), pow(d3, 1 / 3), dm))

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
p1,p2,p3=0,0,0
Dxy=[0]*(n+1)
for i in range(n):
    Dxy[i]=abs(x[i]-y[i])
    p1+=Dxy[i]
    p2+=Dxy[i]**2
    p3+=Dxy[i]**3
p2**=0.5
p3**=(1/3)
print(p1,p2,p3,max(Dxy),sep='\n')

Submission status: Accepted

Solution code:
import math



n= int(input())
x=list(map(float,input().split()))
y=list(map(float,input().split()))

d1=0
d2=0
d3=0
dd=0

for i in range(n):
	d1 +=  abs(x[i]-y[i])
	d2 += (abs(x[i]-y[i]))**2
	d3 += (abs(x[i]-y[i]))**3
	if 	dd < abs(x[i]-y[i]):
		dd=abs(x[i]-y[i])

print(d1)
print(d2**0.5)
print(d3**(1/3))
print(dd)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))
D = []
p1 = 0
p2 = 0
p3 = 0
pi = 0
pimax = 0

for i in range(n):
    p1 = p1 + abs(x[i]-y[i])
    p2 = p2 + pow(abs(x[i]-y[i]),2)
    p3 = p3 + pow(abs(x[i]-y[i]),3)
    pi = abs(x[i] - y[i])
    if pimax < pi:
        pimax = pi
D.append(p1)
D.append(pow(p2,1/2))
D.append(pow(p3,1/3))
D.append(pimax)

for i in D:
    print(i)

Submission status: Accepted

Solution code:
input()
a = list(map(int, input().split()))
b = list(map(int, input().split()))

d = lambda a, b, n: sum([abs(x - y) ** n for x, y in zip(a, b)]) ** (1/n)
print(d(a, b, 1))
print(d(a, b, 2))
print(d(a, b, 3))
print(max([abs(x - y) for x, y in zip(a, b)]))

Submission status: Accepted

Solution code:
import math
n = input()
x = list(map(int, input().split()))
y = list(map(int, input().split()))
d1=0
d2=0
d3=0
d_inf=0
for i in range(len(x)):
    d1 += abs(x[i]-y[i])
    d2 += ((x[i]-y[i])**2)
    d3 += abs((x[i]-y[i])**3)
    if abs(x[i]-y[i]) >= d_inf:
        d_inf = abs(x[i]-y[i])
print(d1)
print(math.sqrt(d2))
print(d3**(1/3.0))
print(d_inf)

Submission status: Accepted

Solution code:
import math
n = int(input())
li1 = [int(i) for i in input().split()]
li2 = [int(i) for i in input().split()]
D1 = 0
D2 = 0
d2 = 0
D3 = 0
d3 = 0
D8 = 0
li = []
for i in range(n):
    D1 += abs(li1[i]-li2[i])
    d2 += (li1[i]-li2[i])**2
    D2 = math.sqrt(d2)
    d3 += (abs(li1[i]-li2[i]))**3
    D3 = d3**(1/3)
    li.append(abs(li1[i]-li2[i]))
    D8 = max(li)
print(D1)
print(D2)
print(D3)
print(D8)

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))

print(sum(abs(x[i]-y[i]) for i in range(n)))
print(sum((abs(x[i]-y[i]) ** 2) for i in range(n)) ** 0.5)
print(sum((abs(x[i]-y[i]) ** 3) for i in range(n)) ** (1/3))
print(max(abs(x[i] - y[i]) for i in range(n)))

Submission status: Accepted

Solution code:
import math
n =int(input())
X=list(map(int, input().split()))
Y=list(map(int, input().split()))
s1=0
s2=0
s3=0
s4=[]
for i in range(n):
  s1 += abs(X[i]-Y[i])
  s2 += (abs(X[i]-Y[i])**2)
  s3 += (abs(X[i]-Y[i])**3)
  s4.append(abs(X[i]-Y[i]))
#print(s4)
s4max = max(s4)
  
print('{:.6f}'.format(s1))
print('{:.6f}'.format(math.sqrt(s2)))
print('{:.6f}'.format(math.pow(s3,1/3)))
print('{:.6f}'.format(s4max))

Submission status: Accepted

Solution code:
import math

n=int(input())
X=input().split()
Y=input().split()
x=[0 for i in range(n)]
y=[0 for i in range(n)]
for i in range(n):
    x[i]=int(X[i])
for i in range(n):
    y[i]=int(Y[i])
sum_1=0
for i in range(n):
    sum_1=sum_1+abs(x[i]-y[i])
D_1=sum_1

sum_2=0
for i in range(n):
    sum_2=sum_2+((x[i]-y[i])**2)
D_2=math.sqrt(sum_2)

sum_3=0
for i in range(n):
    sum_3=sum_3+abs(x[i]-y[i])**3
D_3=sum_3**(1/3)
Abs=[abs(x[i]-y[i]) for i in range(n)]
max=max(Abs)
print(f"{D_1} {D_2} {D_3} {max}")

Submission status: Accepted

Solution code:
import math
n=int(input())
x=list(map(float,input().split()))
y=list(map(float,input().split()))
M=0
Y=0
T=0
Cn=[]

for i in range(n):
  z=abs(x[i]-y[i])
  M+=z
  Y+=z**2
  T+=z**3
  Cn.append(z)
  
Y=math.sqrt(Y)
T=T**(1/3)
C=max(Cn)

print(f"{M:.6f}")
print(f"{Y:.6f}")
print(f"{T:.6f}")
print(f"{C:.6f}")

Submission status: Accepted

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

a = list(map(int, input().split()))
b = list(map(int, input().split()))

p1 = sum(math.fabs(a[i]-b[i]) for i in range(n))
p2 = math.sqrt(sum([math.fabs(a[i]-b[i])**2 for i in range(n)]))
p3 = math.pow(sum([math.fabs(a[i]-b[i])**3 for i in range(n)]), 1/3)
pp = max([math.fabs(a[i]-b[i]) for i in range(n)])

print('{0:6f}'.format(p1))
print('{0:6f}'.format(p2))
print('{0:6f}'.format(p3))
print('{0:6f}'.format(pp))

Submission status: Accepted

Solution code:
import decimal
from operator import sub


# INPUT
n = int(input())

vector_x = list(map(decimal.Decimal, input().split()))
vector_y = list(map(decimal.Decimal, input().split()))


# CALC:d1, d2, d3
d1 = decimal.Decimal("0")
d2 = decimal.Decimal("0")
d3 = decimal.Decimal("0")

for i in range(n):
    d1 += abs(vector_x[i] - vector_y[i])

    d2 += abs(vector_x[i] - vector_y[i]) ** decimal.Decimal("2")

    d3 += abs(vector_x[i] - vector_y[i]) ** decimal.Decimal("3")


d2 = pow(d2, decimal.Decimal("1") / decimal.Decimal("2"))
d3 = pow(d3, decimal.Decimal("1") / decimal.Decimal("3"))


# CALC:dinf
vector_sub = map(abs, map(sub, vector_x, vector_y))
dinf = max(vector_sub)


# OUTPUT
for answer in [d1, d2, d3, dinf]:
    print(answer)