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(' ')))
p1 = 0.0
p2 = 0.0
p3 = 0.0
pm = 0.0
i = 0
while i < n:
    p1 += abs(x[i] - y[i])
    p2 += pow(abs(x[i] - y[i]), 2)
    p3 += pow(abs(x[i] - y[i]), 3)
    pm = max(pm, abs(x[i] - y[i]))
    i += 1
p2 = pow(p2, 0.5)
p3 = pow(p3, 0.333333333333333)
ans = '{0:.8f}'.format(p1) + '\n'
ans += '{0:.8f}'.format(p2) + '\n'
ans += '{0:.8f}'.format(p3) + '\n'
ans += '{0:.8f}'.format(pm)
print(ans)

Submission status: Accepted

Solution code:
from math import fabs, sqrt

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

sum1 = 0
sum2 = 0
sum3 = 0
pil = []

for i in range(n):
    sum1 += fabs(x[i] - y[i])
    sum2 += fabs(x[i] - y[i]) ** 2
    sum3 += fabs(x[i] - y[i]) ** 3
    pil.append(fabs(x[i] - y[i]))

pil = sorted(pil)
p1 = sum1
p2 = sqrt(sum2)
p3 = sum3 ** (1 / 3)
pi = pil[n-1]

print(round(p1, 8))
print(round(p2, 8))
print(round(p3, 8))
print(round(pi, 8))

Submission status: Accepted

Solution code:
def distance(x, y, p):
    """returns Minkowski's distance of vactor x and y if p > 0.
    if p == 0, returns Chebyshev distance

    >>> d = distance([1, 2, 3], [2, 0, 4], 1)
    >>> print('{:.6f}'.format(d))
    4.000000
    >>> d = distance([1, 2, 3], [2, 0, 4], 2)
    >>> print('{:.6f}'.format(d))
    2.449490
    >>> d = distance([1, 2, 3], [2, 0, 4], 3)
    >>> print('{:.6f}'.format(d))
    2.154435
    >>> d = distance([1, 2, 3], [2, 0, 4], 0)
    >>> print('{:.6f}'.format(d))
    2.000000
    """
    if p == 0:
        return max([abs(a-b) for (a, b) in zip(x, y)])
    else:
        return sum([abs(a-b) ** p for (a, b) in zip(x, y)]) ** (1/p)


def run():
    dim = int(input())  # flake8: noqa

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

    print(distance(x, y, 1))
    print(distance(x, y, 2))
    print(distance(x, y, 3))
    print(distance(x, y, 0))


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
#coding: utf-8
import math

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

d1 = d2 = d3 = di = 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 di < abs(x[i] - y[i]):
        di = abs(x[i] - y[i])

print(d1)
print(math.pow(d2, 1.0/2.0))
print(math.pow(d3, 1.0/3.0))
print(di)

Submission status: Accepted

Solution code:
input()
x = list(map(int, input().split()))
y = list(map(int, input().split()))
l = []
for a, b in zip(x, y):
  l += [abs(a - b)]
print("{0:.6f}".format(sum(l)))
print("{0:.6f}".format(sum([e ** 2 for e in l]) ** 0.5))
print("{0:.6f}".format(sum([e ** 3 for e in l]) ** (1/3)))
print("{0:.6f}".format(max(l)))

Submission status: Accepted

Solution code:
n = int(input())
x_i = [float(i) for i in input().split()]
y_i = [float(i) for i in input().split()]
abs_sub = []
D1,D2,D3=0.0,0.0,0.0
for i in range(len(x_i)):
#p==1
    D1 += ((x_i[i]-y_i[i])**2)**(1/2)
#p==2
    D2 +=(x_i[i]-y_i[i])**2
#p==3
    D3 +=(((x_i[i]-y_i[i])**2)**(1/2))**3
#p==inf
    abs_sub.append(((x_i[i]-y_i[i])**2)**(1/2))
