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:
import math
d=int(input())
X=list(map(int,input().split()))
Y=list(map(int,input().split()))
p_1=[abs(x-y) for x,y in zip(X,Y)]
print(round(sum(p_1),8))
p_2=[abs(x-y)**2 for x,y in zip(X,Y)]
print(round(math.sqrt(sum(p_2)),8))
p_3=[abs(x-y)**3 for x,y in zip(X,Y)]
print(round(math.pow(sum(p_3),1.0/3.0),8))
p_4=max(p_1)
print(round(p_4,8))

Submission status: Accepted

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

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

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

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

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

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

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

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

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

Dinf = 0.0
for i in range(n):
    Dinf = max(Dinf, abs(x[i] - y[i]))

print("{:10f}".format(D1))
print("{:10f}".format(D2))
print("{:10f}".format(D3))
print("{:10f}".format(Dinf))

Submission status: Accepted

Solution code:
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 = []
for i in range(n):
    p_1 += abs(x[i]-y[i])
    p_2 += abs(x[i]-y[i])**2
    p_3 += abs(x[i]-y[i])**3
    p_inf.append(abs(x[i]-y[i]))
print(format(float(p_1),".5f"))
print(format(float(p_2**(1/2)),".5f"))
print(format(float(p_3**(1/3)),".5f"))
print(format(float(max(p_inf)),".5f"))

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):
    D = 0
    for i in range(n):
        D += abs(x[i] - y[i]) ** p
    D = D ** (1/p)
    print(D)
D = 0
for i in range(n):
    tmp = abs(x[i] - y[i])
    if D < tmp:
        D = tmp
print(D)

Submission status: Accepted

Solution code:
import math


def Manhattan(x, y):
    n = len(x)
    D = 0
    for i in range(n):
        D += abs(x[i] - y[i])
    return D


def Euclidean(x, y):
    n = len(x)
    D = 0
    for i in range(n):
        D += (x[i] - y[i]) ** 2
    return math.sqrt(D)


def Tri(x, y):
    n = len(x)
    D = 0
    for i in range(n):
        D += abs(x[i] - y[i]) ** 3
    return math.pow(D, 1.0 / 3.0)


def Chebyshev(x, y):
    n = len(x)
    D = 0
    for i in range(n):
        D = max(D, abs(x[i] - y[i]))
    return D


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

print(Manhattan(x, y))
print(Euclidean(x, y))
print(Tri(x, y))
print(Chebyshev(x, y))

Submission status: Accepted

Solution code:
import math

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

for p in range(1, 4):
    sum = 0
    for i in range(n):
        sum += abs(x[i] - y[i]) ** p
    print('{:.10f}'.format(math.pow(sum, 1/p)))
ans = 0
for i in range(n):
    ans = max(ans, abs(x[i] - y[i]))
print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
xs = list(map(float, input().split()))
ys = list(map(float, input().split()))


def mink(p):
    return math.pow(sum([
        math.pow(math.fabs(xs[i] - ys[i]), p)
        for i in range(n)
    ]), 1 / p)


def mink_inf():
    return max([math.fabs(xs[i] - ys[i]) for i in range(n)])


def float_print(v):
    print('{:.5f}'.format(v))


float_print(mink(1))
float_print(mink(2))
float_print(mink(3))
float_print(mink_inf())

Submission status: Accepted

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

Submission status: Accepted

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

ans[1] = ans[1]**(1/2)
ans[2] = ans[2]**(1/3)

for i in ans:
	print(i)

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):
  print("{0:.6f}".format(sum([abs(a-b)**i for a,b in zip(x,y)])**(1/i)))
print("{0:.6f}".format(max([abs(a-b) for a,b in zip(x,y)])))

Submission status: Accepted

Solution code:
import sys
import itertools
import math

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())

A = [int(x) for x in readline().split()]
B = [int(x) for x in readline().split()]

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

for i, j in zip(A, B):
    ans1 += abs(i - j)
    ans2 += (i - j) ** 2
    ans3 += (abs(i - j)) ** 3
    ans4 = max(ans4, abs(i - j))
