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

diff = []
total1 = 0
total2 = 0
total3 = 0

for i in range(n):
    diff.append(abs(X[i] - Y[i]))
    total1 += diff[i]
    total2 += diff[i] ** 2
    total3 += diff[i] ** 3

print("{0:.6f}" . format(total1))
print("{0:.6f}" . format(total2 ** 0.5))
print("{0:.6f}" . format(total3 ** (1 / 3)))
print("{0:.6f}" . format(max(diff)))

Submission status: Accepted

Solution code:
n = int(input())
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))
ds = [abs(x - y) for x, y in zip(xs, ys)]
for i in range(1, 4):
    ans = 0
    for d in ds:
        ans += d**i
    print(ans**(1/i))
print(max(ds))

Submission status: Accepted

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

def minko_f(x,y):
    l = []
    for i in range(len(x)):
        l.append(abs(x[i]-y[i]))
    return max(l)

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

print(minko_n(x,y,1))
print(minko_n(x,y,2))
print(minko_n(x,y,3))
print(minko_f(x,y))

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:
n = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
temp = list(map(lambda x, y: abs(x-y), X, Y))
result = []
for i in [1,2,3]:
    print(sum(list(map(lambda x: x**i, temp)))**(1/i))
print(max(temp))

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([abs(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:
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
d= [abs(x[i] - y[i]) for i in range(n)]
for i in range(1, 4):
    a = 0
    for j in d:
        a += j**i
    print("{:.6f}".format(a**(1/i)))
print("{:.6f}".format(max(d)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n = int(input())
x_list = list(map(int,input().split()))
y_list = list(map(int,input().split()))
def distance(X,Y,N):
    t= 0
    for i  in range(len(X)):
        z = (abs(X[i] - Y[i]))**N
        t += z
    return math.pow(t,1/N)    
def chebi(X,Y):
    abs_list= []
    for i in range(len(X)):
        abs_list.append(abs(X[i]-Y[i]))
    return max(abs_list)
print(distance(x_list,y_list,1))
print(distance(x_list,y_list,2))
print(distance(x_list,y_list,3))
print(chebi(x_list,y_list))

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 = list(map(int, input().split()))
Y = list(map(int, input().split()))
C = [abs(X[i] - Y[i]) for i in range(n)]
D = []
def distance(X, Y):  
  for k in range(1, 4):
    d = 0
    for j in range(len(X)):
      d += C[j] ** k
    d = d ** (1 / k)
    D.append(d)
  D.append(max(C))
  return D

distance(X, Y)
for i in range(4):
  print('{:.08f}'.format(D[i]))

Submission status: Accepted

Solution code:
import math as ma

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

d = [int(0)]*4 #m,e,p3,t
tmp = int(0)
tmp2 = int(0)
tmp3 = [int(0)]*n

for i in range(n):
    d[0] += abs(x[i]-y[i])
    tmp += (x[i]-y[i])**2
    tmp2 += (abs(x[i]-y[i]))**3
    tmp3[i] = abs(x[i]-y[i])

d[1] = ma.sqrt(tmp)
d[2] = (tmp2)**(1/3)
d[3] = max(tmp3)

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

Submission status: Accepted

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

p1 = p2 = p3 = p4 = 0

for i in range(n):
    p1 += abs(x[i] - y[i])
    p2 += abs(x[i] - y[i]) ** 2
    p3 += abs(x[i] - y[i]) ** 3
    p4 = max(p4, abs(x[i] - y[i]))

p2 = p2 ** (1 / 2)
p3 = p3 ** (1 / 3)

print("%f\n%f\n%f\n%f\n" % (p1, p2, p3, p4))

Submission status: Accepted

Solution code:
import math
n=int(input())
a=[]
for i  in range(2):
    s = list(map(int, input().split()))
    a.append(s)
d1=0
for i in range(n):
    d1+=abs(a[0][i-1]-a[1][i-1])
print(format(d1, '.6f'))
d2_1=0
for i in range(n):
    d2_1+=(abs(a[0][i-1]-a[1][i-1]))**2
d2=math.sqrt(d2_1)
print(format(d2, '.6f'))
d3_1=0
for i in range(n):
    d3_1+=(abs(a[0][i-1]-a[1][i-1]))**3
d3=(d3_1)**(1/3)
print(format(d3, '.6f'))
dn=0
for i in range(n):
    dn=max(abs(a[0][i-1]-a[1][i-1]),dn)
print(format(dn, '.6f'))

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:
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:
n = int(input())
tbl = [[0 for i in range(n)] for j in range(5)]
for i in range(2):
    a = list(map(int,input().split()))
    for j in range(n):
        tbl[i][j] = a[j]
D_1 = 0
d_2 = 0
d_3 = 0
for k in range(n):
    tbl[2][k] = abs(tbl[0][k]-tbl[1][k])
    tbl[3][k] = (abs(tbl[0][k]-tbl[1][k]))**2
    tbl[4][k] = (abs(tbl[0][k]-tbl[1][k]))**3
    D_1 += tbl[2][k]
    d_2 += tbl[3][k]
    d_3 += tbl[4][k]
M = max(tbl[2])
print(f'{D_1:.6f}')
print(f'{(d_2)**(1/2):.6f}')
print(f'{(d_3)**(1/3):.6f}')
print(f'{M:.6f}')

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split())) 
y = list(map(int, input().split()))
c = [abs(x[i]-y[i]) for i in range(n)]
for p in range(1, 4):
  ans = 0
  for s in c:
    ans += s**p
  print("{:.6f}".format(ans**(1/p)))
print("{:.6f}".format(max(c)))

Submission status: Accepted

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

for p in range(1,5):
    if p != 4:
        D = 0.0
        for i in range(n):
            D += ((abs(X[i]-Y[i]))**p)
        D = D**(1/p)
        print("{:.6f}".format(D))
    else:
        print("{:.6f}".format(max(abs(x-y)for x,y in zip(X,Y))))

Submission status: Accepted

Solution code:
#72 ミンコフスキー距離

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:
import math
n=int(input())
x=list(map(int,input().split(" ")))
y=list(map(int,input().split(" ")))

def minkowski(x,y,p):
    tmp=[math.fabs(x[i]-y[i])**p for i in range(len(x))]
    return sum(tmp)**(1/p)

ans=[minkowski(x,y,i) for i in range(1,4)]
ans.append(max([math.fabs(x[i]-y[i])for i in range(len(x))]))
for i in ans:
    print(i)

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()))
p = [0]*4

for i in range(n):
    p[0] += abs(x[i] - y[i])
    p[1] += abs(x[i] - y[i])**2
    p[2] += abs(x[i] - y[i])**3
    p[3] = max(p[3], abs(x[i] - y[i]))
p[1] = p[1]**(1/2)
p[2] = p[2]**(1/3)
for i in range(4):
    print("{0:.5f}".format(p[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, p_2, p_3, p_infinit = 0, 0, 0, 0
dis_list = []
for i in range(n):
    dis_list.append(abs(x[i] - y[i]))
    sum_d = abs(x[i] - y[i])
    p_1 += sum_d
    p_2 += sum_d ** 2
    p_3 += sum_d ** 3
p_2 = math.sqrt(p_2)
p_3 = p_3 ** (1 / 3)

print(p_1)
print(p_2)
print(p_3)
print(max(dis_list))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = [int(i) for i in input().split()]
y = [int(j) for j in input().split()]
Manhattan = sum([abs(x[m]-y[m]) for m in range(n)])
Euclidean = math.sqrt(sum([(x[e]-y[e])**2 for e in range(n)]))
p_3 = (sum([(abs(x[c]-y[c]))**3 for c in range(n)]))**(1/3)
Chebyshev = max([abs(x[m]-y[m]) for m in range(n)])
print(Manhattan)
print(Euclidean)
print(p_3)
print(Chebyshev)

Submission status: Accepted

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

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

def cheb_dis(n, x, y):
    dis = 0
    for i in range(n):
        dif = abs(x[i] - y[i])
        if dis < dif:
            dis = dif
    return dis

for p in range(1 , 4):
    print(minc_dis(n, x, y, p))

print(cheb_dis(n, x, y))


#print(cheb_dis(2, [1, 2, 3], [2, 0, 4]))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
xy = []
for i in range(n):
    xy.append(abs(x[i] - y[i]))
D = [0, 0, 0, 0]
D[0] = sum(xy)
D2 = 0
for xyi in xy:
    D2 += xyi ** 2
D[1] = math.sqrt(D2)
D3 = 0
for xyi in xy:
    D3 += xyi ** 3
D[2] = math.pow(D3, 1.0/3.0)
D[3] = max(xy)
for d in D:
    print(d)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
list=[]
for j in range(3):
   D=0
   for i in range(n):
      if x[i]<y[i]:
         d = (y[i]-x[i])
      else:
         d = (x[i]-y[i])
      list.append(d)
      D += (d**(j+1))
   print(f"{D**(1/(j+1)):.6f}") 
print(f"{max(list):.6f}")

Submission status: Accepted

Solution code:
n=int(input())
x=[int(X) for X in input().split()]
y=[int(Y) for Y in input().split()]
p1,p2,p3,p=0,0,0,[]
for i in range(n):
    p1+=abs(x[i]-y[i])
print(f'{p1:.5f}')
for i in range(n):
    p2+=(abs(x[i]-y[i]))**2
print(f'{p2**(1/2):.5f}')
for i in range(n):
    p3+=(abs(x[i]-y[i]))**3
print(f'{p3**(1/3):.5f}')
for i in range(n):
    p.append(abs(x[i]-y[i]))
print(f'{max(p):.5f}')

Submission status: Accepted

Solution code:
import math

S = list(map(int, open(0).read().split()))
N = S[0]
X = S[1:1+N]
Y = S[N+1:]

def calc(p):
    s = 0
    xy = [abs(X[i] - Y[i]) for i in range(N)]
    if p == 1:
        return sum(xy)
    elif p == 2:
        return sum([x ** 2 for x in xy]) ** 0.5
    elif p == 3:
        return sum([x ** 3 for x in xy]) ** (1/3)
    elif p == -1:
        return max(xy)

def show(x):
    print("{:.6f}".format(x))

show(calc(1))
show(calc(2))
show(calc(3))
show(calc(-1))

Submission status: Accepted

Solution code:
import math

n=int(input())
L=list(input().split())
M=list(input().split())

A=0
B=0
D=0
N=[]

for i in range(n):
    a=float(L[i])
    b=float(M[i])
    
    if a>b:
        A+=a-b
        D+=(a-b)**3
        N.append(a-b)
    else:
        A+=b-a
        D+=(b-a)**3
        N.append(b-a)
        
    B+=(a-b)**2
    C=math.sqrt(B)
    
    E=D**(1/3)

print(A)
print(C)
print(E)
print(max(N))

Submission status: Accepted

Solution code:
import math
n=int(input())
x=[float(a) for a in input().split()]
y=[float(b) for b in input().split()]
p=[float(1),float(2),float(3),math.inf]
for j in range(3):
    lst=[]
    for i in range(n):
        if x[i]>=y[i]:
            s=(x[i]-y[i])**(p[j])
        else:
            s=(y[i]-x[i])**(p[j])
        lst.append(s)
    t=sum(lst)
    print(f'{t**(1/p[j]):10.8f}')
lst1=[]
for k in range(n):
    if x[k]>=y[k]:
        u=x[k]-y[k]
    else:
        u=y[k]-x[k]
    lst1.append(u)
print(f'{max(lst1):10.8f}')

Submission status: Accepted

Solution code:
import math
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
p=[1,2,3,math.inf]
p1=0
p2=0
p3=0
p4=0
 
#p=1
for i in range(n):
    p1+=(abs(a[i]-b[i]))
print("{:10f}".format(p1))
 
#p=2
q=0
for i in range(n):
    q+=((abs(a[i]-b[i]))**2)
    p2=math.sqrt(q)
print("{:10f}".format(p2))
 
#3
q=0
for i in range(n):
    q+=((abs(a[i]-b[i]))**3)
    p3=q**(1/3)
print("{:10f}".format(p3))
 
#inf
q=0
for i in range(n):
    q=(abs(a[i]-b[i]))
    if q>p4:
        p4=q 
print("{:10f}".format(p4))

Submission status: Accepted

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

A=0
b=0
c=0
z=[]
for i in range(n):
    A+=abs(x[i]-y[i])
    b+=(x[i]-y[i])*(x[i]-y[i])
    c+=(abs(x[i]-y[i]))**3
    z.append(abs(x[i]-y[i]))
D=max(z)

    
B=b**(1/2)
C=c**(1/3)

print(A)
print(B)
print(C)
print(D)

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
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
    m = abs(x[i]-y[i])
    dm.append(m)
    
D2 = math.sqrt(d2)
D3 = d3 ** (1/3)
Dm = max(dm) 

print(f'{D1:.06f}')
print(f'{D2:.06f}')
print(f'{D3:.06f}')
print(f'{Dm:.06f}')

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):
    D = abs(x[i] - y[i])
    p1 += D
    p2 += D**2
    p3 += D**3
    if pinf < D:
        pinf = D
p2 = math.sqrt(p2)
p3 = 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:
#from functools import reduce


def resolve():
    _ = int(input())
    xv = list(map(float, input().split()))
    yv = list(map(float, input().split()))

    # for p in range(1, 4):
    #     print(reduce(lambda x, y: x + abs(y[0] - y[1]) ** p, zip(xv, yv), 0) ** (1 / p))

    # p = 1, 2, 3
    for p in range(1, 4):
        k = sum([abs(x - y) ** p for x, y in zip(xv, yv)]) ** (1 / p)
        print("{0:.6f}".format(k))
    # p = ∞
    print("{0:.6f}".format(max([abs(x - y) for x, y in zip(xv, yv)])))


resolve()

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
D1=[]
for i in range(n):
    d1+=abs(x[i]-y[i])
print(f'{d1:.6f}')
for i in range(n) :
    d2+=abs(x[i]-y[i])**2
print(f'{math.pow(d2,1/2):.6f}')
for i in range(n):
    d3+=abs(x[i]-y[i])**3
print(f'{math.pow(d3,1/3):.6f}')
for i in range(n):
    D1.append(abs(x[i]-y[i]))
print(f'{max(D1):.6f}')

Submission status: Accepted

Solution code:
p = [1, 2, 3, float("inf")]
sum_p = [0, 0, 0, 0] 
n = int(input())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
for i in range(n):
    
    diff = abs(x[i] - y[i])
    if sum_p[3] < diff:
        sum_p[3] = diff  
    sum_p[0] += diff ** p[0]
    sum_p[1] += diff ** p[1]
    sum_p[2] += diff ** p[2]

for i in range(len(sum_p)):
    if i < 3:
        print("{:.8f}".format(sum_p[i] ** (1/p[i])))
    else:
        print("{:.8f}".format(sum_p[i]))

Submission status: Accepted

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

l=[]
for i in range(n):
  if x[i]-y[i]>0:
    l.append(x[i]-y[i])
  else:
    l.append(y[i]-x[i])

D1=0
D2=0
D3=0
Dinf=max(l)

for i in range(n):
  D1+=l[i]
  D2+=l[i]**2
  D3+=l[i]**3
  
D2=D2**(1/2)
D3=D3**(1/3)

print(D1)
print(D2)
print(D3)
print(Dinf)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
work=[abs(x[i]-y[i]) for i in range(n)]

p1=sum(work)
p2=(sum([x**2 for x in work]))**0.5
p3=(sum([x**3 for x in work]))**(1/3)
p_inf=max(work)

print(p1)
print(p2)
print(p3)
print(p_inf)

Submission status: Accepted

Solution code:
import math

n = int(input())
x_lst = map(int, input().split())
y_lst = map(int, input().split())

xy_lst = list(zip(x_lst, y_lst))

p1 = 0
p2 = 0
p3 = 0
p4 = []
for x, y in xy_lst:
    # マンハッタン距離
    p1 += abs(x - y)
    # ユークリッド距離
    p2 += (abs(x - y)) ** 2
    p3 += (abs(x - y)) ** 3
    # チェビシェフ距離
    p4.append(abs(x - y))
print(p1)
print(math.sqrt(p2))
print(math.pow(p3, 1 / 3))
print(max(p4))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))
a = 0
b = 0
c = 0
d = [0 for i in range(n)]
for i in range(n):
    a += abs(x[i]-y[i])
    b += abs(x[i]-y[i])**2
    c += abs(x[i]-y[i])**3
    d[i] = abs(x[i]-y[i])
b = math.sqrt(b)
c = c**(1/3)
print('{:.6f}'.format(a))
print('{:.6f}'.format(b))
print('{:.6f}'.format(c))
print('{:.6f}'.format(max(d)))

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):
    D = 0
    for j in range(n):
        D += abs(X[j] - Y[j])**(i)
    print(D**(1/i))
