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:
def get_minkowski_distance(a, b, p):
    """
    get Minkowski's distance
    a : int list
    b : int list
    p : int
    """
    s = 0
    res = 0.0

    if p == 0:
        l = []
        for x, y in zip(a, b):
            l.append(abs(x - y))
        res = max(l) 

    else:
        for x, y in zip(a, b):
            s += abs(x - y) ** p

        res = round((s ** (1/p)), 6)

    return res

if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    print(get_minkowski_distance(a, b, 1))
    print(get_minkowski_distance(a, b, 2))
    print(get_minkowski_distance(a, b, 3))
    print(get_minkowski_distance(a, b, 0))

Submission status: Accepted

Solution code:
n = int(input())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
for p in range(1, 4):
    print((sum(abs(x[i] - y[i]) ** p for i in range(n))) ** (1/p))
print(max(abs(x[i] - y[i]) for i in range(n)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# coding: UTF-8
import math

def dist1(data1, data2):
    leng = 0
    for a, b in zip(data1, data2):
        leng += abs(a-b)

    return leng

def dist2(data1, data2):
    leng = 0
    for a,b in zip(data1, data2):
        leng += abs(a-b)**2.0
    
    return (math.sqrt(leng))

def distn(data1, data2, n):
    leng = 0
    for a,b in zip(data1, data2):
        leng += abs(a-b) ** n

    return (pow(leng, 1/n))

def distlm(data1, data2):
    leng = 0
    for a,b in zip(data1, data2):
        ab = abs(a-b)
        leng = leng if leng > ab else ab
    return leng

leng = input()
datax =list(map(float, input().split()))
datay =list(map(float, input().split()))

print ( distn(datax, datay, 1))
print ( distn(datax, datay, 2))
print ( distn(datax, datay, 3))
print ( distlm(datax, datay))

Submission status: Accepted

Solution code:
# ITP_10_D

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

D = 0
for i in range(n):
    D += abs(x[i] - y[i])
print(D)

#p=2
D = 0
for i in range(n):
    D += (x[i]-y[i])**2
print(D**(1/2))

#p=3
D = 0
for i in range(n):
    D += (abs(x[i] - y[i]))**3
print(D ** (1/3))

#p=∞
D=0
for i in range(n):
    if D < abs(x[i] - y[i]):
          D = abs(x[i] - y[i])
print(D)

Submission status: Accepted

Solution code:
import math
data_dim = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
D_1=0
D_2=0
D_3=0
D_inf=0

for i in range(data_dim):
    D_1 += abs(x[i]-y[i])
    D_2 += abs(x[i]-y[i])**2
    D_3 += abs(x[i]-y[i])**3
    D_inf = max(D_inf,abs(x[i]-y[i]))

print("%f"%(D_1))
print("%f"%(math.sqrt(D_2)))
print("%f"%(math.pow(D_3,1/3)))
print("%f"%(D_inf))

Submission status: Accepted

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

import math

def minkowski (x_list, y_list, p):
    """ ミンコフスキー距離 """
    
    # p=1:マンハッタン距離 / p=2:ユークリッド距離
    if p != math.inf:
        a = 0
        for x, y in zip(x_list, y_list):
            a += abs(x - y)** p
        return float(math.pow(a, 1 / p))
    
    # p=∞: チェビシェフ距離    
    else:
        d = 0
        for x, y in zip(x_list, y_list):
            if d < abs(x - y):
                d = abs(x - y)
        return float(d)

def main():
    n = int(input())
    x_list = list(map(int, input().split()))
    y_list = list(map(int, input().split()))
    p_list = [1, 2, 3, math.inf]
    
    for p in p_list:
        print(f"{minkowski(x_list, y_list, p):.6f}")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
n = int(input())
X = list(map(int,input().split()))
Y = list(map(int,input().split()))
table = []
three = 0

Manhattan = 0
Euclid = 0
Chebyshev = 0
for i in range(n):
    Manhattan += abs(X[i] - Y[i])
for i in range(n):
    tmp = abs(X[i] - Y[i])
    Euclid += tmp**2

Euclid_r = math.sqrt(Euclid)
for i in range(n):
    table.append(abs(X[i] - Y[i]))

Chebyshev = max(table)

for i in range(n):
    three += abs(X[i] - Y[i])**3

three_r = math.pow(three,1/3)

print(Manhattan)
print(Euclid_r)
print(three_r)
print(Chebyshev)

Submission status: Accepted

Solution code:
def minkowski_dist(vx, vy, p):
    if p == float("inf"):
        return max([abs(x - y) for x, y in zip(vx, vy)])
    else:
        return sum([abs(x - y)**p for x, y in zip(vx, vy)])**(1 / p)


n = int(input())
vx = [int(c) for c in input().split()]
vy = [int(c) for c in input().split()]
for p in [1, 2, 3, float("inf")]:
    print("%.6f" % minkowski_dist(vx, vy, p))

Submission status: Accepted

Solution code:
import math
dimension = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
for i in range(3):
    d_xy = 0
    b = 0
    for j in range(dimension):
        a = math.fabs(x[j]-y[j]) ** (i+1)
        b += a
    d_xy = b ** (1/(i+1))
    print(d_xy)
distance = []
for k in range(dimension):
    distance.append(math.fabs(x[k]-y[k]))
print(max(distance))

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:
if __name__ == '__main__':
	n = int(input())
	x = [float(s) for s in input().split()]
	y = [float(s) for s in input().split()]

	# p=1
	tmp = 0
	for i in range(n):
		tmp += abs(x[i]-y[i])
	print("%.10f" % (tmp))
	
	# p=2
	tmp = 0
	for i in range(n):
		tmp += (abs(x[i]-y[i]))**2
	print("%.10f" % (tmp**0.5))

	# p=3
	tmp = 0
	for i in range(n):
		tmp += (abs(x[i]-y[i]))**3
	print("%0.10f" % (tmp**(1/3)))

	# p=∞
	tmp = []
	for i in range(n):
		tmp.append(abs(x[i]-y[i]))
	print(max(tmp))

Submission status: Accepted

Solution code:
import math

n = int(input())
x = list(int(x) for x in input().split())
y = list(int(x) for x in input().split())

d = []
d2 = []
d3 = []
for i in range(n):
  d.append(abs(x[i] - y[i]))
  d2.append(abs(x[i] - y[i]) ** 2)
  d3.append(abs(x[i] - y[i]) ** 3)
  
print(sum(d))
print(sum(d2) ** (1/2))
print(sum(d3) ** (1/3))
print(max(d))

Submission status: Accepted

Solution code:
def Minkowski(x,y,n,p):
    s = 0
    for i in range(n):
        s += abs(x[i] - y[i])**p
    
    D = s**(1/p)
    print("{:.9}".format(D))
    
    return D

def Minkowski_inf(x,y,n):
    ma = 0

    for i in range(n):
        m = abs(x[i]-y[i])
        if(ma < m):
            ma = m
    
    D = ma
    print("{}".format(D))

    return D


n = int(input())

    
x = [int(i) for i in input().split(" ")]
y = [int(i) for i in input().split(" ")]
    
Minkowski(x,y,n,1)
Minkowski(x,y,n,2)
Minkowski(x,y,n,3)
Minkowski_inf(x,y,n)

Submission status: Accepted

Solution code:
import math

n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
for num in range(1, 4):
    z = []
    for X, Y in zip(x, y):
        z.append(abs((X - Y))**num)
    print((sum(z))**(1 / num))
print(max(abs(X - Y) for X, Y in zip(x, y)))

Submission status: Accepted

Solution code:
input()
x = list(map(float, input().split()))
y = list(map(float, input().split()))
manhattan = 0
euclidian = 0
minkowski3 = 0
chebyshev = 0
for i in range(len(x)):
    manhattan += abs(x[i]-y[i])
    euclidian += (x[i]-y[i])**2
    minkowski3 += abs(x[i]-y[i])**3
    chebyshev = max(chebyshev, abs(x[i]-y[i]))
euclidian = euclidian ** 0.5
minkowski3 = minkowski3 ** (1/3)
print(manhattan, euclidian, minkowski3, chebyshev, sep=('\n'))

Submission status: Accepted

Solution code:
input()
v = lambda: map(int,input().split())
d = [abs(x-y) for x,y in zip(v(),v())]
print(
	sum(d),
	sum(a**2 for a in d)**(1/2),
	sum(a**3 for a in d)**(1/3),
	max(d),
	sep='\n')

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
z=[abs(a-b) for (a,b) in zip(x,y)]
for i in range(1,4):
    print('{:.9}'.format(sum([j**i for j in z])**(1/i)))
print(max(z))

Submission status: Accepted

Solution code:
import math

n = int(input())

x = input().split()

y = input().split()

Manhattan = 0.0

Euclidean = 0.0

Chebyshev = 0.0

Minkowski = 0.0

for i in range(n):
  Manhattan += abs(int(x[i])- int(y[i]))

  Euclidean += math.pow((int(x[i])- int(y[i])), 2.0)

  Chebyshev += math.pow(abs(int(x[i])- int(y[i])), 3.0)

  if Minkowski < abs((int(x[i])- int(y[i]))): Minkowski = abs(int(x[i])-int(y[i])) 

Euclidean = math.sqrt(Euclidean)
Chebyshev = Chebyshev**(1/3)
  
print("%.8f"%Manhattan)
print("%.8f"%Euclidean)
print("%.8f"%Chebyshev)
print("%.8f"%Minkowski)

Submission status: Accepted

Solution code:
import math

n = int(input())

a = 0
b = 0
c = 0
d = 0
max = -9999999999

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

for i in range(n):
    if x[i] - y[i] < 0:
        a += y[i] - x[i]
    else:
        a += x[i] - y[i]

for i in range(n):
    b += (x[i] - y[i]) ** 2

c = math.sqrt(b)

for i in range(n):
    if x[i] - y[i] > 0:
        d += (x[i] - y[i]) ** 3
    else:
        d += (y[i] - x[i]) ** 3

e = d ** (1 / 3)

for i in range(n):
    if x[i] - y[i] < 0:
        if y[i] - x[i] > max:
            max = y[i] - x[i]
    else:
        if x[i] - y[i] > max:
            max = x[i] - y[i]

print("{:.6f}".format(a))
print("{:.6f}".format(c))
print("{:.6f}".format(e))
print("{:.6f}".format(max))

Submission status: Accepted

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

Submission status: Accepted

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

m_d = 0
y_d = 0
u_d = 0

mmm = [0]*n
md = 0
for i in range(n):
    
    md = abs(x[i]-y[i])
    m_d += md
    y_d += md**2
    u_d += md**3
    mmm[i] = md



print(m_d)
print(y_d**0.5)
print(u_d**(1/3))
print(max(mmm))

Submission status: Accepted

Solution code:
n = int(input())
x_l = list(map(int, input().split()))
y_l = list(map(int, input().split()))
p1 = sum(abs(x_l[i]-y_l[i]) for i in range(n))
p2 = (sum(abs(x_l[i]-y_l[i])**2 for i in range(n)))**0.5
p3 = (sum(abs(x_l[i]-y_l[i])**3 for i in range(n)))**(1/3)
p_max = max(abs(x_l[i]-y_l[i]) for i in range(n))
print(p1, p2, p3, p_max)

Submission status: Accepted

Solution code:
import math

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("%.6f"%(Dxy))

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:
#ベクトルの次元数n
n = int(input())

#ベクトルx
x = list(map(int,input().split()))

#ベクトルy
y = list(map(int,input().split()))


#p==1
Dxy = 0
for i in range(n):
    Dxy += abs(x[i]-y[i])
print(Dxy)

#p==2
Dxy = 0
s1 = 0
for i in range(n):
    s1 += (x[i]-y[i])**2
Dxy = s1**(1/2)
print(Dxy)

#p==3
Dxy = 0
s1 = 0
for i in range(n):
    s1 += abs(x[i]-y[i])**3
Dxy = s1**(1/3)
print(Dxy)

#p==infinity
Dxy = 0
s1 = 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_datas = list(map(int, input().split()))
y_datas = list(map(int, input().split()))
length = len(x_datas)

# p=1の時
# ミンコフスキー距離を求める
D1 = 0
for i in range(length):
    D1 += math.fabs(x_datas[i] - y_datas[i])
print(D1)

# p=2の時
# マンハッタン距離を求める
D2_sum = 0
for i in range(length):
    D2_sum += math.fabs(x_datas[i] - y_datas[i]) ** 2
    D2 = math.sqrt(D2_sum)
print(D2)

# p=3の時
D3_sum = 0
for i in range(length):
    D3_sum += math.fabs(x_datas[i] - y_datas[i]) ** 3
    D3 = pow(D3_sum, 1/3)
print(D3)

# p =∞の時
# ユークリッド距離を求める
D4 = []
for i in range(length):
    D4.append(math.fabs(x_datas[i] - y_datas[i]))
print(max(D4))

Submission status: Accepted

Solution code:
import math

def dist(x,y,N,M):
    sum = 0
    for n in range(N):
        sum += abs(x[n]-y[n])**M
    sum = sum**(1/M)
    
    return sum
    
def dist_inf(x,y,N):
    return max([abs(x[n]-y[n]) for n in range(N)])


N = int(input())
x = [int(i) for i in input().split()]
y = [int(j) for j in input().split()]

print(dist(x,y,N,1))
print(dist(x,y,N,2))
print(dist(x,y,N,3))
print(dist_inf(x,y,N))

Submission status: Accepted

Solution code:
import math

N=int(input())
X=list(map(int,input().split()))
Y=list(map(int,input().split()))
    
for j in range(1,4):
    sum=0
    for i in range(N):
        sum+=(abs(X[i]-Y[i]))**j
    D=math.pow(sum,1/j)
    print(D)
    
dist=0
for i in range(N):
    kyori=abs(X[i]-Y[i])
    if dist<=kyori:
        dist=float(kyori)
print(dist)

Submission status: Accepted

Solution code:
n = input()
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(1, 5):
    if i==4: print(max(list(map(lambda x,y: abs(x-y), a, b))))
    else: print(sum(list(map(lambda x,y: abs(x-y)**i, a, b)))**(1/i))

Submission status: Accepted

Solution code:
import math


n = int(input())

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

#p=1
x_y_1 = [abs(x[i] - y[i]) for i in range(n)]
D_1 = sum(x_y_1)
print(float(D_1))
#p=2
x_y_2 = [abs(x[i] - y[i])**2 for i in range(n)]
D = sum(x_y_2)
D_2 = pow(D, 1/2)
print(D_2)

#p=3
x_y_3 = [abs(x[i] - y[i])**3 for i in range(n)]
D = sum(x_y_3)
D_3 = pow(D, 1/3)
print(D_3)

#p=infty
D_inf = max(x_y_1)
print(float(D_inf))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
for i in range(1,4):
    a = 0
    for j in range(n):
        if x[j] - y[j] < 0:
            a += (y[j] - x[j]) ** i
        else:
            a += (x[j] - y[j]) ** i
    a = a ** (1/i)
    print(a)
b = 0
for k in range(n):
    c = x[k] - y[k]
    if c < 0:
        c = c * -1
    if b < c:
        b = c
print(b)

Submission status: Accepted

Solution code:
import math
num = int(input())
x = [int(_) for _ in input().split()]
y = [int(_) for _ in input().split()]
p1 = p2 = p3 = pe = max_num= 0
for i in range(num):
    p1 += abs(x[i]-y[i])
    p2 += math.pow(abs(x[i]-y[i]),2)
    p3 += math.pow(abs(x[i]-y[i]),3)
    if max_num < abs(x[i]-y[i]):
        max_num = abs(x[i] - y[i])
else:
    p2 = math.sqrt(p2)
    p3 = math.pow(p3,1/3)
    pe = max_num
print("%f\n%f\n%f\n%f" %(p1,p2, p3,pe))

Submission status: Accepted

Solution code:
from math import sqrt

_ = input()
x = [float(i) for i in input().split()]
y = [float(i) for i in input().split()]

def f1(x, y):
    return sum([abs(t[0] - t[1]) for t in zip(x, y)])
    
def f2(x, y):
    return sqrt(sum([pow(t[0] - t[1], 2) for t in zip(x, y)]))

def f3(x, y):
    return pow(sum([ pow(abs(t[0] - t[1]), 3) for t in zip(x, y)]), 1.0 / 3.0)
    
def f4(x, f):
    return max([abs(t[0] - t[1]) for t in zip(x, y)])
    
print(f1(x, y))
print(f2(x, y))
print(f3(x, y))
print(f4(x, y))

Submission status: Accepted

Solution code:
from math import sqrt

_ = input()
x = [ float(i) for i in input().split() ]
y = [ float(i) for i in input().split() ]

def f1(x, y):
    return sum([ abs(t[0] - t[1]) for t in zip(x, y) ])

def f2(x, y):
    return sqrt(sum([ pow(t[0] - t[1], 2) for t in zip(x, y) ]))

def f3(x, y):
    return pow(sum([ pow(abs(t[0] - t[1]), 3) for t in zip(x, y) ]), 1.0 / 3.0)

def f4(x, y):
    return max([abs(t[0] - t[1]) for t in zip(x, y)])

print(f1(x, y))
print(f2(x, y))
print(f3(x, y))
print(f4(x, y))

Submission status: Accepted

Solution code:
import math

n = int(input())

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

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

d1 = float(sum(dist))
d2 = 0.0
d3 = 0.0
for i in range(n):
  d2 += dist[i]**2
  d3 += dist[i]**3
d2 = float(math.sqrt(d2))
d3 = float(d3**(1.0/3.0))
d_inf = float(max(dist))

print("{:.6f}".format(d1))
print("{:.6f}".format(d2))
print("{:.6f}".format(d3))
print("{:.6f}".format(d_inf))

Submission status: Accepted

Solution code:
p1 = 0
p2Tmp = 0
p3Tmp = 0
pInfElement = []
dataNum = int(input())
x = [float(i) for i in input().split()]
y = [float(i) for i in input().split()]

for index in range(dataNum):
    p1 += abs(x[index] - y[index])
    p2Tmp += ((x[index] - y[index])**2)
    p3Tmp += (abs(x[index] - y[index])**3)
    pInfElement.append(abs(x[index] - y[index]))

p2 = p2Tmp **(1/2)
p3 = p3Tmp **(1/3)
pInf = max(pInfElement)

print(p1)
print(p2)
print(p3)
print(pInf)

Submission status: Accepted

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

D1 = 0
D2 = 0
D3 = 0
D = []

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

print(D1)
print(D2**(1/2))
print(D3**(1/3))
print(max(D))

Submission status: Accepted

Solution code:
# coding: utf-8
import math

def Manhattan(n, *d):
    sum = 0
    for i in range(n):
        sum += d[i]
    return sum

def Euclid(n, *d):
    sum = 0
    for i in range(n):
        sum += d[i]**2
    return math.sqrt(sum)

def Minkowski3(n, *d):
    sum = 0
    for i in range(n):
        sum += d[i]**3
    return math.pow(sum, 1/3)

def Chebichev(n, *d):
    return max(d)

n = int(input().rstrip())
x = list(map(float, input().rstrip().split()))
y = list(map(float, input().rstrip().split()))
d = []
for i in range(n):
    d.append(abs(x[i] - y[i]))
print("{0:.5f}".format(Manhattan(n,*d)))
print("{0:.5f}".format(Euclid(n,*d)))
print("{0:.5f}".format(Minkowski3(n,*d)))
print("{0:.5f}".format(Chebichev(n,*d)))

Submission status: Accepted

Solution code:
num = int(input())
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))

