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(float, input().split()))
y = list(map(float, input().split()))

d1 = 0
for i in range(n):
    d1 += abs(x[i] - y[i])
print('{:.8f}'.format(d1))

d2 = 0
for i in range(n):
    d2 += abs(x[i] - y[i])**2
print('{:.8f}'.format(d2**(1/2)))

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

d = []
p = float('inf')
for i in range(n):
    d.append(abs(x[i] - y[i]))
print('{:.8f}'.format(max(d)))

Submission status: Accepted

Solution code:
import math

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

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

dp1 = sum(delta)
print(dp1)

dp2 = 0.0
for i in range(n):
  dp2 += delta[i]**2
dp2 = math.sqrt(dp2)
print(dp2)

dp3 = 0.0
for i in range(n):
  dp3 += delta[i]**3
dp3 = math.pow(dp3,1/3)
print(dp3)

dpe = 0.0
for i in range(n):
  dpe = max(dpe,delta[i])
print(dpe)

Submission status: Accepted

Solution code:
n = int(input())
x_list = list(map(int, input().split()))
y_list = list(map(int, input().split()))
list_sa = [abs(x_list[i] - y_list[i]) for i in range(n)]
list_sa2 = [pow(i, 2) for i in list_sa]
list_sa3 = [pow(i, 3) for i in list_sa]
print(sum(list_sa))
print(pow(sum(list_sa2), 1/2))
print(pow(sum(list_sa3), 1/3))
print(max(list_sa))

Submission status: Accepted

Solution code:
import math

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

d1 = 0
for i in range(n):
    d1 += abs(x[i]-y[i])
print('{:.7f}'.format(d1))

d2 = 0
for i in range(n):
    d2 += abs(x[i]-y[i])**2
print('{:.7f}'.format(math.sqrt(d2)))

d3 = 0
for i in range(n):
    d3 += abs(x[i]-y[i])**3
print('{:.7f}'.format(d3**0.3333333333))

di = []
for i in range(n):
    di.append(abs(x[i]-y[i]))
print('{:.7f}'.format(max(di)))

Submission status: Accepted

Solution code:
# coding: utf-8
#72

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

D=[]
for j in range(n):
    d=abs(X[j]-Y[j])
    D.append(d)
print('{:.05f}'.format(sum(D)))

E=[]
for i in range(n):
    e=D[i]**2
    E.append(e)
A=sum(E)**(1/2)
print('{:.05f}'.format(A))

F=[]
for i in range(n):
    f=D[i]**3
    F.append(f)
B=sum(F)**(1/3)
print('{:.05f}'.format(B))

print('{:.05f}'.format(max(D)))

Submission status: Accepted

Solution code:
def Distance(X,Y,p):
    s=0
    for x,y in zip(X,Y):
        s+=abs(x-y)**p
    print(s**(1/p))
    
n=int(input())
X=list(map(int,input().split()))
Y=list(map(int,input().split()))

for p in range(1,4):
    Distance(X,Y,p)
print(max(abs(x-y) for (x,y) in zip(X,Y)))

Submission status: Accepted

Solution code:
def p(t):
    print(f'{t:.6f}')

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

D1=0
D2=0
D3=0
D4=0

for i in range(n):
    d=x[i]-y[i]
    if d<0:
        d=-d
    
    D1+=d
    D2+=d**2
    D3+=d**3
    if d>D4:
        D4=d
        
p(D1)
p(D2**(1/2))
p(D3**(1/3))
p(D4)

Submission status: Accepted

Solution code:
#1_10_D
S = []
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
for i in range(1,4):
    s = 0
    for j in range(n):
        a = abs(x[j] - y[j])
        S.append(a)
        s += a ** i
    D = s ** (1/i)
    print('{:.06f}'.format(D))
print('{:.06f}'.format(max(S)))

Submission status: Accepted

Solution code:
from math import sqrt, pow
from operator import sub


def sub_(t):
    return abs(sub(*t))


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