print(max(abs(X[i] - Y[i]) for i in range(n)))

Submission status: Accepted

Solution code:
import math
n = int(input())
a = [float(x) for x in list(input().split())]
b = [float(x) for x in list(input().split())]

result = 0
for aa, bb in zip(a, b):
    result += abs(aa - bb)
print(round(result, 6))

result = 0
for aa, bb in zip(a, b):
    result += abs(aa - bb) ** 2
print(round(math.sqrt(result), 8))

result = 0
for aa, bb in zip(a, b):
    result += abs(aa - bb) ** 3
print(round(math.pow(result, 1/3), 8))

result = []
for aa, bb in zip(a, b):
    result.append(abs(aa - bb))
print(max(result))

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    n = int(input())
    x=[]
    y=[]
    x=list(map(int,input().split()))
    y=list(map(int,input().split()))
    ans=0
    ans_list=[]
    for i in range(1,4):
        for x_value,y_value in zip(x,y):
            ans+=abs(x_value-y_value)**i
            ans_list.append(abs(x_value-y_value))
        print("{:.6f}".format(ans**(1/i)))
        ans=0
    print("{:.6f}".format(max(ans_list)))
    
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import math

n = int(input())
x_buf = input().split()
y_buf = input().split()

x = []
y = []
for i in range(n):
    x.append(int(x_buf[i]))
    y.append(int(y_buf[i]))