print(ans1 * 1.0, ans2 ** .5, ans3 ** (1 / 3), ans4 * 1.0, sep='\n')

Submission status: Accepted

Solution code:
def calc_distance(left, right, p):
    return sum(list(map(lambda x, y: abs(x - y) ** p, left, right))) ** (1 / p)


n = int(input())
v1 = list(map(lambda x: float(x), input().split()))
v2 = list(map(lambda x: float(x), input().split()))
for i in range(1, 4):
    print("{:f}".format(calc_distance(v1, v2, i)))
print("{:f}".format(max(list(map(lambda x, y: abs(x - y), v1, v2)))))

Submission status: Accepted

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

d1 = 0.0
d2 = 0.0
d3 = 0.0
dinf = 0.0

for i in range(n):
    d1 += abs(x[i] - y[i])
    d2 += (x[i] - y[i]) ** 2.0
    d3 += abs(x[i] - y[i]) ** 3.0
    dinf = max(dinf, abs(x[i] - y[i]))

d2 = d2 ** (1 / 2)
d3 = d3 ** (1 / 3)

print("{:.10f}".format(d1))
print("{:.10f}".format(d2))
print("{:.10f}".format(d3))
print("{:.10f}".format(dinf))

Submission status: Accepted

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

manhattan = 0
euclidean = 0
p3 = 0
chebyshev = []
for i in range(n) :
    manhattan += abs(x[i] - y[i])
    euclidean += abs(x[i] - y[i]) ** 2
    p3 += abs(x[i] - y[i]) ** 3
    chebyshev.append(abs(x[i] - y[i]))

euclidean = math.sqrt(euclidean)
p3 = p3 ** (1/3)
chebyshev = max(chebyshev)

print(manhattan)
print(euclidean)
print(p3)
print(chebyshev)

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
ans = ans**0.5
print(ans)

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

ans = 0
for i in range(n):
    if abs(x[i] - y[i]) > ans: ans = abs(x[i] - y[i])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
infinity = float("inf")

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

p = [1, 2, 3, infinity]

for i in p:
    if i != infinity:
        total = 0
        for X, Y in zip(x, y):
            total += (abs(X - Y)) ** i
        D = total ** (1/i)
        print(D)
    else:
        d = list()
        for X, Y in zip(x, y):
            d.append(abs(X - Y))
        print(max(d))

Submission status: Accepted

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

sum1 = 0
sum2 = 0
sum3 = 0
dif = []
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
    dif += [abs(x[i] - y[i])]
print("%f" %sum1)
print("%f" %(sum2)**(1/2))
print("%f" %(sum3)**(1/3))
print("%f" %max(dif))

Submission status: Accepted

Solution code:
import math

ans = []
n = int(input())
xn = list(map(float,input().split()))
yn = list(map(float,input().split()))

def man(xx):
    sum = 0
    for i in range(n):
        sum += (abs(xn[i] - yn[i]))**xx
    return sum**(1/xx)

def che():
    sum = 0
    for i in range(n):
        tmp = (abs(xn[i] - yn[i]))
        if tmp > sum:
            sum = tmp
    return sum


for i in range(1,4):
    print(man(i))
print(che())

Submission status: Accepted

Solution code:
n=input()
x=map(float,raw_input().split())
y=map(float,raw_input().split())
d1=0
for i in range(n):
    d1+=abs(x[i]-y[i])
d2=0
for i in range(n):
    d2+=abs(x[i]-y[i])**2
d2=d2**(0.5)
d3=0
for i in range(n):
    d3+=abs(x[i]-y[i])**3
d3=d3**(0.3333333333333)
dinf=0
for i in range(n):
    if dinf<abs(x[i]-y[i]):
        dinf=abs(x[i]-y[i])

print d1
print d2
print d3
print dinf

Submission status: Accepted

Solution code:
n = int(input())

x = [int(x) for x in input().split()]
y = [int(y) for y in 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))

d_max = 0