print(sum(map(sub_, zip(x, y))))
print(sqrt(sum(map(lambda t: sub_(t) ** 2, zip(x, y)))))
print(pow(sum(map(lambda t: sub_(t) ** 3, zip(x, y))), 1/3))
print(max(map(sub_, zip(x, y))))

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:
# 2つのデータがどれだけ似ているか？ミンコフスキー距離
# x={x1, x2, ..., xn}
# y={y1, y2, ..., yn}
import math

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

# p=1 マンハッタン距離
print(sum([abs(i-j) for i, j in zip(x, y)]))
# p=2 ユークリッド距離
print(math.sqrt(sum([(i-j)**2 for i, j in zip(x, y)])))
# p=3
print((sum([abs(i-j)**3 for i, j in zip(x, y)]))**(1.0/3.0))
# print(math.pow(sum([abs(i-j)**3 for i, j in zip(x, y)]), 1/3))
# 上記でも同様の計算となる。
# p=∞ チェビシェフ距離
print(max([abs(i-j) for i, j in zip(x, y)]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
import math

n = int(stdin.readline().rstrip())
lx = list(map(int, stdin.readline().split()))
ly = list(map(int, stdin.readline().split()))

s1=s2=s3=max_x_y = 0
for i in range(n):
    x_y = abs(lx[i] - ly[i])
    #p=1
    s1+=x_y
    #p=2
    s2 += math.pow(x_y,2)
    #p=3
    s3 += math.pow(x_y, 3)
    #p=∞
    if max_x_y < x_y:
        max_x_y = x_y
else:
    #p=1
    print("{0:.8f}".format(s1))
    #p=2
    d2 = math.sqrt(s2)
    print("{0:.8f}".format(d2))
    #p=3
    d3 = math.pow(s3, 1.0 / 3.0)
    print("{0:.8f}".format(d3))
    #p=∞
    print(max_x_y)

Submission status: Accepted

Solution code:

n = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))
xy = [0 for i in range(n)]; xy2 = [0 for i in range(n)]; xy3 = [0 for i in range(n)]

for  i in range(n):
    xy[i] = x[i]-y[i]
    if xy[i]<0:
        xy[i] *= -1
    xy2[i] = xy[i]**2
    xy3[i] = xy[i]**3
    
print("{0:.6f}\n{1:.6f}\n{2:.6f}\n{3:.6f}".format(sum(xy),(sum(xy2)**(1/2)),(sum(xy3)**(1/3)),max(xy)))

Submission status: Accepted

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

p1 = 0
for i in range(n):
    p1 += abs(X[i]-Y[i])
    
p2 = 0
for i in range(n):
    p2 += (abs(X[i]-Y[i]))**2
p2 = p2 ** 0.5

p3 = 0
for i in range(n):
    p3 += (abs(X[i]-Y[i]))**3
p3 = p3 ** (1/3)


p4 = 0
for i in range(n):
    if p4 < abs(X[i]-Y[i]):
        p4 = abs(X[i]-Y[i])
    
print(p1)
print(p2)
print(p3)
print(p4)

Submission status: Accepted

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

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

z = 0
for i in range(len(x)):
    z += (abs(x[i]-y[i]))**2
print(z**0.5)

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

z1 = 0
z2 = 0
for i in range(len(x)):
    z1 = abs(x[i]-y[i])
    if z1 > z2:
        z2 = z1
print(z2)

Submission status: Accepted

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

for j in range(1,4):
    D=0
    for i in range(n):
        D+=(abs(a[i]-b[i]))**j
    D**=1/j
    print('{:.6f}'.format(D))
lst=[]
for k in range(n):
    lst.append(abs(a[k]-b[k]))
print('{:.6f}'.format(max(lst)))

Submission status: Accepted

Solution code:
n = int(input())

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

ans1 = 0
ans2 = 0
ans3 = 0
ansx = []