sum1 = 0
sum2 = 0
sum3 = 0
sum_inf = 0
max_inf = 0
for i in range(n):
    sum1 += abs(x[i] - y[i])
    sum2 += abs(x[i] - y[i]) ** 2
    sum3 += abs(x[i] - y[i]) ** 3
    sum_inf = abs(x[i] - y[i])
    if max_inf < sum_inf:
        max_inf = sum_inf

d1 = sum1
d2 = math.sqrt(sum2)
d3 = sum3 ** (1/3)
d4 = max_inf

print(d1)
print(d2)
print(d3)
print(d4)

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
ab = [math.fabs(x[i]-y[i]) for i in range(n)]
p_1, p_2, p_3 = 0, 0, 0
for i in range(n):
    p_1 += ab[i]
for i in range(n):
    p_2 += ab[i]**2
p_2 = math.sqrt(p_2)
for i in range(n):
    p_3 += ab[i]**3
p_3 = p_3**(1/3)
p_inf = max(ab)
print(p_1)
print(p_2)
print(p_3)
print(p_inf)

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int,input().split()))
Y = list(map(int,input().split()))
P = []
P_1 = 0
P_2 = 0
P_3 = 0
P_m = 0

for i in range(n):
    if X[i] > Y[i]:
        P.append(X[i]-Y[i])
    else:
        P.append(Y[i]-X[i])

