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

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

for i in range(1, 4):
    print('{:.5f}'.format(
        pow(sum([abs(X[j] - Y[j])**i for j in range(N)]), 1/i)
    ))

print('{:.5f}'.format(
    max([abs(X[j] - Y[j]) for j 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()))
tmp = [[0 for i in range(6)] for j in range(n+1)]
absDiff = abs(x[0]-y[0])
tmp[0][3] = tmp[0][0] = absDiff
tmp[0][4] = tmp[0][1] = absDiff ** 2
tmp[0][5] = tmp[0][2] = absDiff ** 3
max = absDiff
for i in range(1, n, 1):
    absDiff = abs(x[i]-y[i])
    tmp[i][0] = absDiff
    tmp[i][1] = absDiff ** 2
    tmp[i][2] = absDiff ** 3
    tmp[i][3] = tmp[i-1][3] + tmp[i][0]
    tmp[i][4] = tmp[i-1][4] + tmp[i][1]
    tmp[i][5] = tmp[i-1][5] + tmp[i][2]
    if absDiff > max:
        max = absDiff
print(tmp[n-1][3])
print(math.sqrt(tmp[n-1][4]))
print(tmp[n-1][5] ** (1/3))
print(max)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
tmp = [[0 for i in range(6)] for j in range(n+1)]
absDiff = abs(x[0]-y[0])
tmp[0][3] = tmp[0][0] = absDiff
tmp[0][4] = tmp[0][1] = absDiff ** 2
tmp[0][5] = tmp[0][2] = absDiff ** 3
max = absDiff
for i in range(1, n, 1):
    absDiff = abs(x[i]-y[i])
    tmp[i][0] = absDiff
    tmp[i][1] = absDiff ** 2
    tmp[i][2] = absDiff ** 3
    tmp[i][3] = tmp[i-1][3] + tmp[i][0]
    tmp[i][4] = tmp[i-1][4] + tmp[i][1]
    tmp[i][5] = tmp[i-1][5] + tmp[i][2]
    if absDiff > max:
        max = absDiff
print(tmp[n-1][3])
print(tmp[n-1][4] ** (1/2))
print(tmp[n-1][5] ** (1/3))
print(max)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
tmp = [[0 for i in range(6)] for j in range(n+1)]
absDiff = abs(x[0]-y[0])
tmp[0][3] = tmp[0][0] = absDiff
tmp[0][4] = tmp[0][1] = absDiff ** 2
tmp[0][5] = tmp[0][2] = absDiff ** 3
max = absDiff
for i in range(1, n, 1):
    absDiff = abs(x[i]-y[i])
    tmp[i][0] = absDiff
    tmp[i][1] = absDiff ** 2
    tmp[i][2] = tmp[i][1] * absDiff
    tmp[i][3] = tmp[i-1][3] + tmp[i][0]
    tmp[i][4] = tmp[i-1][4] + tmp[i][1]
    tmp[i][5] = tmp[i-1][5] + tmp[i][2]
    if absDiff > max:
        max = absDiff
print(tmp[n-1][3])
print(tmp[n-1][4] ** (1/2))
print(tmp[n-1][5] ** (1/3))
print(max)

Submission status: Accepted

Solution code:
n = int(input())
x = [0] + list(map(int, input().split()))
y = [0] + list(map(int, input().split()))
tmp = [[0 for i in range(6)] for j in range(n+1)]
max = 0
for i in range(1, n+1, 1):
    absDiff = abs(x[i]-y[i])
    tmp[i][0] = absDiff
    tmp[i][1] = absDiff ** 2
    tmp[i][2] = tmp[i][1] * absDiff
    tmp[i][3] = tmp[i-1][3] + tmp[i][0]
    tmp[i][4] = tmp[i-1][4] + tmp[i][1]
    tmp[i][5] = tmp[i-1][5] + tmp[i][2]
    if absDiff > max:
        max = absDiff
print(tmp[n][3])
print(tmp[n][4] ** (1/2))
print(tmp[n][5] ** (1/3))
print(max)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
p = 0
q = 0
p1 = 0
p2 = 0
p3 = 0
max = 0
import math
for i, j in zip(x, y):
    p1 += abs(i - j)
    p += abs((i - j) ** 2)
    p2 = math.sqrt(p)
    q += abs((i - j) ** 3)
    p3 = math.pow(q, 1.0/3.0)
    if abs(i - j) > max:
        max = abs(i - j)

print(p1)
print(p2)
print(p3)
print(max)

Submission status: Accepted

Solution code:
import math
n=int(input())
x=list(map(int, input().split()))
y=list(map(int, input().split()))
print(sum([abs(a-b) for (a,b) in zip(x,y)]))
print(math.sqrt(sum([abs(a-b)**2 for (a,b) in zip(x,y)])))
print(math.pow(sum([abs(a-b)**3 for (a,b) in zip(x,y)]), 1/3))
print((max([abs(a-b) for (a,b) in zip(x,y)])))

Submission status: Accepted

Solution code:
import math
n = int(input())
s = input().split(" ")
x = list(map(int,s))
s = input().split(" ")
y = list(map(int,s))
sa = []
for i in range(n):
    sa.append(math.fabs(x[i]-y[i]))
goukei = 0
for i in range(n):
    goukei += sa[i]
p1 = goukei
print(p1)
goukei = 0
for i in range(n):
    goukei += sa[i]**2
p2 = math.sqrt(goukei)
print(p2)
goukei = 0
for i in range(n):
    goukei += sa[i]**3
p3 = math.pow(goukei,1/3)
print(p3)
saidaisa = 0
for i in range(n):
    if sa[i] > saidaisa:
        saidaisa = sa[i]
print(saidaisa)

Submission status: Accepted

Solution code:
import math

n = int(input())
x = [float(x) for x in input().split()]
y = [float(y) for y in input().split()]
d = 0
for i in range(n):
    d += abs(x[i] - y[i])

print(d)

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

d = math.sqrt(d)
print(d)

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

d = math.pow(d, 1.0/3.0)
print(d)

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

print(d)

Submission status: Accepted

Solution code:
import math

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()))
D1=0
for i in range(n):
    D1+=abs(x[i]-y[i])