for j in range(n):
    d_max = max(abs(x[j] - y[j]), d_max)

print(d_max)

Submission status: Accepted

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

p1, p2, p3, pm = 0, 0, 0, 0
for _x, _y in zip(x, y):
    p1 += abs(_x-_y)
    p2 += abs(_x-_y)**2
    p3 += abs(_x-_y)**3
    pm = max(pm, abs(_x-_y))

print("{:.6f}".format(p1))
print("{:.6f}".format(p2**0.5))
print("{:.6f}".format(p3**(1/3)))
print("{:.6f}".format(pm))

Submission status: Accepted

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

d1, d2, d3, dinf = 0, 0, 0, 0
for i in range(n):
    d1 += abs(xl[i] - yl[i])
    d2 += (xl[i] - yl[i])**2
    d3 += (abs(xl[i] - yl[i]))**3
    dinf_ = abs(xl[i] - yl[i])
    if dinf_ > dinf:
        dinf = dinf_

print('{}\n{}\n{}\n{}'.format(d1, d2**(1/2), d3**(1/3), dinf))

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
d4=0

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

for i in range(n):
    d2+=(x[i]-y[i])**2
d2=math.sqrt(d2)
for i in range(n):
    d3+=(abs(x[i]-y[i])**3)

d3=d3**(1/3)

for i in range(n):
    tmp=abs(x[i]-y[i])
    d4=max(d4,tmp)

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

Submission status: Accepted

Solution code:
import math

def dis(n, x1, x2, d):
    dist = 0
    for i in range(n):
        dist += (abs(x1[i] - x2[i]))**d
    
    dist = pow(dist, 1/d)
    
    return dist

def chev(n, x1, x2):
    d = []
    for i in range(n):
        d.append(abs(x2[i]-x1[i]))
    
    return max(d)

n = int(input())
x1 = list(map(int, input().split()))
x2 = list(map(int, input().split()))

for i in range(1,4):
    print('%.5f'%dis(n,x1, x2, i))

print('%.5f'%chev(n, x1, x2))

Submission status: Accepted

Solution code:
n=int(input())
lis=list(map(int,input().split()))
lis2=list(map(int,input().split()))
abslis=[0 for i in range(n)]

for i in range(n):
    abslis[i]=abs(lis[i]-lis2[i])

print("{:.6f}".format(sum(abslis)))
summ=0
for i in range(n):
    summ+=abslis[i]**2
summ**=0.5
print("{:.6f}".format(summ))
summ=0
for i in range(n):
    summ+=abslis[i]**3
summ**=(1/3)
print("{:.6f}".format(summ))
print("{:.6f}".format(max(abslis)))

Submission status: Accepted

Solution code:
import math

def disp(p,x,y,n):
    if p==0:
        max=0
        for i in range(n):
            if max<abs(x[i]-y[i]):
                max=abs(x[i]-y[i])
        return max
    else:
        sum=0
        for i in range(n):
            sum+=(abs(x[i]-y[i]))**p
        return (sum**(1/p))
    
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
for p in range(1,4):
    print(disp(p,x,y,n))
print(disp(0,x,y,n))

Submission status: Accepted

Solution code:
import math

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

answer = []

# p = 1, 2, 3
for p in range(1, 3 + 1):
    result = 0.0
    for i in range(n):
        result += abs(x[i] - y[i]) ** p

    answer.append(result ** (1 / p))

# p = ∞
result = 0.0
for i in range(n):
    result = max(result, abs(x[i] - y[i]))

answer.append(result)

[print("{:.6f}".format(answer[i])) for i in range(len(answer))]

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 x in range(3)]
for pi in range(3):
 S = 0
 for ni in range(n):
  S = S + abs(x[ni]-y[ni])**(pi+1)
 D[pi] = S**(1/(pi+1))
 print(D[pi])
Dinf = -math.inf
for ni in range(n):
 Dinf = Dinf if Dinf>abs(x[ni]-y[ni]) else abs(x[ni]-y[ni])
print(Dinf)

Submission status: Accepted

