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
def main():

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

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


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

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

if __name__ == '__main__':
    main()

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

Solution (Python):
def main():
    input()
    x, y = [list(map(int, input().split())) for _ in range(2)]
    for p in (1, 2, 3):
        distance = (sum(map(lambda i: i ** p, [abs(xi - yi) for xi, yi, in zip(x, y)]))) ** (1/p)
        print('{:.5f}'.format(distance))
    distance = max([abs(xi - yi) for xi, yi in zip(x, y)])
    print('{:.5f}'.format(distance))


if __name__ == '__main__':
    main()

Solution (Python):
import math
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
p1 = p2 = p3 = p = 0
for x,y in zip(x,y):
    d = abs(x-y)
    p1 += d
    p2 += d**2
    p3 += d**3
    if d>p: p = d
print('{0:.5f}\n{1:.5f}\n{2:.5f}\n{3:.5f}'.format(p1,math.sqrt(p2),math.pow(p3,1/3),p))

Solution (Python):
import decimal
d = int(input())

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

D_1 = 0
for i in range(d):
    L_i = abs(x[i] - y[i])
    D_1 += L_i

D_2 = 0
for i in range(d):
    L_i = abs(x[i]-y[i])**2
    D_2 += L_i
D_2 = (D_2) **(1/2)

D_3 = 0
for i in range(d):
    L_i = abs(x[i]-y[i])**3
    D_3 += L_i
D_3 = (D_3) ** (1/3)

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

D_ = max(L)

print('{:.8f}'.format(D_1))
print('{:.8f}'.format(D_2))
print('{:.8f}'.format(D_3))
print('{:.8f}'.format(D_))

Solution (Python):
def dist(p, X, Y):
    s = 0
    for x, y in zip(X, Y):
        s += abs(x - y)**p
    return s**(1/p)
N = int(input())
*X, = map(int, input().split())
*Y, = map(int, input().split())
print(dist(1, X, Y))
print(dist(2, X, Y))
print(dist(3, X, Y))
print(max(abs(x - y) for x, y in zip(X, Y)))

Solution (Python):
n=int(input())
s_str=[]
for i in range(2):
    s_str.append(input().split())
s_int=[]
for i in s_str:
    s_int.append([int(s) for s in i])
manh=0
yu=0
p_3=0
che=[]
for i in range(n):
    manh+=abs(s_int[0][i]-s_int[1][i])
    yu+=(s_int[0][i]-s_int[1][i])**2
    p_3+=(abs(s_int[0][i]-s_int[1][i]))**3
    che.append(abs(s_int[0][i]-s_int[1][i]))
print(manh)
print(yu**(1/2))
print(p_3**(1/3))
print(max(che))

Solution (Python):
n = int(input())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
Distance = [abs(xi-yi) for (xi,yi) in zip(x,y)]
print(sum(Distance))
Euclid=[i**2 for i in Distance]
print(sum(Euclid)**0.5)
wa = [i**3 for i in Distance]
print(sum(wa)**(1/3))
print(max(Distance))

Solution (Python):
from sys import stdin
 
n = int(stdin.readline().rstrip())
x = [float(x) for x in stdin.readline().rstrip().split()]
y = [float(x) for x in stdin.readline().rstrip().split()]
 
def minkowski_dist(x, y, p="INF"):
    if p == "INF":
        return max([abs(x[i]-y[i]) for i in range(n)])
    elif isinstance(p, int):
        return (sum([(abs(x[i]-y[i]))**p for i in range(n)]))**(1/p)
 
print(minkowski_dist(x, y, 1))
print(minkowski_dist(x, y, 2))
print(minkowski_dist(x, y, 3))
print(minkowski_dist(x, y))

Solution (Python):
import math


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







tmp = 0
for i in range(n):
    tmp += abs(x[i] - y[i])
print('%f' % (tmp))

tmp = 0
for i in range(n):
    tmp += (abs(x[i] - y[i]))**2
print('%f' % (math.sqrt(tmp)))

tmp = 0
for i in range(n):
    tmp += (abs(x[i] - y[i]))**3