mh = sum(abs(x-y) for x, y in zip(xs, ys))
ec = pow(sum(abs(x-y)**2 for x, y in zip(xs, ys)), 1/2)
p3 = pow(sum(abs(x-y)**3 for x, y in zip(xs, ys)), 1/3)
cv = max(abs(x-y) for x, y in zip(xs, ys))

print(round(mh, 8))
print(round(ec, 8))
print(round(p3, 8))
print(round(cv, 8))

Submission status: Accepted

Solution code:
import math

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

sum1 = 0
sum2 = 0
sum3 = 0
inf = 0
for i in range(n):
    tmp = abs(x[i] - y[i])
    sum1 += tmp
    sum2 += tmp ** 2
    sum3 += tmp ** 3
    inf = max(inf, tmp)
print("{:.6f}".format(sum1))
print("{:.6f}".format(sum2 ** (1 / 2)))
print("{:.6f}".format(sum3 ** (1 / 3)))
print("{:.6f}".format(inf))

Submission status: Accepted

Solution code:
from math import inf
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
def dist(x,y,k):
    if k == inf:
        d = 0
        for i in range(n):
            tmp = abs(x[i] - y[i])
            if tmp > d:
                d = tmp
        return d
    d = 0
    for i in range(n):
        d += (abs(x[i] - y[i]))**k
    return d**(1/k)