Solution code:
import math

n = int(input())
xlist = input().split()
xlist = [int(s) for s in xlist]
ylist = input().split()
ylist = [int(s) for s in ylist]
zlist = [abs(xlist[x] - ylist[x]) for x in range(n)]

ans1 = 0
ans2 = 0
ans3 = 0
for x in range(n):
    ans1 += abs(xlist[x] - ylist[x])
    ans2 += abs(xlist[x] - ylist[x])**2
    ans3 += abs(xlist[x] - ylist[x])**3
print(ans1)    
print(math.sqrt(ans2))
print(ans3**(1/3))
print(max(zlist))

Submission status: Accepted

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

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

print(sum(b))
print(sum([i ** 2 for i in b]) ** (1 / 2))
print(sum([i ** 3 for i in b]) ** (1 / 3))
print(max(b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#計算方法の定義
def minkowski(p, X, Y):
    s = 0
    for x, y in zip(X, Y):
        s += abs(x - y)**p
    return s**(1/p)
def chebyshev(X, Y):
    return max(abs(x - y) for x, y in zip(X, Y))

#入力dataの格納
N = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))

#計算結果の表示
print(f"{minkowski(1, X, Y):.8f}")
print(f"{minkowski(2, X, Y):.8f}")
print(f"{minkowski(3, X, Y):.8f}")
print(f"{chebyshev(X, Y):.8f}")

Submission status: Accepted

Solution code:
import math

n = int(input())
x = [int(e) for e in input().split()]
y = [int(e) for e in input().split()]
D1=0
D2=0
D3=0
Di=abs(x[0]-y[0])
for i in range(n):
    D1 = D1 + (abs(x[i]-y[i]))
    D2 = D2 + (abs(x[i]-y[i]))**2
    D3 = D3 + (abs(x[i]-y[i]))**3
    if abs(x[i]-y[i]) > Di:
        Di = abs(x[i]-y[i])
print(D1)
print(math.sqrt(D2))
print(D3**(1/3))
print(Di)

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):
    tmp = abs(X[i] - Y[i])
    p1 += tmp
    p2 += tmp ** 2
    p3 += tmp ** 3
    pinf = max(pinf, abs(X[i] - Y[i]))
p2 = p2 ** (1/2)
p3 = p3 ** (1/3)
print(p1)
print(p2)
print(p3)
print(pinf)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

D = [0,0,0]

# P = 1-3
for p in range(1,4):
    for i in range(n):
        D[p-1] += abs(x[i] - y[i]) ** p

    D[p-1] = D[p-1] ** (1 / p)

# P = inf
Dinf = abs(x[0] - y[0])
for i in range(n):
    Dinf = max(Dinf,abs(x[i] - y[i]))

# output
print(D[0])
print(D[1])
print(D[2])
print(Dinf)

Submission status: Accepted

Solution code:
num = int(input())
ab=[]
ab2 = []
ab3 = []

x = list(map(int,input().split()))
y = list(map(int,input().split()))
#x = np.array(x)
#y = np.array(y)
for i in range(num):
    ab.append(abs(x[i]-y[i]))
    ab2.append(abs(x[i]-y[i])**2)
    ab3.append(abs(x[i]-y[i])**3)

print("{:.8f}".format(sum(ab)))
print("{:.8f}".format((sum(ab2))**(1/2)))
print("{:.8f}".format((sum(ab3))**(1/3)))
print("{:.8f}".format(max(ab)))

Submission status: Accepted

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

for p in range(1, 4):
    
    result = 0
    for i in range(n):
        result += pow(abs(x[i] - y[i]), p)
    result = pow(result, 1/p)
    print(result)

result = 0
for i in range(n):
    result = max(result, abs(x[i] - y[i]))
print(result)

Submission status: Accepted

Solution code:
import math
N = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))

a, b, c= 0, 0, 0
d = []

for i in range(N):
    diff = abs(X[i] - Y[i])
    a += diff
    b += (diff)**2
    c += (diff)**3
    d.append(diff)