print('%f' % (pow(tmp, 1 / 3)))

tmp = 0
for i in range(n):
    tmp = abs(x[i] - y[i]) if tmp < abs(x[i] - y[i]) else tmp
print('%f' % (tmp))

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

dsum1 = 0
dsum2 = 0
dsum3 = 0
dmax = 0
for i in range(len(x)):
	d = abs(x[i]-y[i])
	dsum1 += d
	dmax = max(dmax, d)
	dsum2 += d**2
	dsum3 += d**3
print('{0:.6f}'.format(dsum1))
print('{0:.6f}'.format(dsum2**(1/2)))
print('{0:.6f}'.format(dsum3**(1/3)))
print('{0:.6f}'.format(dmax))

Solution (Python):
import math

num = int(input())

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

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

diff = [0]*num

for a in range(num):
    diff[a] = abs(x[a] - y[a])
    
print(sum(diff)) 

po = [0]*num

for b in range(num):
    po[b] = diff[b] * diff[b]
    
print(math.sqrt(sum(po))) 



for c in range(num):
    po[c] = diff[c] * diff[c] * diff[c]
    
print((sum(po))**(1/3)) 

_max = 0

for d in diff:
    if _max < d:
        _max = d

print(_max)

Solution (Python):


import math

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

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

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

result = 0
for i in range(num):
    result += abs(y[i] - x[i]) ** 3
result = math.pow(result,1.0/3.0)
print(result)

result = 0
for i in range(num):
    if i == 0:
        result = abs(y[i] - x[i])
    else:
        if abs(y[i] - x[i]) > result:
            result = abs(y[i] - x[i])
print(result)

Solution (Python):


if __name__ == '__main__':
    n = int(raw_input())
    X = map(int, raw_input().split())
    Y = map(int, raw_input().split())

    dist = []
    for i in range(n):
        dist += [abs(X[i]-Y[i])]

    print sum(dist)
    print sum(map(lambda x : x**2, dist))**(1.0/2)
    print sum(map(lambda x : x**3, dist))**(1.0/3)
    print max(dist)

Solution (Python):
import math
n=int(input())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
a=[1,2,3,4]
for i in a:
	num=0
	if i==1:
		for j in range(len(x)):
			num+=abs(x[j]-y[j])
		print(num)
			
	elif i==2 or i==3:
		for j in range(len(x)):
			num+=(abs(x[j]-y[j]))**i
		num=math.pow(num,1/i)
		print(num)
	
	else:
		b=[]
		for j in range(len(x)):
			b.append(abs(x[j]-y[j]))
		print(float(max(b)))

Solution (Python):
def dist(p, X, Y):
    s = 0
    for x, y in zip(X, Y):
        s += abs(x-y)**p
    return s**(1/p)
N = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
print(dist(1,X,Y))
print(dist(2,X,Y))
print(dist(3,X,Y))
print(max(abs(x-y)for x, y in zip(X, Y)))

Solution (Python):
num = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
first = second = third = inf = 0.00000
for i in range(num):
    dif = x[i] - y[i]
    first = first + abs(dif)
    second = second + dif ** 2
    third = third + abs(dif) ** 3
    inf = max(inf, abs(dif))
print('{:.6f}'.format(first))
print('{:.6f}'.format(second ** 0.5))
print('{:.6f}'.format(third ** 0.333333333333333333))
print('{:.6f}'.format(inf))

Solution (Python):
import math

class vector():

    def __init__(self, x):
        self.x = x

def minkowski_p(x, y, p):
    s = 0

    for xi, yi in zip(x, y):
        s += abs(xi - yi) ** p

    s = s ** (1 / p)

    return s

def minkowski_infty(x, y):
    return max([abs(xi - yi) for xi, yi in zip(x, y)])

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

print(minkowski_p(x, y, 1))
print(minkowski_p(x, y, 2))
print(minkowski_p(x, y, 3))
print(minkowski_infty(x, y))

Solution (Python):

from math import *
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 += pow(abs(x[i]-y[i]),2)
        p_3 += pow(abs(x[i]-y[i]),3)
        p_inf.append(abs(x[i]-y[i]))