for i in range(n):
    P_1 += P[i]
    P_2 += P[i]**2
    P_3 += P[i]**3
P_2 = P_2**(1/2)
P_3 = P_3**(1/3)
P_m = max(abs(x-y) for x,y in zip(X,Y))

print('{:.6f}'.format(P_1))
print('{:.6f}'.format(P_2))
print('{:.6f}'.format(P_3))
print('{:.6f}'.format(P_m))

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, j in zip(x, y):
    d1.append(abs(i - j))
    d2.append(abs(i - j)**2)
    d3.append(abs(i - j)**3)

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

Submission status: Accepted

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

x_y = []
for x, y in zip(X,Y):
    x_y.append(abs(x-y))

d1 = sum(x_y)
d2 = 0
for xy in x_y:
    d2 += xy ** 2
d2 = d2 ** (1/2)
d3 = 0
for xy in x_y:
    d3 += xy ** 3
d3 = d3 ** (1/3)
d4 = max(x_y)

print(d1)
print(d2)
print(d3)
print(d4)

Submission status: Accepted

Solution code:
import math

N = int(input())
v1 = [int(i) for i in input().strip().split()]
v2 = [int(i) for i in input().strip().split()]

# Manhattan distance
m1 = 0
for i in range(N):
    m1 += abs(v1[i] - v2[i])