for x in range(n):
    ans1 += abs(X[x]-Y[x])
    ans2 += abs(X[x]-Y[x])**2
    ans3 += abs(X[x]-Y[x])**3
    ansx.append(abs(X[x]-Y[x]))
print(ans1,ans2**0.5,ans3**(1.0/3),max(ansx), sep = "\n")

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
L1,L2,L3 = 0.0,0.0,0.0
L = []
for i in range(n):
    L1 += abs(x[i]-y[i])
    L2 += (x[i]-y[i])**2
    L3 += abs(x[i]-y[i])**3
    L .append(abs(x[i]-y[i]))
print(L1)
print(math.sqrt(L2))
print(math.pow(L3, 1.0/3.0))    
print(max(L))

Submission status: Accepted

Solution code:
import sys
import math

input = sys.stdin.readline

def main():
    n = int(input().rstrip())
    x = list(map(int, input().rstrip().split()))
    y = list(map(int, input().rstrip().split()))
    
    d1, d2, d3 = 0, 0, 0
    temp_y = y
    
    for i in range(n):
        d1 += math.fabs(x[i] - y[i])
        d2 += math.fabs(x[i] - y[i]) ** 2
        d3 += math.fabs(x[i] - y[i]) ** 3
        temp_y[i] = math.fabs(x[i] - y[i])
    
    print(d1)
    print(math.sqrt(d2))
    print(d3**(1/3))
    print(max(temp_y))
    
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()))

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:
import math

def lp_norm(x,y,p):
    if p==float("inf"):
        z=[]
        for i in range(len(x)):
            z.append(abs(x[i]-y[i]))
        return max(z)
        
    else:
        z=[]
        for i in range(len(x)):
            z.append(math.pow(abs(x[i]-y[i]),p))
            
        s=sum(z)
            
        return math.pow(s,1.0/p)
        

def run():
    
    n=input()
    x=list(map(float,input().split()))
    y=list(map(float,input().split()))
    
    p=float("inf")
    print(f"{lp_norm(x,y,1.0)}")
    print(f"{lp_norm(x,y,2.0)}")
    print(f"{lp_norm(x,y,3.0)}")
    print(f"{lp_norm(x,y,p)}")
        
run()

Submission status: Accepted

Solution code:
n = int(input())

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

din = [0,0,0,0]

for i in range(n):
	din[0] += abs(x[i] - y[i])
	din[1] += abs(x[i] - y[i])**2
	din[2] += abs(x[i] - y[i])**3
	din[3] = max(din[3],abs(x[i] - y[i]))
din[1] = din[1]**(1/2)
din[2] = din[2]**(1/3)
for i in range(4):
	print('{:.6f}'.format(din[i]))

Submission status: Accepted

Solution code:
n=int(input())
xx=list(map(int,input().split()))
yy=list(map(int,input().split()))

p1 = 0
p2 = 0
p3 = 0
p8 = []

for idx in range(n):
    s = abs(xx[idx]-yy[idx])
    p1 += s
    p2 += s**2
    p3 += s**3
    p8.append(s)

print(p1)
print(p2**(1/2))
print(p3**(1/3))
print(max(p8))

Submission status: Accepted

Solution code:


num=int(input())
b1=[int(x) for x in input().split(' ')]
b2=[int(x) for x in input().split(' ')]



def c_minchov(x,y,p):
	b1,b2=x,y
	fsum=0
	for i in range(len(b1)):
		fsum+=abs(b1[i]-b2[i])**p
	res=fsum**(1/p)
	return res

def c_chebichev(x,y):
	b1,b2=x,y
	dis=[abs(b1[i]- b2[i]) for i in range(len(b1))]

	res= max(dis)
	return res


p1=c_minchov(b1,b2,1)
p2=c_minchov(b1,b2,2)
p3=c_minchov(b1,b2,3)
p4=c_chebichev(b1,b2)
print("{:.6f}".format(p1))
print("{:.6f}".format(p2))
print("{:.6f}".format(p3))
print("{:.6f}".format(p4))

Submission status: Accepted

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