print("%f" %p_1)
print("%f" %(sqrt(p_2)))
print("%f" %(pow(p_3,1/3)))
print("%f" %(max(p_inf)))

Solution (Python):
def minkowsuki(x, y, n):
    return sum(abs(x[i] - y[i]) ** n for i in range(len(x))) ** (1 / n)

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

print(minkowsuki(x, y, 1))
print(minkowsuki(x, y, 2))
print(minkowsuki(x, y, 3))
print(max(abs(x[i] - y[i]) for i in range(n)))

Solution (Python):
import math
n = input()
x = map(float,raw_input().split(' '))
y = map(float,raw_input().split(' '))
d1,d2,d3,d4 = 0.0,0.0,0.0,0.0
for i in range(0,n):
	d1 += abs(x[i]-y[i])
	d2 += abs(x[i]-y[i])**2.0
	d3 += abs(x[i]-y[i])**3.0
	if d4 < abs(x[i]-y[i]):
		d4 = abs(x[i]-y[i])
print d1
print math.sqrt(d2)
print d3**(1.0/3)
print d4

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):
def f(): return map(int,raw_input().split())
def f1(n): return sum([e**n for e in x])**(1.0/n)
def f2(n): print '%.6f' %(n)

raw_input()
x = [abs(a-b) for a,b in zip(f(),f())]
f2(f1(1))
f2(f1(2))
f2(f1(3))
f2(max(x))

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

x_y = [abs(xi - yi) for xi, yi in zip(x, y)]
p1 = sum(x_y)
p2 = sum(i ** 2 for i in x_y) ** (1 / 2)
p3 = sum(i ** 3 for i in x_y) ** (1 / 3)
p_inf = max(x_y)

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

Solution (Python):
import math
input()
vx = [int(i) for i in input().split()]
vy = [int(i) for i in input().split()]

Dxy = []
for p in range(1,4):
    Dxy.append(sum([abs(vx[i] - vy[i]) ** p for i in range(len(vx))]) ** (1 / p))

Dxy.append(max([abs(vx[i] - vy[i]) for i in range(len(vx))]))
for res in Dxy:
    print('{0:.5f}'.format(res))

Solution (Python):
import math

input()
vx = [int(i) for i in input().split()]
vy = [int(i) for i in input().split()]

Dxy = []
for p in range(1,4):
    Dxy.append(sum([abs(vx[i] - vy[i]) ** p for i in range(len(vx))]) ** (1/p))

Dxy.append(max([abs(vx[i] - vy[i]) for i in range(len(vx))]))
for res in Dxy:
    print("{0:.5f}".format(res))

Solution (Python):
import math
input()
vx = [int(i) for i in input().split()]
vy = [int(i) for i in input().split()]

Dxy = []
for p in range(1,4):
    Dxy.append(sum([abs(vx[i] - vy[i]) ** p for i in range(len(vx))]) ** (1 / p))

Dxy.append(max([abs(vx[i] - vy[i]) for i in range(len(vx))]))
for res in Dxy:
    print('{0:.5f}'.format(res))

Solution (Python):
import math

input()

vx = [int(i) for i in input().split()]
vy = [int(i) for i in input().split()]

Dxy = []
for p in range(1, 4):
    Dxy.append(sum([abs(vx[i] - vy[i]) ** p for i in range(len(vx))]) ** (1 / p))

Dxy.append(max([abs(vx[i] - vy[i]) for i in range(len(vx))]))

for res in Dxy:
    print('{0:5f}'.format(res))

Solution (Python):
from math import *

n = input()
x = map(float, raw_input().split())
y = map(float, raw_input().split())

for i in range(1,4):
    temp = 0
    for j in range(n):
        temp += fabs(x[j]-y[j])**i
    print pow(temp,1.0/i)

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

Solution (Python):
import sys
n = int(sys.stdin.readline())
x = [float(i) for i in sys.stdin.readline().split()]
y = [float(i) for i in sys.stdin.readline().split()]
d = [abs(x_i - y_i) for (x_i, y_i) in zip(x, y)]