D2 = D2**(1/2)
D3 = D3**(1/3)
D_inf=abs_sub[0]
for i in range(len(abs_sub)):
    if D_inf < abs_sub[i]:
        D_inf = abs_sub[i]
print("{:.6f}\n{:.6f}\n{:.6f}\n{:.6f}".format(D1,D2,D3,D_inf))

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 = [int(i) for i in input().split()]
Y = [int(j) for j in input().split()]
for p in [1,2,3]:
    D = (sum([abs(x-y)**p for (x,y) in zip(X,Y)]))**(1/p)
    print("{:.6f}".format(D))
D = max([abs(x-y) for (x,y) in zip(X,Y)])
print("{:.6f}".format(D))

Submission status: Accepted

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

Submission status: Accepted

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

def p1(x, y, n):
    D = []
    for i in range(n):
        D.append(abs(x[i] - y[i]))
    return sum(D)

def p2(x, y, n):
    D = []
    for i in range(n):
        D.append(abs(x[i] - y[i]))
        D[i] *= D[i]
    Sum = sum(D)
    return math.sqrt(Sum)

def p3(x, y, n):
    D = []
    for i in range(n):
        D.append(abs(x[i] - y[i]))
        D[i] = D[i] * D[i] * D[i]
    Sum = sum(D)
    return math.pow(Sum, 1 / 3)

def pX(x, y, n):
    D = []
    for i in range(n):
        D.append(abs(x[i] - y[i]))
    return max(D)

print('{:6f}'.format(p1(x, y, n)))
print('{:6f}'.format(p2(x, y, n)))
print('{:6f}'.format(p3(x, y, n)))
print('{:6f}'.format(pX(x, y, n)))

Submission status: Accepted

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

import math

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

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

d1 = sum(abs_xy)

d2 = 0
for i in range(n):
    d2 += abs_xy[i] * abs_xy[i]
d2 = math.sqrt(d2)

d3 = 0
for i in range(n):
    d3 += abs_xy[i] * abs_xy[i] * abs_xy[i]
d3 = math.pow(d3, 1/3)

d = max(abs_xy)

print(d1)
print(d2)
print(d3)
print(d)

Submission status: Accepted

Solution code:
#! python 3
# distance_ii.py

import math

def minkovski(a, b, p):
    if p == 'inf':
        tmp_arr = [abs(x-y) for x, y in zip(a, b)]
        max_val = tmp_arr[0]
        for i in range(1, len(a)):
            if max_val < tmp_arr[i]:
                max_val = tmp_arr[i]
        return max_val
    else:
        dst = 0
        for x, y in zip(a, b):
            dst += pow(abs(x-y), p)
        dst = pow(dst, 1/p)
        return dst

n = int(input())
a = [int(x) for x in input().split(' ')]
b = [int(x) for x in input().split(' ')]

for p in [1, 2, 3, 'inf']:
    print('%.6f'%minkovski(a, b, p))

Submission status: Accepted

Solution code:
n=int(input())
d=[abs(s-t)for s,t in zip(*[list(map(int,input().split()))for _ in'12'])]
f=lambda n:sum(s**n for s in d)**(1/n)
print(f(1),f(2),f(3),max(d),sep='\n')

Submission status: Accepted