print(dist(x,y,1))
print(dist(x,y,2))
print(dist(x,y,3))
print(dist(x,y,inf))

Submission status: Accepted

Solution code:
num = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
sum1 = 0
sum2 = 0
sum3 = 0
sum4 = 0

for i in range(num):
        sum1 += abs(x[i] - y[i])
        sum2 += abs(x[i] - y[i]) ** 2
        sum3 += abs(x[i] - y[i]) ** 3
        if sum4 < abs(x[i] - y[i]):
                sum4 = abs(x[i] - y[i])

print("{:.5f}".format(sum1))
print("{:.5f}".format(pow(sum2, 1 / 2)))
print("{:.5f}".format(pow(sum3, 1 / 3)))
print("{:.5f}".format(sum4))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
d1, d2, d3 = 0, 0, 0
dm = []
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
    dm.append(abs(x[i] - y[i]))
print(d1)
print(d2 ** 0.5)
print(d3 ** (1/3))
print(max(dm))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input()
x = [float(i) for i in input().split()]
y = [float(i) for i in input().split()]
print("{:.6f}".format(sum([abs(i - j) for i, j in zip(x, y)])))
print("{:.6f}".format(sum([(i - j) ** 2 for i, j in zip(x, y)]) ** 0.5))
print("{:.6f}".format(sum([abs(i - j) ** 3 for i, j in zip(x, y)]) ** (1/3)))
print("{:.6f}".format(max([abs(i - j) for i, j in zip(x, y)])))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
def euc(p):
    return sum([abs(i-j)**p for i,j in zip(a,b)])**(1/p)