for p in range(1, 4):
    print(sum([a ** p for a in d]) ** (1/p))


print(max(d))

Solution (Python):
def dist(x,y,p):
    tmp = 0
    for k,j in zip(x,y):
        k,j = sorted((k,j))
        tmp += (j-k)**p
    print tmp**(1/float(p))

def cheb(x,y):
    maxjk = None
    for k,j in zip(x,y):
        k,j = sorted((k,j))
        if maxjk is None:
            maxjk = j-k
        elif j-k > maxjk:
            maxjk = j-k
    print float(maxjk)

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

dist(x,y,1)
dist(x,y,2)
dist(x,y,3)
cheb(x,y)

Solution (Python):
import math

def minkowski(x, y, p):
    if p == 0:
        return max(map(lambda xi,yi: abs(xi-yi), x, y))
    else:
        return math.pow(sum(map(lambda xi,yi: math.pow(abs(xi-yi), p), x, y)), 1/p)

if __name__ == '__main__':
    n = int(input())
    x = [int(i) for i in input().split()]
    y = [int(i) for i in input().split()]
    print("{0:.6f}".format(minkowski(x, y, 1)))
    print("{0:.6f}".format(minkowski(x, y, 2)))
    print("{0:.6f}".format(minkowski(x, y, 3)))
    print("{0:.6f}".format(minkowski(x, y, 0)))

Solution (Python):



def distance(p,vectorX,vectorY):
    d = sum([abs(vectorX[i] - vectorY[i]) ** p for i in range(len(vectorX))]) ** (1/p)
    return d

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

Solution (Python):
import math

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


n = int( raw_input() )
x = [ float( i ) for i in raw_input( ).split( " " ) ]
y = [ float( i ) for i in raw_input( ).split( " " ) ]
print( getDistance( x, y, n, 1 ) )
print( getDistance( x, y, n, 2 ) )
print( getDistance( x, y, n, 3 ) )
dxy = [ 0 ]*(n+1)
for i in range( n ):
	dxy[i] = abs( x[i] - y[i] )

print( max( dxy ) )

Solution (Python):
import math

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


n = int( input() )
x = [ float( i ) for i in input( ).split( " " ) ]
y = [ float( i ) for i in input( ).split( " " ) ]
print( getDistance( x, y, n, 1 ) )
print( getDistance( x, y, n, 2 ) )
print( getDistance( x, y, n, 3 ) )
dxy = [ 0 ]*(n+1)
for i in range( n ):
	dxy[i] = abs( x[i] - y[i] )

print( max( dxy ) )

Solution (Python):
def dist(X, Y, p):
    res = 0
    for x, y in zip(X, Y):
        res += abs(x - y) ** p
    return res ** (1.0 / p)
    
n = int(raw_input())
X = map(int, raw_input().split())
Y = map(int, raw_input().split())
for i in [1, 2, 3]:
    print dist(X, Y, i)
print max(map(lambda xy: abs(xy[0] - xy[1]), zip(X, Y)))

Solution (Python):
def dist(X, Y, p):
    return sum(abs(x - y) ** p for x, y in zip(X,Y)) ** (1.0 / p)
    
n = input()
X = map(int, raw_input().split())
Y = map(int, raw_input().split())
for i in [1, 2, 3]:
    print dist(X, Y, i)
print max(map(lambda xy: abs(xy[0] - xy[1]), zip(X, Y)))

Solution (Python):
import math
n=int(raw_input())
x=list(map(int,raw_input().split()))
y=list(map(int,raw_input().split()))
z=[0]*n
zz=[0]*n
zzz=[0]*n
for i in range(n):
	x.append(y[i])
for i in range(n):
	z[i]=math.fabs(x[i]-x[n+i])
	zz[i]=z[i]**2
	zzz[i]=z[i]**3
print sum(z)
print math.sqrt(sum(zz))
print sum(zzz)**(1/3.0)
print max(z)

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

x_y = [abs(xi - yi) for xi, yi in zip(x, y)]
for p in range(1, 4):
    print(sum(i ** p for i in x_y) ** (1 / p))