# p = 1
d1 = 0
r = []
for i in range(n):
    d = abs(x[i] - y[i])
    d1 += d
    r.append(d)

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

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

# p = ∞
d_inf = max(r)

print(d1)
print(d2)
print(d3)
print(d_inf)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

n = int(input())

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

D1 = 0
D2 = 0
D3 = 0
max = 0
for i in range(n):
   diff = abs(X[i] - Y[i])
   D1 += diff
   D2 += diff**2
   D3 += diff**3
   if diff > max:
      max = diff

print('{:.6f}'.format(D1))
print(D2**(1 / 2))
print(D3**(1.0 / 3.0))
print('{:.6f}'.format(max))

Submission status: Accepted

Solution code:
import math
def minkowskiDistance(p, x, y):
    distance, sum = 0, 0
    for i, j in zip(x, y):
        sum += math.fabs(i- j) ** p
    distance = math.pow(sum, 1 / p)
    return distance
def chebyshevDistance(x, y):
    distance = []
    for i, j in zip(x, y):
        distance.append(math.fabs(i - j))
    return max(distance)

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

print(minkowskiDistance(1, x, y))
print(minkowskiDistance(2, x, y))
print(minkowskiDistance(3, x, y))
print((chebyshevDistance(x, y)))

Submission status: Accepted

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

def D(x,y,p):
    s = 0
    for i in range(n):
        s += abs(x[i]-y[i])**p
    return s**(1/p)
mugen = []
for k in range(n):
    mugen.append(abs(x[k]-y[k]))

l = [1,2,3]
a = []
for j in l :
    a.append(D(x,y,j))
a.append(max(mugen))
print(*a)

Submission status: Accepted

Solution code:
import math
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
p1=0; p2=0; p3=0; pinf=0
for i in range(n):
    u=abs(x[i]-y[i])
    p1+=u
    p2+=pow(u,2)
    p3+=pow(u,3)
    if pinf<u:
        pinf=u
p2=math.sqrt(p2)
p3=pow(p3,1/3)
print("{0:.6f}".format(p1))
print("{0:.6f}".format(p2))
print("{0:.6f}".format(p3))
print("{0:.6f}".format(pinf))

Submission status: Accepted

Solution code:
import math

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

m = [abs(x[i] - y[i]) for i in range(n)]
m_d = sum(m)

m2 = [abs(x[i] - y[i]) ** 2 for i in range(n)]
m_u= math.sqrt(sum(m2))

m3=[abs(x[i] - y[i]) ** 3 for i in range(n)]
m_3= (sum(m3))**(1/3)

mi= [abs(x[i] - y[i]) for i in range(n)]
m_c=max(mi)

print('{:.6f}'.format(m_d))
print('{:.6f}'.format(m_u))
print('{:.6f}'.format(m_3))
print('{:.6f}'.format(m_c))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
#p = 1
now_1 = 0
for i in range(n):
    now_1 += float(abs(x[i]-y[i]))
print("{:.10}".format(now_1))
#p = 2
now_2 = 0
for i in range(n):
    now_2 += float(abs(x[i]-y[i])**2)
print("{:.10}".format(now_2**0.5))
# p = 3
now_3 = 0
for i in range(n):
    now_3 += float(abs(x[i]-y[i])**3)
print("{:.10}".format(now_3**(1/3)))
# p = 4
ls = []
for i in range(n):
    ls.append(float(abs(x[i]-y[i])))
print("{:.10}".format(max(ls)))

Submission status: Accepted

Solution code:
n = int(input())

x = list(map(float,input().split()))
y = list(map(float,input().split()))
D = []

sm = 0.0

for i in range(n):
    D.append(abs(x[i]-y[i]))
print('{:.08f}'.format(sum(D)))

for i in range(n):
    sm +=(x[i]-y[i])**2
print('{:.08f}'.format(sm**0.5))

sm = 0.0
for i in range(n):
    sm +=abs(x[i]-y[i])**3

print('{:.08f}'.format(sm**(1.0/3.0)))