for i in range(1,4):
    print("{:.8f}".format(euc(i)))
print("{:.8f}".format(max([abs(i-j) for i,j in zip(a,b)])))

Submission status: Accepted

Solution code:
import math


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

d = []
for i in range(n):
    dis = math.fabs(a[i]-b[i])
    d.append(dis)

print(sum(d))

dd_sum = 0
for i in range(n):
    dd_sum += d[i]**2
print(math.sqrt(dd_sum))

ddd_sum = 0
for i in range(n):
    ddd_sum += d[i]**3
print(math.pow(ddd_sum,(1.0/3.0)))

print(max(d))

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):
    print(sum([abs(a[j]-b[j])**i for j in range(n)])**(1/i))
print(max([abs(a[i]-b[i]) for i in range(n)]))

Submission status: Accepted

Solution code:
dim = int(input())
x = [int(s) for s in input().split()]
y = [int(s) for s in input().split()]
one = 0
two = 0
three = 0
infinity = 0
for i in range(dim):
    term = abs(x[i] - y[i])
    one += term
    two += term ** 2
    three += term ** 3
    infinity = max(infinity, term)
print(round(one, 6))
print(round(two ** (1 / 2), 6))
print(round(three ** (1/3), 6))
print(infinity)

Submission status: Accepted