print(max(x_y))

Solution (Python):

from __future__ import division
from math import sqrt


def main():
    d = input()
    x = map(int, raw_input().split())
    y = map(int, raw_input().split())
    z = [x - y for x, y in zip(x, y)]
    print sum([abs(w) for w in z])
    print sqrt(sum([w ** 2 for w in z]))
    print sum([abs(w) ** 3 for w in z]) ** (1 / 3)
    print max([abs(w) for w in z])


if __name__ == '__main__':
    main()

Solution (Python):

from math import sqrt


def main():
    d = input()
    x = map(int, raw_input().split())
    y = map(int, raw_input().split())
    z = [abs(x - y) for x, y in zip(x, y)]
    print sum(z)
    print sqrt(sum([w ** 2 for w in z]))
    print sum([w ** 3 for w in z]) ** (1.0 / 3)
    print max(z)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
for p in range(1, 5):
    if p < 4:
        print(sum(abs(x - y) ** p for x, y in zip(X, Y)) ** (1 / p))
    else:
        print(max(abs(x - y) for x, y in zip(X, Y)))

Solution (Python):
import math

def getDistance(n, x, y, p):
  temp = 0.0
  for i in xrange(n):
    temp += math.fabs(x[i] - y[i]) ** p
    
  temp2 = 1.0 / p
  
  return math.pow(temp, temp2)


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

for i in xrange(1, 4):
  print "{0:.6f}".format(getDistance(n, x, y, i))

max = 0.0
for i in xrange(n):
  d = math.fabs(x[i] - y[i])
  if d > max:
    max = d

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

Solution (Python):
n=int(raw_input())
x=map(float,raw_input().split())
y=map(float,raw_input().split())
for p in [1,2,3]:
    d=sum([abs(x[i]-y[i])**p for i in xrange(len(x))])**(1.0/p)
    print '%6f' %(d)
print '%6f' %max([abs(x[i]-y[i]) for i in xrange(len(x))])

Solution (Python):
import math
n = input()
x = map(int, raw_input().split())
y = map(int, raw_input().split())
print sum([abs(x[i]-y[i]) for i in range(len(x))])
print math.sqrt(sum([(x[i] - y[i])**2 for i in range(len(x))]))
print pow(sum([abs(x[i] - y[i])**3 for i in range(len(x))]), 1/3.0)
print max([abs(x[i]-y[i]) for i in range(len(x))])

Solution (Python):
n = input()
x =map(int,raw_input().split())
y =map(int,raw_input().split())
sum_1,sum_2,sum_3  = [0,0,0]
sum_end = []
for i in range(n):
	if x[i] > y[i]:
		sum_1 += x[i]-y[i]
		sum_2 += (x[i]-y[i])**2
		sum_3 += (x[i]-y[i])**3
		sum_end.append(x[i]-y[i])
	else :
		sum_1 += y[i]-x[i]
		sum_2 += (y[i]-x[i])**2
		sum_3 += (y[i]-x[i])**3
		sum_end.append(y[i]-x[i])
print(sum_1)
print(pow(sum_2,0.5))
print(pow(sum_3,1.0/3.0))
sum_end.sort()
print(sum_end[len(sum_end)-1])

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

n_vec = zip(x, y)

xy_diff = [abs(xi - yi) for xi, yi in n_vec]

Ma_d = sum(xy_diff)
Eu_d = sum([i ** 2 for i in xy_diff]) ** 0.5
Mi3_d = sum([i ** 3 for i in xy_diff]) ** (1 / 3)
Ch_d = max(xy_diff)

print("{0}\n{1}\n{2}\n{3}".format(Ma_d, Eu_d, Mi3_d, Ch_d))

Solution (Python):
import math
n = int(raw_input())
x = []
y = []

in_line = raw_input().split()
for i in range(n):
    x.append(float(in_line[i]))
in_line = raw_input().split()
for i in range(n):
    y.append(float(in_line[i]))


d = 0.0
for i in range(n):
    d += math.fabs(x[i]-y[i])
print d

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

d = 0.0
for i in range(n):
    d += math.fabs(x[i]-y[i])**3