print('{:.08f}'.format(max(D)))

Submission status: Accepted

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

def dist(x,y,p):
    val = 0
    for xi,yi in zip(x,y):
        val += (abs(xi-yi))**p
    return val**(1/p)
    
def cheb(x,y):
    val = 0
    for xi,yi in zip(x,y):
        val_tmp = abs(xi-yi)
        if val_tmp>val:
            val = val_tmp
    return val
    
print(dist(X,Y,p=1))
print(dist(X,Y,p=2))
print(dist(X,Y,p=3))
print(cheb(X,Y))

Submission status: Accepted

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

D1 = 0
for i in range(n):
    D1 += abs(x[i] - y[i])
D2 = 0
for i in range(n):
    D2 += (x[i] - y[i]) ** 2
D2 = math.sqrt(D2)
D3 = 0
for i in range(n):
    D3 += (abs(x[i] - y[i])) ** 3
D3 = math.pow(D3, 1.0/3.0)
Dinf = []
for i in range(n):
    Dinf.append(abs(x[i]- y[i]))
Dinf = max(Dinf)

print(format(D1, '.6f'))
print(format(D2, '.6f'))
print(format(D3, '.6f'))
print(format(Dinf, '.6f'))

Submission status: Accepted

Solution code:
import math

n = int(input())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
D = 0
z = []

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

D = 0
#p = 2
for i in range(n):
    D += (abs(x[i] - y[i])) ** 2
D = math.sqrt(D)
print(D)

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

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

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    x = [int(i) for i in input().split()]
    y = [int(i) for i in input().split()]
    D1 = 0
    D2 = 0
    D3 = 0
    D4 = 0
    for i in range(n):
        D1 += abs(x[i] - y[i])
        D2 += (x[i] - y[i])**2
        D3 += abs(x[i] - y[i])**3
        D4 = max(D4, abs(x[i] - y[i]))
    D2 = D2**(1 / 2)
    D3 = D3**(1 / 3)
    print(D1 / 1)
    print(D2 / 1)
    print(D3 / 1)
    print(D4 / 1)


resolve()

Submission status: Accepted

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

n_dis = [0 for _ in range(3)]
elem_dis = [abs(x[i]-y[i]) for i in range(n)]

for p in range(3):
    for i in range(n):
        n_dis[p] += elem_dis[i]**(p+1)
    n_dis[p] = n_dis[p]**(1/(p+1))
    print(n_dis[p])
    
index = [i for i, j in enumerate(elem_dis) if j == max(elem_dis) ]
print(elem_dis[index[0]])

Submission status: Accepted

Solution code:
num = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