Solution code:
input()
d=[abs(s-t)for s,t in zip(*[list(map(int,input().split()))for _ in'12'])]
f=lambda n:sum(s**n for s in d)**(1/n)
print(f(1),f(2),f(3),max(d),sep='\n')

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(float, input().split()))
b = list(map(float, 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("{:.6f}".format(ans ** (1 / p)))

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

Submission status: Accepted

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

def suan(p):
	d = 0
	for i in range(n):
		d += (math.fabs(x[i] - y[i]))**p
	print('%.6f'%(d**(1/p)))

suan(1)
suan(2)
suan(3)
print(max(math.fabs(x[i] - y[i])for i in range(n)))

Submission status: Accepted

Solution code:
import math

n=int(input())
x=input().split()
y=input().split()
A=[]
# case p=1
su=0
i=0
while i<n:
    su+=math.sqrt((int(x[i])-int(y[i]))*(int(x[i])-int(y[i])))
    i+=1
A.append(su)

# case p=2
su2=0
l=0
while l<n:
    su2+=(int(x[l])-int(y[l]))*(int(x[l])-int(y[l]))
    l+=1
A.append(math.sqrt(su2))

# case p=3
su3=0
m=0
while m<n:
    su3+=math.pow(math.sqrt(math.pow(int(x[m])-int(y[m]),2)),3)
    m+=1
A.append(math.pow(su3,1/3))

# case p=mugendai
mu=[]
q=0
while q<n:
    mu.append(math.sqrt(math.pow(int(x[q])-int(y[q]),2)))
    q+=1
M=max(mu)
A.append(M)

for Ans in A:
    print(Ans)

Submission status: Accepted

Solution code:
import math
n = input()
x = map(float, raw_input().split())
y = map(float, raw_input().split())
d1 = 0.0
d2 = 0.0
d3 = 0.0
d4 = 0.0
for i in range(n):
    d1 += abs(x[i] - y[i])
    d2 += pow(abs(x[i] - y[i]),2)
    d3 += pow(abs(x[i] - y[i]),3)
    d4 = max(d4,abs(x[i] - y[i]))
d2 = math.sqrt(d2)
d3 = d3 ** (1.0 / 3.0)
print d1
print d2
print d3
print d4

Submission status: Accepted

Solution code:
from  math import sqrt
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
p1=0;p2=0;p3=0;p=[]
for i in range(n):
    a=abs(x[i]-y[i])
    p1+=a
    p2+=a**2
    p3+=a**3
    p.append(a)
print(p1)
print(sqrt(p2))
print(p3**(1/3))
print(max(p))

Submission status: Accepted

Solution code:
import math
n = int(input())
x, y = [int(e) for e in input().split()], [int(e) for e in input().split()]
D1 = D2 = D3 = 0; D4 = []
for x1, y1 in zip(x, y):
    D1 += abs(x1-y1)
    D2 += abs(x1-y1)**2
    D3 += abs(x1-y1)**3
    D4.append(abs(x1-y1))
print('{0:.6f}'.format(D1))
print('{0:.6f}'.format(math.sqrt(D2)))
print('{0:.6f}'.format(math.pow(D3, (1/3))))
print('{0:.6f}'.format(max(D4)))

Submission status: Accepted

Solution code:
# AOJ ITP1_10_D

import math

def intinput():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a

def main():
    n = int(input())
    x = intinput()
    y = intinput()
    sum_1 = 0
    sum_2 = 0
    sum_3 = 0
    MD_INFTY = 0
    for i in range(n):
        MD_INFTY = max(MD_INFTY, abs(x[i] - y[i]))
        sum_1 += abs(x[i] - y[i])
        sum_2 += abs(x[i] - y[i]) ** 2
        sum_3 += abs(x[i] - y[i]) ** 3
    MD_1 = sum_1
    MD_2 = math.sqrt(sum_2)
    MD_3 = sum_3 ** (1 / 3)
    print(MD_1); print(MD_2); print(MD_3); print(float(MD_INFTY))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
n = int(input())
x, y = [int(e) for e in input().split()], [int(e) for e in input().split()]
total_1 = total_2 = total_3 = total_max = 0
for x1, y1 in zip(x, y):
    total_1 += abs(x1 - y1)
    total_2 += (x1 - y1)**2
    total_3 += (abs(x1 - y1))**3
    if total_max < abs(x1 - y1):
        total_max = abs(x1 - y1)
print("{0:.5f}".format(total_1))
print("{0:.5f}".format(math.sqrt(total_2)))
print("{0:.5f}".format(math.pow(total_3, 1.0/3.0)))
print("{0:.5f}".format(total_max))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
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 p in [1, 2, 3, -1]:
    if p == -1:
        print(max(D))
    else:
        print(math.pow(sum([math.pow(D[i],p) for i in range(n)]),1/p))

Submission status: Accepted

Solution code:
import math

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

ans1 = 0
ans2 = 0
ans3 = 0
ans4 = 0

for i in range(n):
	a = math.fabs(x[i] - y[i])
	ans1 += a
	ans2 += a**2
	ans3 += a**3
	ans4 = max(ans4, a) 

print(ans1)
print(ans2**(1./2.))
print(ans3**(1./3.))
print(ans4)

Submission status: Accepted

Solution code:
import math

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

ans1 = ans2 = ans3 = 0
ans4 = -1

for i in range(n):
	a = math.fabs(x[i] - y[i])
	ans1 += a
	ans2 += a**2
	ans3 += a**3
	ans4 = max(ans4, a) 

print(ans1)
print(ans2**(1./2.))
print(ans3**(1./3.))
print(ans4)

Submission status: Accepted

Solution code:
import math
def mink(x,y,p):
    return (sum([math.fabs(_x - _y)**p for _x, _y in zip(x,y)]))**(1/p)
def mink_i(x,y):
    return max([math.fabs(_x - _y) for _x, _y in zip(x,y)])
n = int(input())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
print(mink(x,y,1))
print(mink(x,y,2))
print(mink(x,y,3))
print(mink_i(x,y))

Submission status: Accepted

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

d1 = [abs(x - y) for x, y in zip(a, b)]
print(sum(d1))

d2 = [(x - y)**2 for x, y in zip(a, b)]
print(pow(sum(d2), 1/2))

d3 = [(abs(x - y))**3 for x, y in zip(a, b)]
print(pow(sum(d3), 1/3))

print(max(d1))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
Z = list(itertools.product(X,Y))
Z = Z[::len(X)+1]
def fn(p):
    D = 0
    for z in Z:
        D += abs(z[0]-z[1])**p
    else:
        print(D**(1/p))
fn(1)
fn(2)
fn(3)
MA = []
for z in Z:
    MA.append(abs(z[0]-z[1]))
print(max(MA))

Submission status: Accepted

Solution code:
# Distance II

dimension = int(input())
vectors = [[float(i) for i in input().rstrip().split()] for j in range(2)]
x = vectors[0]
y = vectors[1]

absoluteValues = [abs(x[k] - y[k]) for k in range(dimension)]
# print(absoluteValues)

# xとyのミンコフスキー距離を求める関数(a: 有限)
def mink(a):
    totalValue = 0
    for i in range(dimension):
        totalValue += absoluteValues[i] ** a
    distance = totalValue ** (1 / a)
    return distance

# xとyのChebyshev距離を求める関数
def chev():
    maxValue = 0
    for i in range(dimension):
        # print(absoluteValues[i])
        if absoluteValues[i] > maxValue:
            maxValue = absoluteValues[i]
    print(maxValue)


for i in range(1, 3 + 1):
    dist = mink(i)
    print(dist)
chev()

Submission status: Accepted

Solution code:
import math
n = int(input())
xlist = list(map(float, input().split()))
ylist = list(map(float, input().split()))
#x, y = [list(map(int, input().split())) for _ in range(2)] 로 한방에 가능
print(sum(abs(xlist[i] - ylist[i]) for i in range(n)))
print(pow(sum(abs(xlist[i] - ylist[i]) ** 2 for i in range(n)),0.5))
print(pow(sum(abs(xlist[i] - ylist[i]) ** 3 for i in range(n)),1/3))
print(max(abs(xlist[i] - ylist[i])for i in range(n)))

Submission status: Accepted

Solution code:
import math

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(sum([abs(a[i] - b[i]) for i in range(n)]))
print(pow(sum([abs(a[i] - b[i]) **2 for i in range(n)]), 1/2))
print(pow(sum([abs(a[i] - b[i]) **3 for i in range(n)]), 1/3))
print(max([abs(a[i] - b[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()))

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

for p in range(1, 4):
  ans = 0
  for d in a:
    ans += d ** p
  print('{:.10f}'.format(ans ** (1/p)))
print('{:.10f}'.format(max(a)))

Submission status: Accepted

Solution code:
def Dis(X,Y,p):
    D = 0
    for x, y in zip(X, Y):
        dis = abs(x - y) ** p
        D += dis 
    result = D ** (1/p)
    print(result)
def Dis_Che(X, Y):
    print(max(abs(x-y) for x, y in zip(X, Y)))

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

for p in range(1,4):
    Dis(X, Y, p)
Dis_Che(X,Y)

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:


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

d1 = 0
d2 = 0
d3 = 0
Di = []

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"{(d2)**(1/2):.6f}")

for i in range(n) :
    d3 += abs(X[i] - Y[i]) ** 3
print(f"{(d3)**(1/3):.6f}")
    
for i in range(n) :
    Di.append(abs(X[i] - Y[i]))
print(f"{max(Di):.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())
a = list(map(float, input().split()))
b = list(map(float, input().split()))

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

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

Submission status: Accepted

Solution code:
input()
d=[abs(s-t)for s,t in zip(*[list(map(int,input().split()))for _ in'12'])]
f=lambda n:sum(s**n for s in d)**(1/n)
print(f(1),f(2),f(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()))
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(map(int,input().split()))
y=list(map(int,input().split()))
D=[0,0,0,0]
p=[1,2,3]
for k in range(4):
    if k==3:
        D[k]=max(abs(x[i]-y[i])for i in range(n))
    else:
        D[k]=math.pow(sum(abs((x[i]-y[i])**p[k])for i in range(n)),1/p[k])
    print("{:.6f}".format(D[k]))

Submission status: Accepted

Solution code:
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:
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
p1,p2,p3 = 0,0,0
pin = []
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
	pin.append(abs(x[i] - y[i]))
p2 = p2**(1/2)
p3 = p3**(1/3)
pinf = max(pin)
print(p1)
print(p2)
print(p3)
print(pinf)

Submission status: Accepted

Solution code:
mark_list=[1,2,3,'∞']
n=int(input())
element_x=[int(i) for i in input().split()]
element_y=[int(l) for l in input().split()]
for p in mark_list:
    if p==1:
        m_distance=0
        for o in range(n):
            x=element_x[o]
            y=element_y[o]
            m_distance+=abs(x-y)
        print(m_distance)
    if p==2:
        y_distance=0
        y_distance2=0
        for m in range(n):
            x=element_x[m]
            y=element_y[m]
            y_distance2+=pow(abs(x-y),2)
        y_distance=pow(y_distance2,0.5)
        print(y_distance)
    if p==3:
        third_distance=0
        third_distance2=0
        for q in range(n):
            x=element_x[q]
            y=element_y[q]
            third_distance2+=pow(abs(x-y),3)
        third_distance=pow(third_distance2,1/3)
        print(third_distance)
    if p=='∞':
        c_distance=0
        difference=0
        for r in range(n):
            x=element_x[r]
            y=element_y[r]
            difference=abs(x-y)
            if difference>c_distance:
                c_distance=difference
        print(c_distance)

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]:
	a = 0
	for j in range(0,n):
		if i == 0 :
			if a <= abs(yi[j]-xi[j]):
				a = abs(yi[j]-xi[j])
		else:
			a = a + abs(yi[j]-xi[j])**i
	if i == 0:
		print(a)
	else:
		print(a**(1/i))

Submission status: Accepted

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

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

c=[abs(a[i]-b[i]) for i in range(n)]  #abs:絶対値
for p in range(1,4):
    ans=0
    for d in c:
        ans+=d**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()))
a = [0]*4
for i in range(n):
    a[0] += abs(x[i]-y[i])
    a[1] += abs(x[i]-y[i])**2
    a[2] += abs(x[i]-y[i])**3
    a[3] = max(a[3],abs(x[i]-y[i]))
a[1] = a[1]**(1/2)
a[2] = a[2]**(1/3)
for i in range(4):
    print('%f' % a[i])

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()]
d1=0
d2=0
d3=0
D=[]
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.sqrt(d2):.6f}')
for i in range(n):
    d3+=abs(X[i]-Y[i])**3