print d**(1.0/3.0)

d = 0.0
for i in range(n):
    if d < math.fabs(x[i]-y[i]):
        d = math.fabs(x[i]-y[i])
print d

Solution (Python):
import math

def manhattan(xs, ys):
    return sum([abs(x-y) for x, y in zip(xs, ys)])

def euclid(xs, ys):
    return math.sqrt(sum([(x-y)**2 for x, y in zip(xs, ys)]))

def l3(xs, ys):
    return sum([abs(x-y)**3 for x, y in zip(xs, ys)])**(1/3)

def chev(xs, ys):
    return max([abs(x-y) for x, y in zip(xs, ys)])

def norm():
    n = int(input())
    xs = list(map(int, input().split()))
    ys = list(map(int, input().split()))
    print(manhattan(xs, ys))
    print(euclid(xs, ys))
    print(l3(xs, ys))
    print(chev(xs, ys))

norm()

Solution (Python):
inf = float('inf')

def distance(p,n,x,y):
    D = 0
    s = []
    for i in range(0,n):
        s.append(x[i]-y[i])
        if s[i]<0.0:
            s[i]*=-1.0
        D += s[i]**p 
        
    if p==inf:
        return max(s)
    else:
        return D**(1.0/p)

if __name__ == '__main__':
    n = int(input())
    x = map(int,raw_input().split())
    y = map(int,raw_input().split())
        
    print float(distance(1,n,x,y))
    print float(distance(2,n,x,y))
    print float(distance(3,n,x,y))
    print float(distance(inf,n,x,y))

Solution (Python):
inf = float('inf')

def distance(p,n,x,y):
    D = 0
    s = []
    for i in range(0,n):
        s.append(x[i]-y[i])
        if s[i]<0.0:
            s[i]*=-1.0
        D += s[i]**p 
        
    if p==inf:
        return max(s)
    else:
        return D**(1.0/p)

if __name__ == '__main__':
    n = int(input())
    x = map(int,raw_input().split())
    y = map(int,raw_input().split())
        
    print float(distance(1,n,x,y))
    print float(distance(2,n,x,y))
    print float(distance(3,n,x,y))
    print float(distance(inf,n,x,y))

Solution (Python):

from math import *
n = input()
xli=map(float,raw_input().split())
yli=map(float,raw_input().split())
md1 = 0 
md2 = 0 
md3 = 0 
mdinf = 0 
for i in xrange(n):
    d = fabs(xli[i]-yli[i])
    md1 += d
    md2 += d**2
    md3 += d**3
    if d>mdinf:
        mdinf = d
md2 = md2**(1/2.0)
md3 = md3**(1/3.0)
print md1
print md2
print md3
print mdinf

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):
import math

n = int(input())
xv = [int(xi) for xi in input().split()]
yv = [int(yi) for yi in input().split()]

print(sum([abs(xi-yi) for (xi, yi) in zip(xv, yv)]))
print(math.sqrt(sum([pow(abs(xi-yi), 2) for (xi, yi) in zip(xv, yv)])))
print(math.pow(sum([pow(abs(xi-yi), 3) for (xi, yi) in zip(xv, yv)]), 1/3))
print(max([abs(xi-yi) for (xi, yi) in zip(xv, yv)]))

Solution (Python):
n = int(input())
data = list(zip([int(n) for n in input().split()], [int(n) for n in input().split()]))
dist = []
for pair in data:
    dist.append(abs(pair[0] - pair[1]))
print(sum(dist))
print(pow(sum(n ** 2 for n in dist), 1 / 2))
print(pow(sum(n ** 3 for n in dist), 1 / 3))
print(max(dist))

Solution (Python):
import math

n = input()

dis_1 = 0.0
dis_2 = 0.0
dis_3 = 0.0
dis_inf = 0.0

x = map(float, raw_input().split())
y = map(float, raw_input().split())

for i in xrange(n):
    dis_1 += abs(x[i] - y[i])
print "%.6f" % dis_1

for i in xrange(n):
    dis_2 += (x[i] - y[i])**2