s = [0 for i in range(4)]
for i in range(num):
	s[0] += abs(A[i] - B[i])
	s[1] += abs(A[i] - B[i])**2
	s[2] += abs(A[i] - B[i])**3
	s[3] = max(s[3], abs(A[i] - B[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:
import math

n = int(input())
x = input().split()
y = input().split()

d1 = d2 = d3 = dinf = 0.0
for i in range(n):
    di = abs(int(x[i]) - int(y[i]))
    d1 += di
    d2 += di ** 2
    d3 += di ** 3
    if di > dinf:
        dinf = di

d2 = math.sqrt(d2)
d3 = math.pow(d3, 1/3)
print(d1, d2, d3, dinf, sep='\n')

Submission status: Accepted

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

man = 0
euc = 0
euc3 = 0
che = 0
for i in range(n):
    man += abs(x[i]-y[i])
    euc += abs(x[i]-y[i])**2
    euc3 += abs(x[i]-y[i])**3
    che = max(che, abs(x[i]-y[i]))

print(man)
print(euc**0.5)
print(euc3**(1/3))
print(che)

Submission status: Accepted

Solution code:
a = int(input())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
D1,D2,D3,D00 = 0., 0., 0., 0.
for i in range(a):
    D1 += abs(x[i] - y[i])
    D2 += abs(x[i] - y[i])**2
    D3 += abs(x[i] - y[i])**3
    if D00 < abs(x[i] - y[i]):
        D00 = abs(x[i] - y[i])
print(D1)
print(D2**(1/2))
print(D3**(1/3))
print(D00)

Submission status: Accepted

Solution code:
from typing import List


def minkowski():
    num_elements = int(input())
    x_list: List[int] = list(map(int, input().split()))
    y_list: List[int] = list(map(int, input().split()))

    for p in range(1, 4):  # p = 1, 2, 3
        sum_error = 0
        for i in range(num_elements):
            sum_error += abs(x_list[i] - y_list[i]) ** p
            sum_dist = sum_error ** (1 / p)
        print(f"{sum_dist:.6f}")

    max_dist = 0  # p = infinity
    for i in range(num_elements):
        dist = abs(x_list[i] - y_list[i])
        if dist > max_dist:
            max_dist = dist
    print(f"{max_dist:.6f}")


if __name__ == "__main__":
    minkowski()

Submission status: Accepted

Solution code:
import math

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

man = 0
for i in range(n):
    man += math.sqrt((x[i]-y[i])**2)
print(f'{man:.6f}')

euc = 0
for i in range(n):
    euc += (x[i]-y[i])**2
Euc = math.sqrt(euc)
print(f'{Euc:.6f}')

n3 = 0
for i in range(n):
    n3 += (math.sqrt((x[i]-y[i])**2))**3
N3 = n3**(1/3)
print(f'{N3:.6f}')

che = []
for i in range(n):
    che.append(math.sqrt((x[i]-y[i])**2))
Che = max(che)
print(f'{Che:.6f}')

Submission status: Accepted

Solution code:
n = int(input())
x_list = list(map(int, input().split()))
y_list = list(map(int, input().split()))
Dxy1, Dxy2, Dxy3 = 0, 0, 0
Dxyinf_list = []

for x, y in zip(x_list, y_list):
    Dxy1 += abs(x-y)
    Dxy2 += (abs(x-y))**2
    Dxy3 += (abs(x-y))**3
    Dxyinf_list.append(abs(x-y))

Dxy2 = Dxy2**(1/2)
Dxy3 = Dxy3**(1/3)
Dxyinf = max(Dxyinf_list)

print('{0:.6f}'.format(Dxy1))
print('{0:.6f}'.format(Dxy2))
print('{0:.6f}'.format(Dxy3))
print('{0:.6f}'.format(Dxyinf))

Submission status: Accepted

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

def MDD(p,x,y):#Malti Dimension Distance
    ans=0
    for i,j in zip(x,y):
        ans+=abs(i-j)**p
    ans=ans**(1/p)
    print(ans)
MDD(1,x,y)
MDD(2,x,y)
MDD(3,x,y)
print(max(abs(i-j) for i,j in zip(x,y)))

Submission status: Accepted

Solution code:
import math

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

f_inf = float('inf')



for p in range(1,5):
    sum1 = 0
    for i in range(n):
        tmp = abs(xlist[i] - ylist[i])**p
        sum1 += tmp
    
    if p == 4:
        maax = 0
        for i in range(n):
            maax = max(abs(xlist[i] - ylist[i]), maax)
        print(maax)
    else:
        print(sum1**(1/p))

Submission status: Accepted

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

distance_m = 0
distance_u = 0
distance_p3 = 0
distance_mic = 0

for i in range(n):
    distance_m += abs(vector_x[i] - vector_y[i])
    distance_u += abs(vector_x[i] - vector_y[i]) ** 2
    distance_p3 += abs(vector_x[i] - vector_y[i]) ** 3
    if distance_mic < abs(vector_x[i] - vector_y[i]):
        distance_mic = abs(vector_x[i] - vector_y[i])

print(distance_m)
print(distance_u ** (1/2))
print(distance_p3 ** (1/3))
print(float(distance_mic))

Submission status: Accepted

Solution code:
n = int(input())

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

dim_list = [1,2,3,float('inf')]

for dim in dim_list:
    D = [0]*n
    if dim == float('inf'):
        for i in range(n): 
            D[i] = abs(x[i] - y[i])
        print("{:.6f}".format(max(D)))
    else:
        for i in range(n):
            D[i] = abs(x[i] - y[i])**dim
        
        print("{:.6f}".format(sum(D)**(1/dim)))

Submission status: Accepted

Solution code:
n=int(input())
x_arr=map(float,input().split())
y_arr=map(float,input().split())

r1,r2,r3,rm=0,0,0,0

for x,y in zip(x_arr, y_arr):
    tmp=abs(x-y)
    r1 += tmp
    r2 += tmp**2
    r3 += tmp**3
    rm = max(rm, tmp)

print(r1)
print(r2**(0.5))
print(r3**(1/3))
print(rm)

Submission status: Accepted

Solution code:
def Dxy(a,b,dim):

    if dim == 'inf':
        c=[abs(x-y) for (x,y) in zip(a,b)]
        result = max(c) 
        return result
    else:
        sum = 0.0 
        for i in range(n):
            tmp = abs(a[i]-b[i])
            sum += pow(tmp,dim)
        result = pow(sum,1.0/dim)
        return result

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

print(f'{Dxy(x,y,1):6f}')
print(f'{Dxy(x,y,2):6f}')
print(f'{Dxy(x,y,3):6f}')
print(f'{Dxy(x,y,"inf"):6f}')

Submission status: Accepted

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

p = [0] * 3
Max = 0
for i in range(3) :
    for j in range(n) :
        p[i] += abs(x[j] - y[j]) ** (i+1)
        if abs(x[j] - y[j]) > Max :
            Max = abs(x[j] - y[j])
            
    print('{:.5f}'.format(p[i] ** (1/(i+1))))
print('{:.5f}'.format(Max))

Submission status: Accepted

Solution code:
subabs = lambda x, y: [abs(i - j) for i, j in zip(x, y)]
minkowski = lambda arr, p: sum(a ** p for a in arr) ** (1 / p)

_ = input()
x = map(float, input().split())
y = map(float, input().split())
subs = subabs(x, y)

print("{:.8f}".format(minkowski(subs, 1)))
print("{:.8f}".format(minkowski(subs, 2)))
print("{:.8f}".format(minkowski(subs, 3)))
print("{:.8f}".format(max(subs)))

Submission status: Accepted

Solution code:
from math import sqrt

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

p_1 = sum([abs(x[i]-y[i]) for i in range(n)])
p_2 = sqrt(sum([abs(x[i]-y[i])**2 for i in range(n)]))
p_3 = pow(sum([abs(x[i]-y[i])**3 for i in range(n)]), 1.0/3.0)
p_inf = max([abs(x[i]-y[i]) for i in range(n)])
print(p_1)
print(p_2)
print(p_3)
print(p_inf)

Submission status: Accepted

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

d1, d2, d3, dx = 0, 0, 0, 0
for i in range(n):
    d1 += abs(x_array[i] - y_array[i])
    d2 += (x_array[i] - y_array[i])**2
    d3 += abs(x_array[i] - y_array[i])**3
    d = abs(x_array[i] - y_array[i])
    if d > dx:
        dx = d
print(d1)
print(d2**0.5)
print(d3**(1/3))
print(dx)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
d = [0] * 4

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

for i in range(4):
	print('%f' % d[i])

Submission status: Accepted

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

# pが1-3のとき
for t in range(3):
    temp = [abs(a-b)**(t+1) for (a,b) in zip(x,y)]
    print(sum(temp)**(1/(1+t)))

# pが∞のとき
temp = [abs(a-b) for (a,b) in zip(x,y)]
print(max(temp))

Submission status: Accepted

Solution code:
import math

num = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
xy = [abs(i - j) for i,j in zip(x, y)] 
for i in range(3):
  result = 0
  p = i + 1
  for j in xy:
    result += j ** p
  result = math.pow(result, 1 / p)
  print(result)
result = 0
print(max(xy))

Submission status: Accepted

Solution code:
import math
n = int(input())
s = list(map(int,input().split()))
t = list(map(int,input().split()))
L=[]
for i in range(n):
   L.append(abs(s[i]-t[i]))
d1 = sum(L)
d2 = math.sqrt(sum(map(lambda x: x**2,L)))
d3 =math.pow(sum(map(lambda x: x**3,L)),(1.0/3.0))
dinf=max(L)
print('{:.5f}\n{:.5f}\n{:.5f}\n{:.5f}'.format(d1,d2,d3,dinf))

Submission status: Accepted

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


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


func(1)
func(2)
func(3)
print(f'{max(abs(x_i - y_i) for x_i, y_i in zip(x, y)):.5f}')

Submission status: Accepted

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

d1=0
d2=0
d3=0
d9=0
for i in range(n):
    if x[i]>y[i]:
        d=x[i]-y[i]
    else:
        d=y[i]-x[i]
    d1+=d

for i in range(n):
    d=(x[i]-y[i])**2
    d2+=d
d2=d2**(1/2)

for i in range(n):
    if x[i]>y[i]:
        d=(x[i]-y[i])**3
    else:
        d=(y[i]-x[i])**3
    d3+=d
d3=d3**(1/3)
z=[]
for i in range(n):
    if x[i]>y[i]:
        d=x[i]-y[i]
    else:
        d=y[i]-x[i]
    z.append(d)
d9=max(z)

print(f'{d1:.6f}')
print(f'{d2:.6f}')
print(f'{d3:.6f}')
print(f'{d9:.6f}')

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):
    ans = 0
    for d in c:
        ans += d ** p
    print(ans ** (1 / p))

print(max(c))

Submission status: Accepted

Solution code:
import math
import itertools
import sys

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

  p1 = 0
  p2 = 0
  p3 = 0
  pf = 0

  for i in range(n):
    p1 += math.sqrt((X[i] - Y[i]) ** 2)
    p2 += (X[i] - Y[i]) ** 2
    p3 += (math.sqrt((X[i] - Y[i]) ** 2)) ** 3
    if pf < math.sqrt((X[i] - Y[i]) ** 2):
      pf = math.sqrt((X[i] - Y[i]) ** 2)

  p2 = math.sqrt(p2)
  p3 = p3 ** (1/3)

  print(p1,p2,p3,pf, sep='\n')

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
n = int(input())
xi = list(map(int, input().split()))
yi = list(map(int, input().split()))
for i in [1,2,3,0]:
	dd = 0
	for j in range(0,n):
		if i == 0 :
			if dd <= abs(yi[j]-xi[j]):
				dd = abs(yi[j]-xi[j])
		else:
			dd = dd + abs(yi[j]-xi[j])**i
	if i == 0:
		print(dd)
	else:
		print(dd**(1/i))

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:
from math import sqrt

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

# p << inf
def dxy(p):
    l = [(abs(x[i]-y[i]))**p for i in range(n)]
    sum_l = sum(l)
    return sum_l ** (1/p)

for p in range(1, 4):
    print(dxy(p))

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

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(xi-yi) for xi,yi in zip(x,y)))
print(math.sqrt(sum((xi-yi)**2 for xi,yi in zip(x,y))))
print((sum(abs(xi-yi)**3 for xi,yi in zip(x,y)))**(1/3))
print(max(abs(xi-yi) for xi,yi in zip(x,y)))

Submission status: Accepted

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

for i in range(N):
    abs_xy.append(abs(x[i] - y[i]))

abs_xy_2 = [abs_xy[i] ** 2 for i in range(N)]
abs_xy_3 = [abs_xy[i] ** 3 for i in range(N)]

print(sum(abs_xy))
print(sum(abs_xy_2) ** (1 / 2))
print(sum(abs_xy_3) ** (1 / 3))
print(max(abs_xy))