print(float(m1))

# Euclidean distance
m2 = 0
for i in range(N):
    m2 += (v1[i] - v2[i]) ** 2

m2 = math.sqrt(m2)

print(float(m2))

# ??
m3 = 0
for i in range(N):
    m3 += abs(v1[i] - v2[i]) ** 3

m3 = m3 ** (1 / 3)

print(float(m3))

# Chebyshev distance
m4 = 0
for i in range(N):
    m4 = max(abs(v1[i]-v2[i]), m4)

print(float(m4))

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):
    cnt=0
    for j in range(n):
        cnt+=abs(A[j]-B[j])**i
    print(cnt**(1/i))
cnt=0
for j in range(n):
    cnt=max(cnt,abs(A[j]-B[j]))
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

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

print(sum)

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

print(math.sqrt(sum))

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

print(sum ** (1/3))

check = 0
for i in range(n):
    if check < abs(x[i] - y[i]):
        check = abs(x[i] - y[i])

print(check)

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

Solution code:
num = int(input())
xs  = [int(n) for n in input().split()]
ys  = [int(n) for n in input().split()]

def calc_distance(xs, ys, p):
    sums = sum([abs(x-y) ** p for x,y in zip(xs, ys)])
    return sums ** (1.0 / p)

for index in range(1,4):
    print(calc_distance(xs, ys, index))