print "%.6f" % math.sqrt(dis_2)

for i in xrange(n):
    dis_3 += abs(x[i] - y[i])**3
print "%.6f" % math.pow(dis_3, (1.0/3.0))

max_num = 0
for i in xrange(n):
    max_num = max(max_num, abs(x[i] - y[i]))
dis_inf = max_num
print "%.6f" % dis_inf

Solution (Python):
import math
n=int(input())
A=[]

for i in range(2):
	A.append(input().split())


Manhattan=0

Euclidean=0

Minkowski_3=0

Chebyshev=[]
for i in range(n):	
	a=math.fabs(int(A[0][i])-int(A[1][i]))
	Manhattan+=a
	Euclidean+=a**2
	Minkowski_3+=a**3
	Chebyshev.append(a)




Euclidean=math.sqrt(Euclidean)
Minkowski_3=Minkowski_3**(1/3)
Chebyshev.sort()
Chebyshev.reverse()
print(Manhattan)
print(Euclidean)
print(Minkowski_3)
print(Chebyshev[0])

Solution (Python):


import math

n = int(raw_input())
x = map(int, raw_input().split())
y = map(int, raw_input().split())
d1 = d2 = d3 = d4 = 0.0
for i in range(n):
    d = math.fabs(x[i]-y[i])
    d1 += d
    d2 += d*d
    d3 += d*d*d
    if d4 < d:
        d4 = d
d2 = math.pow(d2, (1.0/2.0))
d3 = math.pow(d3, (1.0/3.0))

print d1
print d2
print d3
print d4

Solution (Python):
import math
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

for p in range(1, 4):
    D = []
    for count in range(n):
        D.append((math.fabs(A[count] - B[count]))**p)
    if p==1:
        di = max(D)
    d=(sum(D))**(1/p)
    print('%.06f' % d)
print('%.06f' % di)

Solution (Python):
import math

n = input()
a = map(float, raw_input().split(" "))
b = map(float, raw_input().split(" "))

p1 = sum([abs(x-y) for x, y in zip(a, b)])
p2 = math.sqrt(sum([(x - y)**2 for x, y in zip(a, b)]))
p3 = math.pow(sum([math.fabs(x - y)**3 for x, y in zip(a, b)]), (1.0/3.0))
p = max([abs(x - y) for x, y in zip(a, b)])

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

Solution (Python):
import math

n = input()
x = [int(_) for _ in raw_input().split()]
y = [int(_) for _ in raw_input().split()]

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

print max([abs(float(i) - float(j)) for i,j in zip(x,y) ])

Solution (Python):
import math

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

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

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

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

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

Solution (Python):

import math

n = int(input())

x = list(input())
x = "".join(x).split()

y = list(input())
y = "".join(y).split()


dxy = [0 for i in range(4)]

for i in range(0, n):
    dxy[0] += math.fabs(int(x[i]) - int(y[i]))

for i in range(0, n):
    dxy[1] += (math.fabs(int(x[i]) - int(y[i]))) ** 2
dxy[1] = math.sqrt(dxy[1])

for i in range(0, n):
    dxy[2] += (math.fabs(int(x[i]) - int(y[i]))) ** 3
dxy[2] = math.pow(dxy[2], 1.0 / 3.0)

dxy[3] = math.fabs(int(x[0]) - int(y[0]))
for i in range(1, n):
    if dxy[3] < math.fabs(int(x[i]) - int(y[i])):
        dxy[3] = math.fabs(int(x[i]) - int(y[i]))

for i in range(0, 4):
    print("{0}".format(dxy[i]))

Solution (Python):
import math

n = input()
x = map(float, raw_input().split())
y = map(float, raw_input().split())
temp = p1 = p2 = p3 = pinf = 0

for i,j in zip(x, y):
    temp = math.fabs(i - j)
    p1 += temp
    p2 += temp ** 2
    p3 += temp ** 3
    if temp > pinf:
        pinf = temp

print u"%f\n%f\n%f\n%f" % (p1, p2**(1.0/2), p3**(1.0/3), pinf)