print('{0:8f}'.format(a))
print('{0:8f}'.format(math.sqrt(b)))
print('{0:8f}'.format(c**(1/3)))
print('{0:8f}'.format(max(d)))

Submission status: Accepted

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

s = 0
t = 0
u = 0
v = []

for i in range(n):
	x = abs(a[i]-b[i])
	s += x
	t += x**2
	u += x**3
	v.append(x)

print("{:.10f}".format(s))
print("{:.10f}".format(math.sqrt(t)))
print("{:.10f}".format(u**(1/3)))
print("{:.10f}".format(max(v)))

Submission status: Accepted

Solution code:
import math

# 整数nを受け取る
n = int(input())

# ベクトルxを受け取る
x = [float(i) for i in input().split()]

# ベクトルyを受け取る
y = [float(j) for j in input().split()]

# マンハッタン距離を求める
D1 = sum([abs(k - l) for (k, l) in zip(x, y)])
print(D1)

# ユークリッド距離(p = 2)を求める
D2 = math.sqrt(sum([abs(k - l)**2 for (k, l) in zip(x, y)]))
print(D2)

# ユークリッド距離(p = 3)を求める
D3 = (sum([abs(k - l)**3 for (k, l) in zip(x, y)]))**(1/3)
print(D3)

# チェビシェフ距離を求める
Dc = max([abs(k - l) for (k, l) in zip(x, y)])
print(Dc)

Submission status: Accepted

Solution code:
import math

if __name__ =="__main__":

    param = input()

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

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

    #p = 1
    d1 = math.pow(sum([math.fabs(xi - yi) for xi, yi in zip(x, y)]), 1/1.0)

    #p = 2
    d2 = math.pow(sum([math.fabs(xi - yi)**2 for xi, yi in zip(x, y)]), 1/2.0)

    #p = 3
    d3 = math.pow(sum([math.fabs(xi - yi)**3 for xi, yi in zip(x, y)]), 1/3.0)

    #p = ∞
    dm = max([math.fabs(xi - yi) for xi, yi in zip(x, y)])

    print(d1)
    print(d2)
    print(d3)
    print(dm)

Submission status: Accepted

Solution code:
def lp_norm(x, y, p):
    if p == "INF":
        return max([abs(x_i - y_i) for (x_i, y_i) in zip(x, y)])
    else:
        return (sum([abs(x_i - y_i) ** p for (x_i, y_i) in zip(x, y)])) ** (1/p)


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

for i in [1, 2, 3, "INF"]:
    print(lp_norm(x, y, i))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
dim = int(input())

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

abs_dist_lst = [abs(z[0] - z[1]) for z in zip(x, y)]

print(sum(abs_dist_lst))

for p in [2, 3]:
    dist = 0
    for abs_d in abs_dist_lst:
        dist += abs_d ** p
    dist = dist ** (1 / p)
    print(dist)

print(max(abs_dist_lst))

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):
    p1+=abs(x[i]-y[i])
    p2+=abs(x[i]-y[i])**2
    p3+=abs(x[i]-y[i])**3
    pinf=max(pinf,abs(x[i]-y[i]))
p2=math.sqrt(p2)
p3=p3**(1/3)

print(p1)
print(p2)
print(p3)
print(pinf)

Submission status: Accepted

Solution code:
n=int(input())

p1=list(map(int, input().split()))
p2=list(map(int, input().split()))

mhd=0
ecd=0
trd=0
chd=0
for i  in range(n):
    mhd=mhd+abs(p1[i]-p2[i])
    ecd= ecd+abs(p1[i]-p2[i])**2
    trd=trd+abs(p1[i]-p2[i])**3
    chd=max(chd, abs(p1[i]-p2[i]))

print(mhd)
print(ecd**0.5)
print(trd**(1/3))
print(chd)

Submission status: Accepted

Solution code:
from math import *

n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
vv = list(zip(x, y))
da = lambda v, p: pow(sum(map(lambda q: pow(abs(q[0] - q[1]), p), v)), 1 / p)
di = lambda v: max(map(lambda q: abs(q[0] - q[1]), v))
for i in range(3):
    print(da(vv,i+1))