D2=0
for i in range(n):
    D2+=(x[i]-y[i])**2
D2=D2**0.5
D3=0
for i in range(n):
    D3+=abs(x[i]-y[i])**3
D3=D3**(1/3)
D4=0
for i in range(n):
    if D4<=abs(x[i]-y[i]):
        D4=abs(x[i]-y[i])
print("%.6f\n%.6f\n%.6f\n%.6f\n" %(D1,D2,D3,D4))

Submission status: Accepted

Solution code:
import math
n=int(input())
x=[float(i) for i in input().split()]
y=[float(i) for i in input().split()]
z=[float(0)for i in range(len(x))]
count=0.0
for i in range(len(x)):
    z[i]=abs(x[i]-y[i])
    count=count+abs(x[i]-y[i])
print("%.5f"%count)
count=0.0
for i in range(len(x)):
    count=count+abs(x[i]-y[i])*abs(x[i]-y[i])
print("%.5f"%math.sqrt(count))
count=0.0
for i in range(len(x)):
    count=count+abs(x[i]-y[i])*abs(x[i]-y[i])*abs(x[i]-y[i])
print("%.5f"%math.pow(count,(1.0/3.0)))
print("%.5f"%max(z))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
??????????????????????????¢

???????????????????????????????????????????????????????????????????????¢??§??¬???????????????
????????????????????°???????????????????§?????????¨????????§????????????????????????
????????§??????????????? n ?¬???????????????? 
x={x1,x2,...,xn}x={x1,x2,...,xn} ??¨ 
y={y1,y2,...,yn}y={y1,y2,...,yn} ????????¢????¨????????????????????????????

Dxy=(???i=1~n|x(i)???y(i)|^p)1/p

p=??? ?????¨??? 
Dxy=max(ni=1)(|xi???yi|)

??¨???????????????????????§????????§????????¢??¨?????°????????????