print(f'{d3**(1/3):.6f}')
for i in range(n):
    D.append(abs(X[i]-Y[i]))
print(f"{max(D):.6f}")

Submission status: Accepted

Solution code:
import math
n=int(input())
x=[int(0) for i in range(n)]
y=[int(0) for i in range(n)]
x[:]=(int(a) for a in input().split())
y[:]=(int(a) for a in input().split())
p1=0
p2=0
p3=0
px=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
  if px<abs(x[i]-y[i]):
    px=abs(x[i]-y[i])

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

print('{:.05f}'.format(p1))
print('{:.05f}'.format(p2))
print('{:.05f}'.format(p3))
print('{:.05f}'.format(px))

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()))
z,a,b=[],0,0
for i in range(n):
    z.append(abs(x[i]-y[i]))
for i in range(n):
    a+=(z[i]**2)
    b+=(z[i]**3)
a=math.sqrt(a)
b=math.pow(b,1.0/3.0)
print('{:.6f}\n{:.6f}\n{:.6f}\n{:.6f}'.format(sum(z),a,b,max(z)))

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:


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

for i in range(1, 4):
    nums = [(abs(a - b)) ** i for a, b in zip(A,B)]
    print(sum(nums) ** (1/i))
    if i == 1:
        ans = max(nums)