Solution (Python):
import math
n=int(input())
xs=list(map(float,input().split()))
ys=list(map(float,input().split()))
ds=[ abs(x - y) for (x,y) in zip(xs,ys) ]
print('{0:.6f}'.format(sum(ds)))
print('{0:.6f}'.format((sum(map(lambda x: x*x,ds)))**0.5))
print('{0:.6f}'.format((sum(map(lambda x: x*x*x,ds)))**(1./3.)))
print('{0:.6f}'.format(max(ds)))

Solution (Python):




















































n = int(input())
x = list( map(int, input().split()) )
y = list( map(int, input().split()) )
pp=[1.0, 2.0, 3.0, 500.0]


for p in pp:
	if p==500.0:
		print( "{0:.8f}".format( max( [abs(x[i]-y[i]) for i in range(n)] )  ))
	else:
		print( "{0:.8f}".format( sum( [abs(x[i]-y[i])**p for i in range(n)] )**(1./p)  ))

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(sum([abs(a-b)**p for a,b in zip(X,Y)])**(1/p)))

print("{0:.6f}".format(max([abs(a-b) for a,b in zip(X,Y)])))

Solution (Python):
import sys
import math

def str_input():
    S = raw_input()
    if S[len(S)-1] == "\r":
        return S[:len(S)-1]
    return S

def float_to_str(num):
    return str("{:.10f}".format(num))

def list_input(tp):
    return map(tp, str_input().split())



n = input()
a = list_input(float)
b = list_input(float)

for d in xrange(1, 4):
    dist = 0
    for i in xrange(n):
        dist += pow(abs(a[i] - b[i]), d)
    dist = pow(dist, 1./d)
    print float_to_str(dist)

dist = 0
for i in xrange(n):
    dist = max(dist, abs(a[i] - b[i]))
print dist

Solution (Python):
import math

Manhattan = 0
Euclid = 0
Three = 0
Chebychev = []
num = int(input())
x = list(map(float,input().split()))
y = list(map(float,input().split()))


for i in range(num):
	Manhattan += math.fabs(x[i] - y[i]) 
	Euclid += math.fabs((x[i] - y[i])**2)
	Three += math.fabs((x[i] - y[i])**3)
	Chebychev.append(math.fabs((x[i] - y[i])))

print(Manhattan)
print(math.sqrt(Euclid))
print(math.pow(Three,1/3))
print(max(Chebychev))

Solution (Python):
from math import sqrt



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

def Euclid(x,y):
	D=0.0
	for i in range(n):
		D+=(x[i]-y[i])**2
	D=sqrt(D)
	return D

def Noname(x,y):
	D=0.0
	for i in range(n):
		D+=abs((x[i]-y[i])**3)
	D**=1.0/3
	return D

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




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

print Manhattan(x,y)
print Euclid(x,y)
print Noname(x,y)
print Infinity(x,y)

Solution (Python):
from math import sqrt



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

def Euclid(x,y):
	D=0.0
	for i in range(n):
		D+=(x[i]-y[i])**2
	D=sqrt(D)
	return D

def Nazo(x,y):
	D=0.0
	for i in range(n):
		D+=abs((x[i]-y[i])**3)
	D**=1.0/3
	return D

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




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

print Manhattan(x,y)
print Euclid(x,y)
print Nazo(x,y)
print Infinity(x,y)

Solution (Python):
import math
n = int(input())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]

D = [x for i in range(4)]

for i in range(len(D)):
    if i == len(D) -1:
        D[i] = 0
        for j in range(n):
            di = math.fabs(x[j]-y[j])
            if D[i] < di:
                D[i] = di

    else:
        p = 1
        p += i
        di = 0
        d = 0
        for k in range(n):
            d += math.fabs(x[k]-y[k])**p
        di = d**(1/p)
        D[i] = di

for i in range(len(D)):
    print(D[i])

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

for p in range(1,4):
    s =sum([abs(x[i]-y[i])**p for i in range(n)])
    s = s**(1/p)
    print("%.6f" % s)

s = max([abs(x[i]-y[i]) for i in range(n)])
print("%.6f" % s)