????????? n?¬???????????????????????????????????????§???p??????????????? 1???2???3?????????
??????????????????????????¢????±???????????????°????????????????????????????????????
"""
import math

def Mink(x,y,n,p):
    """
    p????????????????????????????????????????????¢
    """
    D = 0
    for i in range(n):
        j = abs(x[i] - y[i])
        b = j ** p
        D += b
    return D ** (1 / p)

def Chebyshev(x,y,n):
    """
    ?????§????????§????????¢
    """
    D = 0
    for i in range(n):
        j = abs(x[i] - y[i])
        if D < j:
            D = j
    return D


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

print(Mink(x,y,n,1))
print(Mink(x,y,n,2))
print(Mink(x,y,n,3))
print(Chebyshev(x,y,n))

Submission status: Accepted

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

xy = [abs(xi-yi) for xi,yi in zip(x,y)]

D = lambda p:sum([xyi**p for xyi in xy])**(1/p)

print(D(1.0))
print(D(2.0))
print(D(3.0))
print(max(xy))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

def main():
    N = int(input())
    X = tuple(map(int, input().split()))
    Y = tuple(map(int, input().split()))

    for i in (1, 2, 3):
        print(dist(X, Y, N, i))
    max_ = -float('inf')
    for i in range(N):
        max_ = max(max_, abs(X[i] - Y[i]))
    print(max_)

def dist(x, y, n, p):
    ret = 0
    for i in range(n):
        ret += abs(x[i] - y[i]) ** p
    return round(math.pow(ret, 1 / p), 6)
    
main()

Submission status: Accepted

Solution code:
def min_dist(x_b,y_b):
    s=0
    for x,y in zip(x_b,y_b):
        s+=abs(x-y)
    return s

def man_dist(x_b,y_b):
    s=0
    for x, y in zip(x_b,y_b):
        s+=(abs(x-y))**2
    return s**0.5

def che_dist(x_b,y_b):
    s=0
    for x,y in zip(x_b,y_b):
        s+=(abs(x-y))**3
    return s**(1/3)

def anf_dist(x_b,y_b):
    s=[abs(x-y) for x,y in zip(x_b,y_b)]
    return max(s)

n=int(input())
x_b=list(map(int,input().split()))
y_b=list(map(int,input().split()))

print(min_dist(x_b,y_b))
print(man_dist(x_b,y_b))
print(che_dist(x_b,y_b))
print(anf_dist(x_b,y_b))

Submission status: Accepted

Solution code:
n = int(input())

vec_x = (int(x) for x in input().split())
vec_y = (int(x) for x in input().split())

# calc each abs
abs_vec = [abs(int(x) - int(y)) for x, y in zip(vec_x, vec_y)]

for p in range(1, 4):
    print(sum(x ** p for x in abs_vec) ** float(1 / p))

print(max(abs_vec))

Submission status: Accepted

Solution code:
from math import exp
from math import log

n = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
Z = [abs(X[i] - Y[i]) for i in range(n)]
D1 = sum(Z)
try:
    D2 = exp((1 / 2) * log(sum((Z[i] ** 2) for i in range(n))))
    D3 = exp((1 / 3) * log(sum((Z[i] ** 3) for i in range(n))))
except ValueError:
    D2 = 0
    D3 = 0
Di = max(Z)
print("{:.5f}".format(D1))
print("{:.5f}".format(D2))
print("{:.5f}".format(D3))
print("{:.5f}".format(Di))

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

Submission status: Accepted

Solution code:
a=int(input())
x=list(map(float,input().split()))
y=list(map(float,input().split()))
d1=0
d2=0
d3=0
for i in range(a):
    d1+=abs(x[i]-y[i])
    d2+=(x[i]-y[i])**2
    d3+=abs((x[i]-y[i])**3)
d22=d2**0.5
d33=d3**(1/3)
print(d1)
print(d22)
print(d33)
print(max([abs(x[i]-y[i]) for i in range(a)]))

Submission status: Accepted

Solution code:
# Aizu Problem ITP_1_10_D: Distance II
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


n = int(input())
X = [int(_) for _ in input().split()]
Y = [int(_) for _ in input().split()]
for p in range(1, 4):
    D = 0
    for i in range(n):
        D += pow(abs(X[i] - Y[i]), p)
    D = pow(D, 1 / p)
    print("%.6f" % D)
D = 0
for i in range(n):
    D = max(D, abs(X[i] - Y[i]))
print("%.6f" % D)

Submission status: Accepted

Solution code:
import math
n=int(input())
xli=list(map(float,input().split()))
yli=list(map(float,input().split()))
a=0
b=0
c=0
d=0
 
for x,y in zip(xli,yli):
    base = math.fabs(x-y)
    a += base
    b += base**2
    c += base**3
    if d < base : d = base
 
print(a)
print(b**(1/2))
print(c**(1/3))
print(d)

Submission status: Accepted

Solution code:
import math
n = int(input())
x = [int(num) for num in input().split()]
y = [int(num) for num in input().split()]
D1 = 0
D2 = 0
D3 = 0
D4 = []
for i, j in zip(x, y):
    D1 += abs(i - j)
print(D1)
for i, j in zip(x, y):
    D2 += (abs(i - j))**2
print(math.sqrt(D2))
for i, j in zip(x, y):
    D3 += (abs(i - j))**3
print(math.pow(D3, 1.0/3.0))
for i, j in zip(x, y):
    D4.append(abs(i - j))
print(max(D4))

Submission status: Accepted

Solution code:
import math
ni = int(input())
xi = map(int, raw_input().split())
yi = map(int, raw_input().split())

p1 = []
p2 = []
p3 = []
p4 = []
for x, y in zip(xi, yi):
    p1.append(abs(x - y))
    p2.append(abs(x - y)**2)
    p3.append(abs(x - y)**3)
    p4.append(abs(x - y))

print '%.8f' % reduce(lambda x, y: x + y, p1)
print '%.8f' % reduce(lambda x, y: x + y, p2) ** (1.0/2.0)
print '%.8f' % reduce(lambda x, y: x + y, p3) ** (1.0/3.0)
print '%.8f' % max(p4)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
p1 = p2 = p3 = pi = 0
for i in range(n):
    p1 += abs(a[i] - b[i])
    p2 += (abs(a[i] - b[i]))**2
    p3 += (abs(a[i] - b[i]))**3
    if pi < abs(a[i] - b[i]):
        pi = abs(a[i] - b[i])

print("%f" % (p1))
print("%f" % (p2**(1/2)))
print("%f" % (p3**(1/3)))
print("%f" % (pi))

Submission status: Accepted

Solution code:
num = int(input())
x_list = [float(i) for i in input().split()]
y_list = [float(i) for i in input().split()]
distance = 0
tmp = 0
sum_dis = 0
for i in range(1, 4):
    for j in range(num):
        tmp = (abs(x_list[j] - y_list[j])) ** i
        distance += tmp
        if sum_dis < tmp and i == 1:
            sum_dis = tmp
    distance = distance ** (1/i)
    print("{:.6f}".format(distance))
    distance = 0
print("{:.6f}".format(sum_dis))

Submission status: Accepted

Solution code:
from math import sqrt

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

    f1 = lambda it: abs(it[0]-it[1])
    f2 = lambda it: (it[0]-it[1]) ** 2
    f3 = lambda it: abs(it[0]-it[1]) ** 3

    yield sum(map(f1, zip(x, y)))
    yield sqrt(sum(map(f2, zip(x, y))))
    yield sum(map(f3, zip(x, y))) ** (1/3)
    yield max(map(f1, zip(x, y)))

for n in g():
    print("{:.6f}".format(n))

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

Submission status: Accepted

Solution code:
from functools import reduce

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

d = list(map(lambda x, y: abs(x - y), xs, ys))
ds = [sum([i ** p for i in d]) ** (1 / p) for p in range(1, 4)]
ds.append(max(d))

print(reduce(lambda h, t: h + '\n' + t, [str(e) for e in ds]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

def str2list(str):
	result = []
	for value in str.split(' '):
		result.append(int(value))
	return result

def distance1(n, x, y):
	result = 0
	for i in range(n):
		result += abs(x[i] - y[i])
		
	return result
	
def distance2(n, x, y):
	result = 0
	for i in range(n):
		result += (x[i] - y[i]) * (x[i] - y[i])
		
	return math.sqrt(result) if result != 0 else 0
	
def distance3(n, x, y):
	result = 0
	for i in range(n):
		result += math.pow(abs(x[i] - y[i]), 3)
		
	return math.exp(math.log(result)/3) if result != 0 else 0
	
def distanceInf(n, x, y):
	result = 0
	for i in range(n):
		new_result = abs(x[i] - y[i])
		result = new_result if new_result > result else result
		
	return result

n = int(input());
x = str2list(input())
y = str2list(input())

print('%.6f'%distance1(n, x, y))
print('%.6f'%distance2(n, x, y))
print('%.6f'%distance3(n, x, y))
print('%.6f'%distanceInf(n, x, y))

Submission status: Accepted

Solution code:
import math

n = int(raw_input())
x = map(int, raw_input().split())
y = map(int, raw_input().split())

for i in range(3):
	sum = 0
	for j in range(n):
		sum += (math.fabs(x[j] - y[j]))**(i+1)
		#print("sum = {:}".format(sum))
	ans = sum ** (1.0/(i+1))
	print("{:.6f}".format(ans))

ans = 0
for i in range(n):
	if math.fabs(x[i] - y[i]) > ans:
		ans = math.fabs(x[i] - y[i])
print("{:.6f}".format(ans))

Submission status: Accepted

Solution code:
import math
def dist(a,b,n):
    s=0
    m=0
    for i in range(len(a)):
        d=math.fabs(b[i]-a[i])
        m=max(m,d)
        s+=d**n
    return (s**(1/n),m)

n=int(input())
a=list(map(float,input().split()))
b=list(map(float,input().split()))
print('%.6f'%(dist(a,b,1)[0]))
print('%.6f'%(dist(a,b,2)[0]))
print('%.6f'%(dist(a,b,3)[0]))
print('%.6f'%(dist(a,b,1)[1]))

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(pow(sum([pow(abs(x - y),2) for x,y in zip(x,y)]), 1/2))
print(pow(sum([pow(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:
import math

input()
xl = [int(s) for s in input().split()]
yl = [int(s) for s in input().split()]

m1 = 0
m2 = 0
m3 = 0
m4 = 0
for x, y in zip(xl, yl):
    fabs = math.fabs(x - y) 
    m1 += fabs
    m2 += fabs ** 2
    m3 += fabs ** 3
    m4 = max(fabs, m4)
    
print(m1)
print(math.pow(m2, 1/2))
print(math.pow(m3, 1/3))
print(m4)

Submission status: Accepted

Solution code:
from __future__ import division


def dist(vx, vy, p, n):
    if p == float("inf"):
        d = max([abs(vx[i] - vy[i]) for i in range(n)])
        return d
    d = 0
    for i in range(n):
        d += abs(vx[i]-vy[i]) ** p
    d = d ** (1/p)
    return d


n = int(raw_input())
vx = map(int, raw_input().split())
vy = map(int, raw_input().split())

print "{:.6f}".format(dist(vx, vy, 1, n))
print "{:.6f}".format(dist(vx, vy, 2, n))
print "{:.6f}".format(dist(vx, vy, 3, n))
print "{:.6f}".format(dist(vx, vy, float("inf"), n))

Submission status: Accepted

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

import math

def main():

    inputNum = int(input())

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

    absVec = []
    absVec2 = []
    absVec3 = []
    for i, j in zip(x, y):
        absVec.append(abs(i-j))
        absVec2.append(pow(abs(i-j),2))
        absVec3.append(pow(abs(i-j),3))

    print(sum(absVec))
    print(pow(sum(absVec2),1/2))
    print(pow(sum(absVec3),1/3))
    print(max(absVec))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
z = [abs(x[i] - y[i]) for i in range(n)]
dist_1 = 0.
dist_2 = 0.
dist_3 = 0.
dist_infty = float(max(z))
for i in range(n):
    dist_1 += z[i]
    dist_2 += z[i] ** 2
    dist_3 += z[i] ** 3
dist_2 = pow(dist_2, 1 / 2.)
dist_3 = pow(dist_3, 1 / 3.)
print(dist_1, dist_2, dist_3, dist_infty, sep='\n')

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    xs = list(map(int, input().split()))
    ys = list(map(int, input().split()))

    dist_ma = sum(abs(x - y) ** 1 for x, y in zip(xs, ys)) ** (1 / 1)
    dist_eu = sum(abs(x - y) ** 2 for x, y in zip(xs, ys)) ** (1 / 2)
    dist_p3 = sum(abs(x - y) ** 3 for x, y in zip(xs, ys)) ** (1 / 3)
    dist_inf = max(abs(x - y) for x, y in zip(xs, ys))

    print(dist_ma)
    print(dist_eu)
    print(dist_p3)
    print(dist_inf)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
x=list(int(s) for s in input().split())
y=list(int(s) for s in input().split())
d=[(s-t,t-s)[s<t]for s,t in zip(x,y)]
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:
import math
n = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

d_1 = 0
d_2 = 0
d_3 = 0
d_4 = []
for i in range(n):
    d_1 += abs(x[i] - y[i])
    d_2 += abs(x[i] - y[i]) ** 2
    d_3 += abs(x[i] - y[i]) ** 3
    d_4.append(abs(x[i] - y[i]))
print("{0:8f}".format(d_1))
print("{0:8f}".format(math.sqrt(d_2)))
print("{0:8f}".format(d_3 ** (1/3)))
print("{0:8f}".format(max(d_4)))

Submission status: Accepted

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

x_minus_y = list(map(lambda xi, yi: xi - yi, x, y))

print(sum(list(map(abs, x_minus_y))))
print(sum(list(map(lambda x: x**2, x_minus_y))) ** .5)
print(sum(list(map(lambda x: abs(x)**3, x_minus_y))) ** (1 / 3))
print(max(list(map(abs, x_minus_y))))

Submission status: Accepted

Solution code:
import math
n=int(input())
p=print
i=input
a=[int(s) for s in i().split()]
b=[int(s) for s in i().split()]
m1=0
m2=0
m3=0
m4=0
for x,y in zip(a,b):
    fabs=math.fabs(x-y)
    m1+=fabs
    m2+=fabs**2
    m3+=fabs**3
    m4=max(fabs,m4)
p(m1)
p(pow(m2,1/2))
p(pow(m3,1/3))
p(m4)

Submission status: Accepted

Solution code:
import math

n = int(input())

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

one = sum(math.fabs(x[i] - y[i]) for i in range(n))
print("{0:5f}".format(one))

two = math.sqrt(sum((x[i] - y[i]) ** 2 for i in range(n)))
print("{0:5f}".format(two))

three = (sum(math.fabs((x[i] - y[i]) ** 3) for i in range(n))) ** (1/3)
print("{0:5f}".format(three))

inf = max(math.fabs(x[i] - y[i]) for i in range(n))
print("{0:5f}".format(inf))

Submission status: Accepted

Solution code:
input()
P1 = []
P2 = []
P3 = []
for a, b in zip(map(int, input().split()), map(int, input().split())):
    t = abs(a - b)
    P1.append(t)
    P2.append(t ** 2)
    P3.append(t ** 3)
print("{:f}".format(sum(P1)))
print(sum(P2) ** (1 / 2))
print(sum(P3) ** (1 / 3))
print("{:f}".format(max(P1)))

Submission status: Accepted

Solution code:
n = int(input())
x = input().split()
y = input().split()
import math
for i in range(4):
    D = 0
    for j in range(n):
        if i != 3:
            D = D + math.pow(math.fabs(int(x[j]) - int(y[j])),i+1)
        else:
            compare = math.fabs(int(x[j]) - int(y[j]))
            if D < compare:
                D = compare
    if i != 3:
        print('{0:.6f}'.format(math.pow(D,1/(i+1))))
    else:
        print('{0:.6f}'.format(D))

Submission status: Accepted

Solution code:
import math
N = int(input())
X = list(map(float, input().split()))
Y = list(map(float, input().split()))
p1 = 0
i = 0
while i < N :
    p1 = p1 + abs(X[i]-Y[i])
    i = i + 1
print(p1)

p2 = 0
i = 0
while i < N :
    p2 = p2 + (X[i]-Y[i])**2
    i = i + 1
print (math.sqrt(p2))

p3 = 0
i = 0
while i < N :
    p3 = p3 + abs((X[i]-Y[i])**3)
    i = i + 1
print(math.pow(p3, (1/3)))

p4 = abs(X[0]-Y[0])
i = 0
while i < N-1 :
    if p4 < abs(X[i+1]-Y[i+1]) :
        p4 = abs(X[i+1]-Y[i+1])
    i = i + 1
print(p4)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
d=[abs(s-t)for s,t in zip(x,y)]
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,y=[list(map(int,input().split()))for _ in range(2)]
d=[abs(s-t)for s,t in zip(x,y)]
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,y=[list(map(int,input().split()))for _ in range(2)]
d=[abs(s-t)for s,t in zip(x,y)]
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 = input()
xs = map(float, input().split())
ys = map(float, input().split())
abs_d = [abs(x - y) for (x, y) in zip(xs, ys)]
d = [sum([i ** p for i in abs_d]) ** (1 / p) for p in range(1, 4)]
d.append(max(abs_d))

print(*d, sep='\n')

Submission status: Accepted

Solution code:
import math

def hoge(x, y, p):
    s = 0
    for x_i, y_i in zip(x, y):
         s += math.pow(math.fabs(x_i - y_i), p)
    return math.pow(s, 1/p)

_ = input()
x = [int(e) for e in input().split()]
y = [int(e) for e in input().split()]
print (hoge(x, y, 1))
print (hoge(x, y, 2))
print (hoge(x, y, 3))
print (max([math.fabs(x_i - y_i) for x_i, y_i in zip(x,y)]))

Submission status: Accepted

Solution code:
# coding:utf-8
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:
n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
dif = [abs(i-j) for i, j in zip(x, y)]
print(sum(dif))
print(sum([i**2 for i in dif])**0.5)
print(sum([i**3 for i in dif])**(1/3))
print(max(dif))

Submission status: Accepted

Solution code:
dim = int(input())
a, b = list(map(int, input().split())), list(map(int, input().split()))
dif = [ abs(x-y) for x, y in zip(a, b)]
print("%lf\n%lf\n%lf\n%lf" % (sum(dif), 
                                sum([i ** 2 for i in dif]) ** (1 / 2),
                                sum([i ** 3 for i in dif]) ** (1 / 3),
                                max(dif)
                                ))

Submission status: Accepted

Solution code:
n = int(input())
x = [int(x) for x in input().split()]
y = [int(x) for x in input().split()]
abs_list = [abs(a-b) for a,b in zip(x,y)]
p1 = p2 = p3 = p4 = 0
for a in abs_list:
    p1 += a
    p2 += a**2
    p3 += a**3
p2 = p2 **(1/2)
p3 = p3 **(1/3)
p4 = max(abs_list)
preanswer = [p1,p2,p3,p4]
answer = ['{:.6f}'.format(i) for i in preanswer]
for a in answer:
    print(a)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

n = int(sys.stdin.readline())
x = list(map(int, sys.stdin.readline().split()))
y = list(map(int, sys.stdin.readline().split()))

for dim in range(1, 4):
    total = 0
    for i in range(n):
        total += abs(x[i]-y[i]) ** dim
    print(round(total ** (1/dim), 6))

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

print(float(maxval))

Submission status: Accepted

Solution code:
n, x, y = int(input()), list(map(int, input().split())), list(map(int, input().split()))
 
 
def dist(p):
    return pow(sum(pow(abs(a - b), p) for a, b in zip(x, y)), 1.0 / p)
 
 
for i in [1, 2, 3]:
    print(dist(i))
 
print(max(abs(a - b) for a, b in zip(x, y)))

Submission status: Accepted

Solution code:
from math import sqrt
def distance2(x, y, p='infinite'):
    if p == 'infinite':
        return max([abs(dx - dy) for dx, dy in zip(x,y)])
    else:
        return sum([(abs(dx - dy)**p) for dx, dy in zip(x, y)])**(1/p)

if __name__ == '__main__':
    n = int(input())
    x = list(map(int, input().split()))[0:n]
    y = list(map(int, input().split()))[0:n]
    print(distance2(x, y, 1))
    print(distance2(x, y, 2))
    print(distance2(x, y, 3))
    print(distance2(x, y))

Submission status: Accepted

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

xy_abs = [abs(x[i] - y[i]) for i in range(n)]
manhattan_distance = sum(xy_abs)
euclid_distance = sum([xy ** 2 for xy in xy_abs]) ** 0.5
p3_distance = sum([xy ** 3 for xy in xy_abs]) ** (1/3)
chebyshev_distance = max(xy_abs)

print(manhattan_distance)
print(euclid_distance)
print(p3_distance)
print(chebyshev_distance)

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(float, input().split()))
y = list(map(float, input().split()))
for p in [1.0, 2.0, 3.0]:
    a = 0
    for i in range(n):
        a += abs(x[i] - y[i]) ** p
    D = a ** (1/p)
    print(D)
inf_D = float("-inf")
for i in range(n):
    if abs(x[i] - y[i]) > inf_D:
        inf_D = abs(x[i] - y[i])
print(inf_D)