print(max(abs(x-y) for x,y in zip(xs,ys)))

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=1
d = [0.0 for i in range(3)]
var = 0
for i in range(n):
    temp = abs(x[i]-y[i])
    if var < temp:
        var = temp
    for j in range(3):
        d[j]+=temp**(j+1)

for i in range(3):
    print(d[i]**(1/(i+1)))
print(float(var))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
if __name__ == '__main__':
    def manh(x, y):
        d = 0
        for i, j in zip(x, y):
            d += abs(int(i)-int(j))
        return d

    def uqurid(x, y):
        d = 0
        for i, j in zip(x, y):
            d += abs(int(i)-int(j))**2
        return math.sqrt(d)

    def minco(x, y):
        p = 3
        d = 0
        for i, j in zip(x, y):
            d += abs(int(i)-int(j))**p
        return d**(1/p)

    def ebis(x, y):
        d = 0
        for i, j in zip(x, y):
            d = max(d, abs(int(i)-int(j)))
        return d

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

    print(manh(x, y))
    print(uqurid(x, y))
    print(minco(x, y))
    print(ebis(x, y))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
e = []
def kome(p):
    c = 0
    for i in range(n):
        c = c + (abs(a[i] - b[i]) ** p)
        e.append(abs(a[i] - b[i]))
    d = c ** (1 / p)
    return d
    
print(kome(1))
print(kome(2))
print(kome(3))
print(float(max(e)))

Submission status: Accepted

Solution code:
n=int(input())

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

p1=p2=p3=p4=0
for i in range(n):
    if x[i]>y[i]:
        k=x[i]-y[i]
        p1+=k
        p2+=k**2
        p3+=k**3
        p4=max(p4,k)
        
    else:
        k=y[i]-x[i]
        p1+=k
        p2+=k**2
        p3+=k**3
        p4=max(p4,k)