print(di(vv))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
d = [1, 2, 3, 4]
D = [0.0]*5
for i in d:
    if i == 1:
        D[i] = sum((abs(x[j] - y[j]) for j in range(n)))
    elif i == 2:
        D[i] = math.sqrt(sum((x[j] - y[j])**2 for j in range(n)))
    elif i == 3:
        D[i] = (sum(abs(x[j] - y[j])**3 for j in range(n)))**(1.0/3)
    else:
        D[i] = max(abs(x[j] - y[j]) for j in range(n))
    print('%.6f' % D[i])

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
x_y = [abs(x[i]-y[i]) for i in range (n)]
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_infinity = max(x_y)
print(f"{p1:.6f}")
print(f"{p2:.6f}")
print(f"{p3:.6f}")
print(f"{p_infinity:.6f}")

Submission status: Accepted

Solution code:
from math import *
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
doo=max([abs(x[i]-y[i]) for i in range(n)])
d1=d2=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
print(d1)
print(d2**0.5)
print(d3**(1/3))
print(doo)

Submission status: Accepted

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

D1 = D2 = D3 = Dc = 0
for xi, yi in zip(x, y):
    D1 += abs(xi-yi)
    D2 += abs(xi-yi)**2
    D3 += abs(xi-yi)**3
    Dc = max(Dc, abs(xi-yi))

D2 = D2**(1/2)
D3 = D3**(1/3)

print("{:.8f}".format(D1))
print("{:.8f}".format(D2))
print("{:.8f}".format(D3))
print("{:.8f}".format(Dc))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = input()
x = list(map(float, input().split()))
y = list(map(float, input().split()))
d1 = [abs(p-q) for p, q in zip(x, y)]
d2 = [abs(p-q)**2 for p, q in zip(x, y)]
d3 = [abs(p-q)**3 for p, q in zip(x, y)]
print(sum(d1))
print(sum(d2)**(1/2))
print(sum(d3)**(1/3))
print(max(d1))

Submission status: Accepted

Solution code:

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

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

#p=1
print(sum(dis))

#p=2
sum = 0
for elem in dis:
    sum = sum + elem**2
print(sum**(1/2))

#p=3
sum = 0
for elem in dis:
    sum = sum + elem**3
print(sum**(1/3))

#p=n
print(max(dis))

Submission status: Accepted

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

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

p1 = sum(p)
p2 = sum([ d**2 for d in p ]) ** (1/2)
p3 = sum([ d**3 for d in p ]) ** (1/3)
pinf = max(p)

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

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
d = [0 for j in range(n)]

for i in range(n):
 a += abs(x[i]-y[i])
 b += (x[i]-y[i])**2
 c += abs(x[i]-y[i])**3
 d[i] = abs(x[i]-y[i])

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

print(a)
print(b)
print(c)
print(max(d))

Submission status: Accepted

Solution code:
from math import *
from statistics import *
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    input()
    xx = list(map(int, input().split()))
    yy = list(map(int, input().split()))
    d1=d2=d3=d4=0
    for x,y in zip(xx,yy):
        d=abs(x-y)
        d1+=d
        d2+=d**2
        d3+=d**3
        d4=max(d4,d)
    print(d1)
    print(d2**0.5)
    print(d3**(1/3))
    print(d4)

main()

Submission status: Accepted

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

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

print(res)

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

print(res ** (1/2))

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

print(res ** (1/3))

res = 0
for i in range(n):
    res = max(res, abs(x[i] - y[i]))

print(res)

Submission status: Accepted

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

import math

p1 = []
p2 = []
p3 = []

for i in range(n):
    p1.append(abs(X[i] - Y[i]))
    p2.append(p1[i]**2)
    p3.append(p1[i]**3)
    
print(sum(p1))
print(sum(p2)**(1/2))
print(sum(p3)**(1/3))
print(max(p1))

Submission status: Accepted

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