print(ans)

Submission status: Accepted

Solution code:
def dist(A_lst, B_lst, p):
    s = 0
    for a, b in zip(A_lst, B_lst):
        s += abs(a - b) ** p
    return s ** (1 / p)
    
    
N = int(input())
*A, = map(int, input().split())
*B, = map(int, input().split())
print("{:.6f}".format(dist(A, B, 1)))
print("{:.6f}".format(dist(A, B, 2)))
print("{:.6f}".format(dist(A, B, 3)))
chebyshev = max(abs(a - b) for a, b in zip(A, B))
print("{:.6f}".format(chebyshev))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
d=0
for i in range(n):
  d += max(x[i],y[i])-min(x[i],y[i])
print(float(d))

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

d=0
for i in range(n):
  d+=(max(x[i],y[i])-min(x[i],y[i]))**3
print(math.pow(d,1/3))

d=[]
for i in range(n):
  d.append(max(x[i],y[i])-min(x[i],y[i]))
print(float(max(d)))

Submission status: Accepted

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

Submission status: Accepted

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

p1 = 0
p2b = 0
p3b = 0
pn = 0
for i in range(n):
  p1 += abs(x[i] - y[i])
  p2b += abs((x[i] - y[i]) ** 2)
  p3b += abs(((x[i] - y[i]) ** 3))