Solution code:
import sys
import math

n = int(sys.stdin.readline())
x, y = (list(map(int, line.strip().split())) for line in sys.stdin.readlines()[0:2])


def distance(p):
    return math.pow(sum(abs(x[i] - y[i]) ** p for i in range(n)), 1/p)


def distance_inf():
    return max(abs(x[i] - y[i]) for i in range(n))


for i in range(1, 4):
    print('{0:.8f}'.format(distance(i)))

print('{0:.8f}'.format(distance_inf()))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def minkowski_distance(arr1, arr2, p):
    if p == 'inf':
        cmp_arr = [abs(x-y) for x, y in zip(arr1, arr2)]
        return max(cmp_arr)
    sum_cmp_arr = sum([pow(abs(x-y), p) for x, y in zip(arr1, arr2)])
    return pow(sum_cmp_arr, 1/p)


n = int(input())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
for p in [1, 2, 3, 'inf']:
    print(f'{minkowski_distance(arr1,arr2,p):.8f}')

Submission status: Accepted

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

d1=[]
p1=0
p2=0
p3=0
pi=0

for i in range(n):
    di=abs(x[i]-y[i])
    p1=p1+di
    p2=p2+pow(di,2)
    p3=p3+pow(di,3)
    if di>pi:
        pi=di