for i in range(n):
    t = abs(x[i]-y[i])
    a += t
    b += t**2
    c += t**3
    d.append(t)

print(a)
print(b**(1/2))
print(c**(1/3))
print(max(d))

Submission status: Accepted

Solution code:
N = int(input())
X = [int(i) for i in input().split()]
Y = [int(i) for i in input().split()]
X_Y = [abs(x-y) for x, y in zip(X, Y)]

for p in [1, 2, 3, 'inf']:
    if p == 'inf':
        d = max(X_Y)
    else:
        d = sum([x_y**p for x_y in X_Y]) ** (1/p)
    
    print(d)

Submission status: Accepted

Solution code:
import math

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

p1 = p2 = p3 = pinf = 0

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

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

print(p1)
print(p2)
print(p3)
print(pinf)

Submission status: Accepted

Solution code:
from math import sqrt
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
d1,d2,d3,di=0,0,0,abs(x[0]-y[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
    if di < abs(x[i]-y[i]):
        di=abs(x[i]-y[i])
d2=sqrt(d2)
d3=d3**(1/3)
print(d1,d2,d3,di,sep='\n')

Submission status: Accepted

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

m1 = m2 = m3 = mi = 0
for i in range(n):
    m1 += abs(x[i]-y[i])
    m2 += (x[i]-y[i])**2
    m3 += (abs(x[i]-y[i]))**3
    mi = max([abs(x[i]-y[i]), mi])
print(m1)
print(m2**0.5)
print(m3**(1/3))
print(mi)

Submission status: Accepted

Solution code:
import math

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

Dxy = Dxy2 = Dxy3 = Dxyp = 0
for i in range( n ):
	t = abs(x[i]-y[i])
	Dxy += t
	Dxy2 += t**2
	Dxy3 += t**3
	Dxyp = max( Dxyp, t )
print( Dxy )
print( Dxy2**(1/2) )
print( Dxy3**(1/3) )
print( Dxyp )

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
n=int(input())
x=[float(k) for k in input().split()]
y=[float(k) for k in input().split()]
for i in range(1,4):
    print((sum([math.fabs(x[k]-y[k])**i for k in range(n)]))**(1/i))
m=-1000000
for k in range(n):
    v=math.fabs(x[k]-y[k])
    if v>m:
        m=v
print(m)

Submission status: Accepted

Solution code:
dev = int(input())
list_x = [float(i) for i in input().split()]
list_y = [float(i) for i in input().split()]

listabs = []
for vector in zip(list_x, list_y):
    x, y = vector
    listabs.append(abs(x -y))

for p in range(1, 4):
    D = 0
    for vector in listabs:
        D += vector**p
    D = D**(1/p)
    print(D)

print(max(listabs))

Submission status: Accepted

Solution code:
from math import *
n = int(input())
x = []
y = []
x[:] = map(int,input().split())
y[:] = map(int,input().split())

dxy1 = 0.0
dxy2 = 0.0
dxy3 = 0.0
dxyinf = 0.0

for i in range(0,n):
    dxy1 += abs(x[i]-y[i])
    dxy2 += (x[i]-y[i])**2
    dxy3 += (abs(x[i]-y[i]))**3
    if (dxyinf < abs(x[i]-y[i])):
        dxyinf = abs(x[i]-y[i])
dxy2 = sqrt(dxy2)
dxy3 = pow(dxy3, 1.0/3.0)
print(dxy1)
print(dxy2)
print(dxy3)
print(dxyinf)

Submission status: Accepted

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

distance = {
    1: lambda: sum([abs(x[i] - y[i]) for i in range(n)]),
    2: lambda: pow(sum([pow(abs(x[j] - y[j]), 2) for j in range(n)]), 1 / 2),
    3: lambda: pow(sum([pow(abs(x[j] - y[j]), 3) for j in range(n)]), 1 / 3),
    4: lambda: max([abs(x[i] - y[i]) for i in range(n)])
}

print(distance[1]())
print(distance[2]())
print(distance[3]())
print(distance[4]())