p2 = p2b ** 0.5
p3 = p3b ** (1 / 3)
pn = max([abs(x[i] - y[i]) for i in range(n)])

print("{:.10f}".format(p1)) 
print(p2)
print(p3)
print("{:.10f}".format(pn))

Submission status: Accepted

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

n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
print('{0:.6f}'.format(dist(x,y,1)))
print('{0:.6f}'.format(dist(x,y,2)))
print('{0:.6f}'.format(dist(x,y,3)))
print('{}'.format(max([abs(x[i]-y[i])for i in range(n)])))

Submission status: Accepted

Solution code:
def minkovsky(A,B,n = 0):
    C = [abs(a - b) for a , b in zip(A,B)]
    if n == 0:
        return max(C)
    else:
        d = 0
        for c in C:
            d += c ** n
        d = d ** (1 / n)
        return d
            
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

print(minkovsky(A,B,1))
print(minkovsky(A,B,2))
print(minkovsky(A,B,3))
print(minkovsky(A,B))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
i = 0
p1 = 0
p2 = 0
p3 = 0
p4 = 0
for _ in range(n):
    if x[i] > y[i]:
        p1 = (x[i] - y[i]) + p1
    else:
        p1 = (y[i] - x[i]) + p1
    i = i + 1
print('{:.8f}'.format(p1))

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

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