p2=p2**(1/2)
p3=p3**(1/3)

print(p1,p2,p3,p4, sep="\n")

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    x = list(map(int, input().split()))
    y = list(map(int, input().split()))
    for answer in calc(n, x, y):
        print("{:.6f}".format(answer))


def calc(n, x, y):
    ret = []
    patterns = [1, 2, 3]
    for pattern in patterns:
        sum = 0
        for i in range(0, n):
            sum += (abs(x[i] - y[i])) ** pattern
        ret.append(sum ** (1 / pattern))

    last = 0
    for i in range(0, n):
        last = max(last, abs(x[i] - y[i]))
    ret.append(last)

    return ret


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
N = int(input())
X = list(map(float, input().split()))
Y = list(map(float, input().split()))
S = [abs(x-y) for x,y in zip(X, Y)]
SS = [s**2 for s in S]
SSS = [s**3 for s in S]
print(sum(S))
print(pow(sum(SS), 1/2))
print(pow(sum(SSS), 1/3))
print(max(S))

Submission status: Accepted

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

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

c=[abs(x[i]-y[i]) for i in range(n)]
print(f'{max(c):.6f}')

Submission status: Accepted

Solution code:
import math
n = int(input())
v1 = map(float, input().split())
v2 = map(float, input().split())

v = [abs(x - y) for (x,y) in zip(v1, v2)]

d1 = sum(v)
d2 = math.sqrt(sum([x**2 for x in v]))
d3 = math.pow(sum([x**3 for x in v]), 1.0/3.0)
d4 = max(v)

print(d1)
print(d2)
print(d3)
print(d4)

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

p1 = 0
p2 = 0
p3 = 0
p4 = []
for i in range(n):
    p1 += math.pow(abs(x[i]-y[i]),1)

for i in range(n):
    p2 += math.pow(abs(x[i]-y[i]),2)

for i in range(n):
    p3 += math.pow(abs(x[i]-y[i]),3)
for i in range(n):
    p4.append(abs(x[i]-y[i]))

print(p1)
print(math.sqrt(p2))
print(math.pow((p3),1/3))
print(max(p4))

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
dmax = 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 dmax < abs(x[i] - y[i]):
    dmax = abs(x[i] - y[i])
print(d1)
print(math.sqrt(d2))
print(d3**(1.0/3.0))
print(dmax)

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_1 = 0
p_2 = 0
p_3 = 0
p_inf = 0
for i, j in zip(x, y):
    p_1 += abs(i - j)
    p_2 += (i - j) ** 2
    p_3 += abs((i - j)) ** 3
    p_inf = max(p_inf, abs(i - j))

print(p_1)
print(sqrt(p_2))
print(p_3 ** (1 / 3))
print(p_inf)

Submission status: Accepted

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

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

d1 = d2 = d3 = d4 = 0
l = []
for a, b in zip(a, b):
    d1 += math.pow(abs(a - b), 1)
    d2 += math.pow(abs(a - b), 2)
    d3 += math.pow(abs(a - b), 3)
    l.append(abs(a-b))

d1 = math.pow(d1, 1)
d2 = math.pow(d2, 1/2)
d3 = math.pow(d3, 1/3)
d4 = max(l)
print(d1)
print(d2)
print(d3)
print(d4)

Submission status: Accepted

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


def calc_dist_mink(list_x, list_y, p):
    list_xy = [abs(list_x[i] - list_y[i]) for i in range(len(list_x))]
    list_xy_p = [i ** p for i in list_xy]
    ans = sum(list_xy_p) ** (1 / p)
    return(ans)

list_ans = []
ans_1 = calc_dist_mink(X, Y, 1)
list_ans.append(ans_1)

ans_2 = calc_dist_mink(X, Y, 2)
list_ans.append(ans_2)

ans_3 = calc_dist_mink(X, Y, 3)
list_ans.append(ans_3)

ans_4 = max([abs(X[i] - Y[i]) for i in range(N)])
list_ans.append(ans_4)

for ans in list_ans:
    print(f"{ans:.5f}")