p2=math.sqrt(p2)
p3=pow(p3,(1.0/3.0))

print(p1)
print(p2)
print(p3)
print(pi)

Submission status: Accepted

Solution code:

from functools import reduce
import math


n = int(input())
l1  =  list(map(int,input().split()))
l2  =  list(map(int,input().split()))

sum1 = sum2 = sum3 = 0
max = 0
for i in range(0,n):
    sum1 += abs(l1[i] - l2[i])
    sum2 += pow(abs(l1[i] - l2[i]),2)
    sum3 += pow(abs(l1[i] - l2[i]),3)
    if ( max < abs(l1[i] - l2[i])):
        max = abs(l1[i] - l2[i])


print (sum1)
print (math.sqrt(sum2))
print (math.pow(sum3,1.0/3.0))
print (max*1.0)

Submission status: Accepted

Solution code:
import math
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
d1,d2,d3,d_inf=0,0,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
    d_inf.append(abs(x[i]-y[i]))
print(d1)
print(d2**(1/2))
print(d3**(1/3))
print(max(d_inf))

Submission status: Accepted

Solution code:
import math
import statistics

n = int(input())

xlist = list(map(int, input().split()))
ylist = list(map(int, input().split()))

p1 = 0
p2 = 0
p3 = 0
plist = []