i = 0
M = 0
for _ in range(n):
    if x[i] > y[i]:
        p4 = x[i] - y[i]
        if M < p4:
            M = p4
    else:
        p4 = y[i] - x[i]
        if M < p4:
            M = p4
    i = i + 1
print('{:.8f}'.format(M))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
manhattan = 0
euclid = 0
euclid2 = 0
chebishef = 0

for i, j in zip(x, y):
    tmp = abs(i - j)
    manhattan += tmp
    euclid += tmp * tmp
    euclid2 += tmp * tmp * tmp
    chebishef = max(chebishef, tmp)

print(manhattan)
print(math.sqrt(euclid))
print(math.pow(euclid2, 1 / 3))
print(chebishef)

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):
    print('{0:f}'.format(sum(j ** i for j in d) ** (1 / i)))
print('{0:f}'.format(max(d)))

Submission status: Accepted

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

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

num = 0
for i in range(n):
    num += (x[i] - y[i])**2
num **= 1/2
ans += num,

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

num = []
for i in range(n):
    num += abs(x[i] - y[i]),
num = max(num)
ans += num,

print(*ans, sep = "\n")

Submission status: Accepted

Solution code:
from math import pow,fabs

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

def distance(length,x,y,n):
    D = 0
    for i in range(length):
        D += fabs(x[i]-y[i]) ** n
    return pow(D,1.0/n)

def che_distance(length,x,y):
    D = 0
    for i in range(length):
        if D < fabs(x[i]-y[i]):
            D = fabs(x[i]-y[i])
    return D

p1 = distance(N,X,Y,1)
p2 = distance(N,X,Y,2)
p3 = distance(N,X,Y,3)
pinf = che_distance(N,X,Y)

print("{}\n{}\n{}\n{}".format(p1,p2,p3,pinf))

Submission status: Accepted

Solution code:
#10_D
import math
n=int(input())
x_v=list(map(int,input().split()))
y_v=list(map(int,input().split()))

p=1
while(p<5):
    d=0
    if(p==4):
        my_list=[]
        for i in range(n):
            my_list.append(abs(x_v[i]-y_v[i]))
        d=max(my_list)
        print(d)
    else:
        for i in range(n):
            d+=abs(x_v[i]-y_v[i])**p
        d=pow(d,1/p)
        print(d)
    p+=1

Submission status: Accepted

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

x = [int(j) for j in x]
y = [int(z) for z in y]
d1 = 0
d2 = 0
d4 = []
d3 = 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.append(abs(x[i]- y[i]))
d2_root = d2**0.5
d3_root = d3**(1/3)
print('{:.08f}'.format(d1))
print('{:.08f}'.format(d2_root))
print('{:.08f}'.format(d3_root))
print('{:.08f}'.format(max(d4)))

Submission status: Accepted

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

y=list(map(int,input().split()))
z=[0]*n
a=0
b=0
c=0
d=0
for i in range(n):
    a=a+abs(x[i]-y[i])
    b=b+(x[i]-y[i])**2
    c=c+abs(x[i]-y[i])**3
    z[i]=abs(x[i]-y[i])
print(f'{a:.10f}')
b=b**(1/2)
print(f'{b:.10f}')
c=c**(1/3)
print(f'{c:.10f}')
print(f'{max(z):.10f}')

Submission status: Accepted

Solution code:
#
# 10d
#
import math


def main():
    n = int(input())
    x = list(map(int, input().split()))
    y = list(map(int, input().split()))
    d1 = 0
    d2 = 0
    d3 = 0
    dn = 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
        dn = max(dn, abs(x[i] - y[i]))

    d2 = math.sqrt(d2)
    d3 = math.pow(d3, 1/3)
    print(f"{d1:.5f}")
    print(f"{d2:.5f}")
    print(f"{d3:.5f}")
    print(f"{dn:.5f}")


if __name__ == '__main__':
    main()