Problem:
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

Solution (Python):
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))

Solution (Python):


"""
??????????????????????????¢

???????????????????????????????????????????????????????????????????????¢??§??¬???????????????
????????????????????°???????????????????§?????????¨????????§????????????????????????
????????§??????????????? 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))

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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()

Solution (Python):
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))

Solution (Python):
n = int(input())

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


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

Solution (Python):
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))

Solution (Python):
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)))

Solution (Python):
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)))

Solution (Python):
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)]))

Solution (Python):


import sys, math, os


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)

Solution (Python):
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)

Solution (Python):
input()
X_vec = map(float, input().split())
Y_vec = map(float, input().split())
pair = list(zip(X_vec, Y_vec))
D1 = sum(abs(x-y) for x,y in pair)
D2 = (sum((x-y)**2 for x,y in pair)) ** .5
D3 = (sum(abs(x-y)**3 for x,y in pair)) ** (1/3)
Dinf = max(abs(x-y) for x,y in pair)
print("{0:.6f}\n{1:.6f}\n{2:.6f}\n{3:.6f}\n".format(D1,D2,D3,Dinf))

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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]))

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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)
		
	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))

Solution (Python):
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]))

Solution (Python):
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)]))

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):


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

Solution (Python):
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')

Solution (Python):
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()

Solution (Python):
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')

Solution (Python):
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)))

Solution (Python):
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))))

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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)))

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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')

Solution (Python):
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')

Solution (Python):
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')

Solution (Python):
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')

Solution (Python):
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)]))

Solution (Python):

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

Solution (Python):
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))

Solution (Python):
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)
                                ))

Solution (Python):
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)

Solution (Python):



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

Solution (Python):
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)))

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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())  

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

Solution (Python):

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)

Solution (Python):
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)))

Solution (Python):
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)):

    D1 += ((x_i[i]-y_i[i])**2)**(1/2)

    D2 +=(x_i[i]-y_i[i])**2

    D3 +=(((x_i[i]-y_i[i])**2)**(1/2))**3

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

Solution (Python):
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)]))

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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)))

Solution (Python):


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)

Solution (Python):



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

Solution (Python):
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')

Solution (Python):
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')

Solution (Python):
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)))

Solution (Python):
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)))

Solution (Python):
import math

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

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)


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


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


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)

Solution (Python):
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

Solution (Python):
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))

Solution (Python):
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)))