for x, y in zip(xlist, ylist):
    p1 += abs(x - y)
    p2 += (abs(x - y)**2)
    p3 += (abs(x - y)**3)
    plist.append(abs(x - y))

p2 = math.sqrt(p2)
p3 = math.pow(p3, 1/3)
p4 = max(plist)

print(p1)
print(p2)
print(p3)
print(p4)

Submission status: Accepted

Solution code:
n = int(input())
x_list = list(map(float,input().split()))
y_list = list(map(float,input().split()))

print(sum([abs(x_list[i]-y_list[i]) for i in range(n)]))
print(pow(sum([abs(x_list[i]-y_list[i])**2 for i in range(n)]),1/2))
print(pow(sum([abs(x_list[i]-y_list[i])**3 for i in range(n)]),1/3))
print(max([abs(x_list[i]-y_list[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()))
for j in range(3):
	w=0
	dist=0
	dist_2=[]
	for i in range(n):
		w=abs(x[i]-y[i])
		dist+=w**(j+1)
		dist_2.append(w)
	print(pow(dist,1/(j+1)))
print(float(max(dist_2)))

Submission status: Accepted

Solution code:
import math

def dist(z,p):
    sigma=0
    for i in range(0,len(z)):
        sigma += z[i]**p
    print(sigma**(1/p))


n=int(input())
x=list(map(float,input().split()))
y=list(map(float,input().split()))
z=[abs(x[i]-y[i]) for i in range(0,n)]
dist(z,1)
dist(z,2)
dist(z,3)
print(max(z))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def non_minus(n):
    if n < 0:
        return (-1) * n
    else:
        return n

def kyori(n1, n2):
    n1 = non_minus(n1)
    n2 = non_minus(n2)
    n3 = non_minus(n1 - n2)
    return n3

def minkov(x: list, y: list, p: int):
    len_x = len(x)
    dm = 0
    for i in range(len_x):
        dm += kyori(x[i], y[i])**p
    dxy = dm**(1/p)
    return dxy

def chebyshev(x: list, y: list):
    d_ary = []
    len_x = len(x)
    for i in range(len_x):
        d_ary.append(kyori(x[i], y[i]))
    
    return max(d_ary)

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

print(minkov(x,y,1))
print(minkov(x,y,2))
print(minkov(x,y,3))
print(float(chebyshev(x,y)))

Submission status: Accepted

Solution code:
input()
x=list(map(int,input().split()))
z=[abs(x[i]-y) for i,y in enumerate(map(int, input().split()))]
print(sum(z),sum([u**2 for u in z])**(1/2),sum([u**3 for u in z])**(1/3),max(z),sep='\n')

Submission status: Accepted

Solution code:
from math import sqrt
n = int(input())
x = input().split()
y = input().split()
max_num = -1
D_p1 = 0
D_p2 = 0
D_p3 = 0

for i in range(0, n):
    x[i] = abs(float(x[i]) - float(y[i]))
    y[i] = x[i] ** 2
    D_p1 += x[i]
    D_p2 += y[i]
    D_p3 += x[i] ** 3
    if (x[i] > max_num):
        max_num = x[i]

print(D_p1)
print(sqrt(D_p2))
print(pow(D_p3, 1.0 / 3))
print(max_num)

Submission status: Accepted

Solution code:
import sys

n = int(input())
Vx = list(map(int, input().split()))
Vy = list(map(int, input().split()))
maxList = []

plist = [1, 2, 3]
for p in plist:
    Sum = 0
    for i in range(n):
        Sum += abs(Vx[i] - Vy[i])**p
        D = Sum**(1/p)
    print(D)

D = 0
for i in range(n):
    maxList.append(abs(Vx[i] - Vy[i]))
    
print(max(maxList))

Submission status: Accepted

Solution code:
import math

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

for i in range(1,4):
	sum=0
	for j in range(n):
		sum+=pow(math.fabs(x[j]-y[j]),i)
	print(str(pow(sum,1/i)))

infD=-1
for i in range(n):
	infD=max(math.fabs(x[i]-y[i]),infD)
print(float(infD))

Submission status: Accepted

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

tmp = 0
for i in range(n):
    tmp+=abs(x[i]-y[i])
print(tmp)
tmp = 0
for i in range(n):
    tmp+=(x[i]-y[i])**2
print(math.sqrt(tmp))
tmp = 0
for i in range(n):
    tmp+=abs(x[i]-y[i])**3
print(tmp**(1/3))
tmp = 0
for i in range(n):
    tmp = max(tmp, abs(x[i]-y[i]))
print(tmp)

Submission status: Accepted

Solution code:
import math

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

for p in range(1, 4):
    D = sum([abs(x[i]-y[i]) ** p for i in range(n)]) ** (1/p)
    print(D)

print(max([abs(x[i]-y[i]) for i in range(n)]))